• HowToDo ItInSalesforce
  • NEWBIE
  • 5 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 10
    Replies
I am currently trying to parse an XML response, however it does not seem to want to parse whats being returned, and im not sure if its because of the complexity of the response, or if im doing something wrong.

I am using HttpRequests to send requets to our api. 

Here is the xml the API responds with:
 
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <getGLBranchResponse xmlns="our end point is here">
      <getGLBranchResult>
        <Branch>
          <ID>
            <Value>int</Value>
            <Valid>boolean</Valid>
          </ID>
          <Code>
            <Value>string</Value>
            <Valid>boolean</Valid>
          </Code>
        </Branch>
        <BranchName>
          <Value>string</Value>
          <Valid>boolean</Valid>
        </BranchName>
        <Customer>
          <ID>
            <Value>int</Value>
            <Valid>boolean</Valid>
          </ID>
          <Code>
            <Value>string</Value>
            <Valid>boolean</Valid>
          </Code>
        </Customer>
        <DefaultWarehouse>
          <ID>
            <Value>int</Value>
            <Valid>boolean</Valid>
          </ID>
          <Code>
            <Value>string</Value>
            <Valid>boolean</Valid>
          </Code>
        </DefaultWarehouse>
        <MainBranch>
          <Value>boolean</Value>
          <Valid>boolean</Valid>
        </MainBranch>
        <Active>
          <Value>boolean</Value>
          <Valid>boolean</Valid>
        </Active>
      </getGLBranchResult>
    </getGLBranchResponse>
  </soap:Body>
</soap:Envelope>

I use Dom.Document doc = res.getBodyDocument(); to get the document from the HTTP response. 

When I call Dom.XMLNode rootele = doc.getRootElement(); and put it in a system.debug, it doesnt really make much sense, returns something like:
 
[13]|DEBUG|XMLNode[ELEMENT,Envelope,http://schemas.xmlsoap.org/soap/envelope/,null,[common.apex.api.dom.XmlNode$NamespaceDef@c2d03a7, common.apex.api.dom.XmlNode$NamespaceDef@59ff7f61, common.apex.api.dom.XmlNode$NamespaceDef@32f5bbfc],[XMLNode[ELEMENT,Body,http://schemas.xmlsoap.org/soap/envelope/,null,null, etc

when I call getChildElement on this, it returns null, everytime. I have tried calling getChildElement('Envelope',null)​, getChildElement('Body',null)​ and many others. Always returns null. Am i doing something wrong? 
Hi,

Can anyone please guide me as to how do we write the test class for a Web Service.

Thanks.
Hi,

I am confused while it comes to order of execution of 'before triggers' and 'validation rules' while saving a record in salesforce. Salesforce document says-1)System Validation 2)Before Triggers 3)Custom Validation. I had this question in my developer exam and 2 options were confusing.
A)Before Triggers  B) Validation Rules,
Options were-1)AB 2)BA 
Which one of above is correct?

 
Hi All
can you please share interview questions and different senarios which ask for experienced sfdc developers
 
Hi everyone,

The company I work for is looking for a Senior Salesforce Developer for one of our clients. I have been asked to come up with some questions to ask Senior Salesforce Developers that interviewers at my company can ask applicants to verify their knowledge. If someone could provide me with some questions that I could ask a Senior Salesforce Developer to verify their knowledge of salesforce and their experience that would be fantastic.

Thanks,
XmlParseVFPage:

<apex:page Controller="Xmlparsar">

<apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Parse Xml" action="{!Parsexml}" />   
                <apex:commandButton value="ParseXML File" action="{!Parsexmlfile}"/>
            </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:outputLabel value="Select Xml File" for="file"/>
            <apex:inputFile fileName="{!fileName}" value="{!body}"/>
        </apex:pageBlock>
    </apex:form>
   
</apex:page>

Apex Class

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())
       {        
         
         
          loadChilds(xmlnodeobj);         
       }      
       return null;
    }
   
    //loading the child elements
    public void loadChilds(DOM.XMLNode xmlnode)
    {
        for(Dom.XMLNode child : xmlnode.getChildElements())
        {
          if(child.getText()!= null)
          {
          outxmlstring+='\n'+child.getName()+': '+child.getText();
       
          }
          loadChilds(child);       
        }
    }
   
   
//This is for parsing xml file what you selected
  public pagereference Parsexmlfile()
  {
       DOM.Document xmlDOC = new DOM.Document();
       xmlstring=body.tostring();         
       xmlDOC.load(xmlstring);
       DOM.XMLNode rootElement = xmlDOC.getRootElement();
       outxmlstring=String.valueof(xmlDOC.getRootElement().getName());//gives you root element Name
       for(DOM.XMLNode xmlnodeobj:xmlDOC.getRootElement().getChildElements())
       {        
                  
          loadChilds(xmlnodeobj); 
       }      
      return null;
    }
}

Apex Test Class

@isTest
public class XmlParsarTest
{
    
    //test for parse xml only
    @isTest static void Parsexml()
    {      
        Xmlparsar xmlpar=new Xmlparsar();
        PageReference pageRef = Page.XmlParseVFPage;
        Test.setCurrentPage(pageRef);
        xmlpar.xmlstring='<?xml version="1.0"?><catalog><book id="bk101"><author>Gambardella, Matthew</author></book></catalog>';
        xmlpar.Parsexml();
        System.assertNotEquals(null, xmlpar.outxmlstring);
    }
   
    @isTest static void ParsexmlFile()
    {
        Xmlparsar xmlpar=new Xmlparsar();
        PageReference pageRef = Page.XmlParseVFPage;
        Test.setCurrentPage(pageRef);
        List<StaticResource> resourceList=[SELECT body FROM StaticResource WHERE Name = :'testxmlfile' limit 1];
        xmlpar.body=resourceList[0].body;
        System.debug('xmlpar.body'+xmlpar.body);
        System.debug('xmlpar.xmlstring'+xmlpar.xmlstring);
        xmlpar.Parsexmlfile();
        System.assertNotEquals(null, xmlpar.outxmlstring);
    }
   
  
}

Hi guys,

 

Can you clarify the meaning and execution process of both system mode and user mode?

 

 

Hi All,

 

As shown below the  execution of the process order is right  r not?if not so Please do the changes and let me know.

 

Validation

Apex class

Triggers

Workflows

 

Thanks