Tuesday 11 July 2017

'
 Code TO Parse XML  Using Apex Through DOM
public  class Xmlparsar
{
    //xml string
    public String xmlstring{get;set;}
  
    //display xml string
    public String outxmlstring{get;set;}
  
    //rootelement
    public String rootElement{get;set;}
  
    //
    public String filename{get;set;}
  
    public blob body{get;set;}
     
    //constructor
    public Xmlparsar()
    {
   
    }
  
   
//Parsing xml what you entered in the left text area
    public pagereference Parsexml()
    {
       DOM.Document xmlDOC = new DOM.Document();
       xmlDOC.load(xmlstring);
       DOM.XMLNode rootElement = xmlDOC.getRootElement();
       outxmlstring=String.valueof(xmlDOC.getRootElement().getName());
      
       for(DOM.XMLNode xmlnodeobj:xmlDOC.getRootElement().getChildElements())
       //.getChildren())
       {       
        
         if(xmlnodeobj.getChildElements().size()== 0)
          {
          outxmlstring+='\n'+xmlnodeobj.getName()+': '+xmlnodeobj.getText();
      
          }
          System.debug(xmlnodeobj);
          loadChilds(xmlnodeobj);        
       }
       
            
       return null;
    }
  
    //loading the child elements
    public void loadChilds(DOM.XMLNode xmlnode)
    {

        
         if(xmlnode.getChildElements().size()>0){
          outxmlstring+='\n'+xmlnode.getName();
        }
       
        for(Dom.XMLNode child : xmlnode.getChildElements())
        {
         
         
          if(child.getText()!= null)
          {
          outxmlstring+='\n'+child.getName()+': '+child.getText();
      
          }
          loadChilds(child);      
        }
        System.debug(xmlnode.getChildElements());
       
    
    }
  
  

}

VisualForce Page
<apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Parse Xml" action="{!Parsexml}" />  
            </apex:pageBlockButtons>
            <apex:inputTextArea value="{!xmlstring}" style="width:336px;height:260px;"/> &nbsp;&nbsp;
            <apex:inputTextArea value="{!outxmlstring}" style="width:336px;height:260px;" id="response"/><br/>
        </apex:pageBlock>
    </apex:form>







Wednesday 28 June 2017

Insertion Sort

Start empty handed
Insert  a card in the right postion of the already sorted hand.
Continue until all cards are inserted/sorted.



Integer [] a=new Integer[]{50,11,23,10,20,30,40,10};

Integer n = a.size();
for (Integer i=1; i<n; ++i)
{
    Integer key = a[i];
    Integer j = i-1;

    /* Move elements of a[0..i-1], that are
    greater than key, to one position ahead
    of their current position */
    while (j>=0 && a[j] > key)
    {
        a[j+1] = a[j];
        j = j-1;
    }
    a[j+1] = key;
}

for (Integer i=0; i<n; ++i)
System.debug(a[i] + '  ');




Code in Apex to get Maximum Number from  an Array

                Integer [] a=new Integer[]{10,20,30,40};

                Integer currentMax=a[0];

                for(integer i =1;i<=a.size()-1;i++){
                   if(currentMax<a[i]){
                       currentMax=a[i];
                  }
                }


Friday 15 July 2016

Code to Get All Parent and Child Object For A Give Object


Parent Object


Set<String> testName= new set<String>();

public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();

Map <String, Schema.SObjectField> fieldMap = schemaMap.get('Account').getDescribe().fields.getMap();
for(Schema.SObjectField sfield : fieldMap.Values())
{
schema.describefieldresult dfield = sfield.getDescribe();


if(dfield.getRelationshipName()!=null){
SObjectField sObjfieldName  =dfield.getSobjectField();
//System.debug(sObjfieldName  );
String abc=sObjfieldName  +'';

Map<String, Schema.SobjectField> fields = Account.getSObjectType().getDescribe().fields.getMap();

Schema.DescribeFieldResult f=fields.get(abc).getDescribe();



for(Schema.SObjectType reference : f.getReferenceTo()) {
testName.add(reference.getDescribe().getName());
}

}


}

for(String itr :testName){
System.debug(itr );
}


Child Object

// To Retrieve Sechma of the given Object
Schema.DescribeSObjectResult R = Account.SObjectType.getDescribe();
// Reterieving relation ship field
List<Schema.ChildRelationship> C = R.getChildRelationships();
Set<String> abc= new Set<String>();
// iterating all the fields
for (Schema.ChildRelationship child : r.getChildRelationShips()) {
   // from field getting its sobject describe call
   Schema.DescribeSObjectResult childDesc = child.getChildSObject().getDescribe();
   // from that getting its name and passing in set
   abc.add(childDesc.getName());
}
for(String itr :abc){
System.debug(itr);
}

Wednesday 11 March 2015

ENUM


An enum is an abstract data type with values that each take on exactly one of a finite set of identifiers that you specify. Enums are typically
used to define a set of possible values that don’t otherwise have a numerical order, such as the suit of a card, or a particular season of
the year. Although each value corresponds to a distinct integer value, the enum hides this implementation so that you don’t inadvertently
misuse the values, such as using them to perform arithmetic. After you create an enum, variables, method arguments, and return types
can be declared of that type.


public enum Season {WINTER, SPRING, SUMMER, FALL}
Season e = Season.WINTER;

public Season m(Integer x, Season e1) {
if (e1 == Season.SUMMER) {
return Season.SUMMER;
}
else{
return Season.WINTER;
}
}

System.debug(m(1,Season.SUMMER));

Tuesday 10 March 2015

Uniqueness of map keys of user-defined types is determined by the equals and hashCode methods,


public class PairNumbers {
    Integer x,y;

    public PairNumbers(Integer a, Integer b) {
        x=a;
        y=b;
    }

    public Boolean equals(Object obj) {
        if (obj instanceof PairNumbers) {
            PairNumbers p = (PairNumbers)obj;
            return ((x==p.x) && (y==p.y));
        }
        return false;
    }

    public Integer hashCode() {
        return (31 * x) * y;
    }
}

Use case:

Map<PairNumbers, String> m = new Map<PairNumbers, String>();
PairNumbers p1 = new PairNumbers(1,2);
PairNumbers p2 = new PairNumbers(3,4);
// Duplicate key
PairNumbers p3 = new PairNumbers(1,2);
PairNumbers p4 = new PairNumbers(2,1);

m.put(p1, 'first');
m.put(p2, 'second');
m.put(p3, 'third');
m.put(p4, 'third');


// Map size is 2 because the entry with 
// the duplicate key overwrote the first entry.
System.assertEquals(2, m.size());

// Use the == operator
if (p1 == p3) {
    System.debug('p1 and p3 are equal.');
}

// Perform some other operations
System.assertEquals(true, m.containsKey(p1));
System.assertEquals(true, m.containsKey(p2));
System.assertEquals(false, m.containsKey(new PairNumbers(5,6)));

for(PairNumbers pn : m.keySet()) {
    System.debug('Key: ' + pn);
}

List<String> mValues = m.values();
System.debug('m.values: ' + mValues);

// Create a set
Set<PairNumbers> s1 = new Set<PairNumbers>();
s1.add(p1);
s1.add(p2);
s1.add(p3);

// Verify that we have only two elements
// since the p3 is equal to p1.
System.assertEquals(2, s1.size());

Sunday 15 February 2015

Common Trigger Problems


  1. Cascading Triggres
  2. Apex governor Limits
  3. Null Refernce errors
  4. Too many Soql