-3

Apex call out has an xml response.How can I map the fields in Salesforce to the response I receieved. I could map the fields like clean etc. But how could I map fields like make,model, year relatively.

Assume the fields are : Make__c ,Model__c etc.... Considering the XML response.

XML response is:

<?xml version="1.0" encoding="UTF-8"?>
<result>
  <vin>xxxxxxxxxxx</vin>
  <id>xxxxxxxxxxx</id>
  <date>2018-02-08 05:02:15 PST</date>
  <specs>
    <VIN />
    *<Year>2011</Year>
    <Make>Chevrolet</Make>
    <Model>Suburban</Model>*
    <Trim>LS 1500</Trim>
    </specs>




  <clean>0</clean>
  <success>1</success>
  <error />
</result>

    public PageReference doVinExtract()
    { 

            HttpRequest httpRequest = new HttpRequest();
            httpRequest.setEndpoint('https:/VinProviderxxxx.php');
            String body = 'id=01123999999198&key=xxxxxxx&vin=1GNSKHE34BRxxxxxxx&mode=test&user=vxxxxxx&pass=slll4250&format=xml';
            httpRequest.setBody(body);
            httpRequest.setMethod('GET');
            Http htt = new hTTP();
            HttpResponse httpRe = htt.send(httpRequest);
            system.debug(httpRe.geTbody());
            String re = httpRe.geTbody();

            Dom.Document doc = httpRe.getBodyDocument();
            Dom.XMLNode address = doc.getRootElement();


        String vinDate= address.getChildElement('date', null).getText();

       String state = address.getChildElement('specs', null).getText();
        // print out specific elements

        System.debug('address: ' + address);
          System.debug('state: ' + state);



            system.debug('result'+re);


             for(Dom.XMLNode child : address.getChildElements()) {
             if(child.getName() == 'specs'){
           System.debug('******'+child.getText());
           }
        }




          }

Any help is gladly appreciated.

  • 3
    Please make an effort to format your code so that your question can be read. Please also edit your question to make it specific: what exactly are you struggling with, and what have you tried to solve the problem? Consider including only limited, relevant portions of your source code. – David Reed Feb 8 '18 at 19:00
  • 1
    Your post doesn't contain a question. What are you trying to achieve? Where are you stuck? – Adrian Larson Feb 8 '18 at 19:01
  • I made the recommended edits. I was struck and could not map these three fields: *<Year>2011</Year> <Make>Chevrolet</Make> <Model>Suburban</Model> to the fields in salesforce. – Vamsi Feb 8 '18 at 19:31
3

You need to go down a further level in the nodes:

Dom.Document doc = httpRe.getBodyDocument();
Dom.XMLNode result = doc.getRootElement();

String vinDate= result.getChildElement('date', null).getText();
...

Dom.XMLNode specs = result.getChildElement('specs', null);
String year = specs.getChildElement('Year', null).getText();
String make = specs.getChildElement('Make', null).getText();
String model = specs.getChildElement('Model', null).getText();
...
| improve this answer | |
  • We need this callouts to be run synchronously. And use the response to map the fields. i wrote it as a batch to update 7000 records But when I try to do it, it says "You have uncommitted work pending. Please commit or rollback before calling out". What is the work around?? – Vamsi May 23 '18 at 14:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.