Thursday, November 21, 2013

Screaming for XMLStreamReader!!

I am studying on how to extract data from xml file.Actually I need to learn it for my project requirement.So I googling around and found these nice links.

http://salesforceapexcodecorner.blogspot.com

http://blogs.developerforce.com

http://it.toolbox.com/

Some of it using XMLStreamReader and some using XMLNode.But I choose to use XMLStreamReader.Why? I think it enough for my project requirement and for this time being I am
able to understand XMLStreamReader better.

If you refer to links above, most of it giving simple XML which only contain one element.During my self study, I am trying to read XML file whereby the element contains sub elements such as below


<user xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<id>2262</id>
  <displayname>Unidha</displayname>
<email>unidha@yahoo.com</email>
<usergroups>
 <usergroup><id>301</id><name>Administrators</name></usergroup> <usergroup><id>302</id><name>Sales</name></usergroup>
</usergroups>
<city>Cybercity</city></user>
Notice that beside <Id>2262</Id> ,there Id for UserGroups which are 301 and 302.Now I am going to read the Id without get confuse which one is User id and which one is Group Id.The code below is wrote based on article in link I mentioned above.
public class MyXmlStreamReader {

   //object that hold information on transaction,user etc.
     class Employee{
        
        private List&lt;String&gt; GroupId{get;set;} //one user might have multiple group being assigned
        private Boolean autoProvision{get;set;}
        private Id userId{get;set;}
        private String email{get;set;}
        private String displayName{get;set;}
        private String city{get;set;}
      
   
    }
  
  public void test() {
     String str = '<user xmlns:i="http://www.w3.org/2001/XMLSchema-instance">'
                   +'<id>2262</id>'
                   +'<displayname>Unidha</displayname>'
                   +'<email>unidha@yahoo.com</email>'
                   +'<usergroups>'
                   +'<usergroup><id>301</id><name>Administrators</name></usergroup>'
                   +'<usergroup><id>302</id><name>Sales</name></usergroup>'
                   +'</usergroups>'
                   +'<city>Cybercity</city></user>';
                   

     
     XmlStreamReader xsr = new XmlStreamReader(str);
      
     readResponse(xsr);
        
        }


   public void readResponse(XmlStreamReader reader) {
   
   
    boolean isFlag=false;
    Employee   empRecord= new Employee ();
    empRecord.GroupId=new List&lt;String&gt;();
   
   while (reader.hasNext()) { 
       
        if (reader.getEventType() == XmlTag.START_ELEMENT) { 
           if ('Id' == reader.getLocalName()) {
              String strId= getValueFromTag(reader);
                          
              if(isFlag){
                 empRecord.GroupId.add(strId);
              }
             
            }
            else if ('DisplayName' == reader.getLocalName()) {
                empRecord.displayName= getValueFromTag(reader);
          
            }
             else if ('Email' == reader.getLocalName()) {
                empRecord.email= getValueFromTag(reader);
                
            }
           else if('UserGroups' == reader.getLocalName()) {
                isFlag=true;
            }      
            else if('City' == reader.getLocalName()) {
                empRecord.city= getValueFromTag(reader);
            }       
                
           }
          else if (reader.getEventType() == XmlTag.END_ELEMENT){
             if('UserGroups' == reader.getLocalName()){
                 isFlag=false;
             }
            }
      reader.next();
            
            
            }//end of while
     
      }
      
      }//end of class
Now, watch closely at isFlag variable.I use this boolean to indicate when the Id is UserId and when it is become the GroupId.

That's it.My problem is solved for this time being.Let's drink some coffeee with cinnamon roll.Yum yum!

Monday, November 18, 2013

Getting value from Custom Setting

I figured out how to getting value from custom setting. I know, I am too slow! :) .

The purpose of custom setting can be read here.But my purpose is to store the Application configuration such as Key.In java , maybe I will store


I create Custom Setting with details value:

Label : Application Config
Object Name: Application_Config__c 
Setting Type : List  [Please go to here for more knowledge on List/Hierarchy differences]
Visibility:Protected [I choose it by default]

Once created, you will able to create custom fields.Last time I tried to create Name field without realize Name is default field.

For this , I create one field named Value with API name Value__c .

I inserted the data with Name : App_Name and Value__c : Health Monitoring .

I just test in Apex Execute :

String str1=Application_Config__c.getValues('App_Name').Value__c;
system.debug('unid -'+str1);

So that is how we can call custom setting value.

Thanks.