function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
George Laird 12George Laird 12 

Please help with XML response to populate a lead field.

Hello.  I'm quite new to callouts.  This is my first callout that is XML repsonse and not JSON.  My callout is working fine, and I'm getting a very long resonse in the debug log.  I need to popualte two custom fields on the Lead called "Bedrooms" and "Bathrooms."  These are decimal fields.   I can see the very long XML response in the debug log but I can't figure out how to get it in APEX and popualte the lead field.  Here is my apex class, which works , but I'm not sure what to add to get the bedrooms and bathrooms decimals and put them into the fields on the lead.  I'll post the apex class and the sample resonse from the debug log. 


global class ZillowCallout{
 
  @future (callout=true) 
   public static void getFields(Id lid) {
       
   string zwsid = 'X1-ZWz188ya5jca2z_4hjdy';
    
     Lead l=[select id,Name,Street,City,State,PostalCode from Lead where id=:lid];
     string addy = l.Street;
     string citystatezip = l.City + ',' + l.State + ',' + l.PostalCode;  

     
     //Construct HTTP request and response
     //Http request method, Header, and Endpoint
     HttpRequest req = new HttpRequest();
     req.setEndpoint('http://www.zillow.com/webservice/GetDeepSearchResults.htm?zws-id=' + zwsid + '&address=' + addy + '&citystatezip=' + citystatezip);  
     req.setMethod('GET');  
     req.setHeader('Content-Type','application/json');
     req.setHeader('Authorization', 'BASIC' + 'X1-ZWz188ya5jca2z_4hjdy'); 
    
       
     //Http response method 
     Http http = new Http();
     HTTPResponse res = http.send(req);
     System.debug(res.getStatusCode());
     string responseJSON = res.getBody();
     System.debug(responseJSON);
     Dom.Document doc = res.getBodyDocument();
     Dom.XMLNode address = doc.getRootElement();
       
  
  
       
       if(res.getStatusCode() < 300) {  
      // ResponseModel r = (ResponseModel)JSON.deserialize(responseJSON, ResponseModel.class);   
         ResponseModel r = new ResponseModel(); 
           
            
            //l.responseAcceptedBody__c = 'Time: ' +system.now() +' - ' +responseJSON; 
            l.Bathrooms__c = r.Bathrooms;
            l.Bedrooms__c = r.Bedrooms;   
   
            update l;   
            
                   
       }
       
       else {
     
        //l.responseErrorBody__c = 'Time: ' +system.now() +' - ' +responseJSON;    
  
          update l;   
       }
       

   }
    

  
  //This inner class is to handle the mapping in reference to the response.
    private class ResponseModel{
        
        public Decimal Bathrooms;
        public Decimal Bedrooms;
        
        
    }
        
        
}





Here is a sample response from debug log:

09:23:56:190 USER_DEBUG [36]|DEBUG|<?xml version="1.0" encoding="utf-8"?><SearchResults:searchresults xsi:schemaLocation="http://www.zillow.com/static/xsd/SearchResults.xsd https://www.zillowstatic.com/vstatic/18f7df5/static/xsd/SearchResults.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SearchResults="http://www.zillow.com/static/xsd/SearchResults.xsd"><request><address>5536Foxrun</address><citystatezip>Cincinnati,OH,45239</citystatezip></request><message><text>Request successfully processed</text><code>0</code></message><response><results><result><zpid>34264904</zpid><links><homedetails>https://www.zillow.com/homedetails/5536-Foxrun-Ct-Cincinnati-OH-45239/34264904_zpid/</homedetails><graphsanddata>http://www.zillow.com/homedetails/5536-Foxrun-Ct-Cincinnati-OH-45239/34264904_zpid/#charts-and-data</graphsanddata><mapthishome>http://www.zillow.com/homes/34264904_zpid/</mapthishome><comparables>http://www.zillow.com/homes/comps/34264904_zpid/</comparables></links><address><street>5536 Foxrun Ct</street><zipcode>45239</zipcode><city>CINCINNATI</city><state>OH</state><latitude>39.192961</latitude><longitude>-84.57547</longitude></address><FIPScounty>39061</FIPScounty><useCode>SingleFamily</useCode><taxAssessmentYear>2017</taxAssessmentYear><taxAssessment>151220.0</taxAssessment><yearBuilt>1966</yearBuilt><lotSizeSqFt>12458</lotSizeSqFt><finishedSqFt>2912</finishedSqFt><bathrooms>3.0</bathrooms><bedrooms>4</bedrooms><totalRooms>9</totalRooms><zestimate><amount currency="USD">188146</amount><last-updated>11/18/2018</last-updated><oneWeekChange deprecated="true"></oneWeekChange><valueChange duration="30" currency="USD">1541</valueChange><valuationRange><low currency="USD">178739</low><high currency="USD">197553</high></valuationRange><percentile>0</percentile></zestimate><localRealEstate><region name="Mt. Airy" id="274620" type="neighborhood"><zindexValue>119,400</zindexValue><links><overview>http://www.zillow.com/local-info/OH-Cincinnati/Mt.-Airy/r_274620/</overview><forSaleByOwner>http://www.zillow.com/mt.-airy-cincinnati-oh/fsbo/</forSaleByOwner><forSale>http://www.zillow.com/mt.-airy-cincinnati-oh/</forSale></links></region></localRealEstate></result><result><zpid>110070415</zpid><links><homedetails>https://www.zillow.com/homedetails/5536-Foxrun-Ct-Cincinnati-OH-45239/110070415_zpid/</homedetails><graphsanddata>http://www.zillow.com/homedetails/5536-Foxrun-Ct-Cincinnati-OH-45239/110070415_zpid/#charts-and-data</graphsanddata><mapthishome>http://www.zillow.com/homes/110070415_zpid/</mapthishome><comparables>http://www.zillow.com/homes/comps/110070415_zpid/</comparables></links><address><street>5536 Foxrun Ct</street><zipcode>45239</zipcode><city>CINCINNATI</city><state>OH</state><latitude>39.192961</latitude><longitude>-84.57547</longitude></address><FIPScounty>39061</FIPScounty><useCode>SingleFamily</useCode><taxAssessmentYear>2017</taxAssessmentYear><yearBuilt>1965</yearBuilt><lotSizeSqFt>48918</lotSizeSqFt><finishedSqFt>2908</finishedSqFt><bathrooms>4.0</bathrooms><bedrooms>4</bedrooms><totalRooms>13</totalRooms><zestimate><amount currency="USD">240166</amount><last-updated>11/18/2018</last-updated><oneWeekChange deprecated="true"></oneWeekChange><valueChange duration="30" currency="USD">-997</valueChange><valuationRange><low currency="USD">194534</low><high currency="USD">293003</high></valuationRange><percentile>0</percentile></zestimate><localRealEstate><region name="Mt. Airy" id="274620" type="neighborhood"><zindexValue>119,400</zindexValue><links><overview>http://www.zillow.com/local-info/OH-Cincinnati/Mt.-Airy/r_274620/</overview><forSaleByOwner>http://www.zillow.com/mt.-airy-cincinnati-oh/fsbo/</forSaleByOwner><forSale>http://www.zillow.com/mt.-airy-cincinnati-oh/</forSale></links></region></localRealEstate></result></results></response></SearchResults:searchresults><!-- H:027  T:88ms  S:2041  R:Sat Nov 24 09:23:57 PST 2018  B:5.0.57261.8-hotfix_2018-11-13.68abad3~hotfix-for-2018-11-13.bfcf05d -->



 
Best Answer chosen by George Laird 12
Raj VakatiRaj Vakati
Try this code .. and its working for me 
 
global class ZillowCallout{
    
    //  @future (callout=true) 
    public static void getFields(Id lid) {
        string zwsid = 'X1-ZWz188ya5jca2z_4hjdy';
        Lead l=[select id,Name,Street,City,State,PostalCode from Lead where id=:lid];
        string addy = l.Street;
        string citystatezip = l.City + ',' + l.State + ',' + l.PostalCode;  
        //Construct HTTP request and response
        //Http request method, Header, and Endpoint
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://www.zillow.com/webservice/GetDeepSearchResults.htm?zws-id=' + zwsid + '&address=' + addy + '&citystatezip=' + citystatezip);  
        req.setMethod('GET');  
        req.setHeader('Content-Type','application/json');
        req.setHeader('Authorization', 'BASIC' + 'X1-ZWz188ya5jca2z_4hjdy'); 
        //Http response method 
        Http http = new Http();
        HTTPResponse res = http.send(req);
        string respoXML = res.getBody();
        //System.debug(respoXML);
        if(res.getStatusCode() < 300) {  
            
            ResponseModel rm = new ResponseModel() ;
            
            Dom.Document doc = new Dom.Document();
            doc.load(respoXML);//Here xmlstring data is from the above XML shown above 
            //Retrieve the root element for this document.
            Dom.XMLNode node= doc.getRootElement();
            
            Dom.XMLNode[] childs = node.getChildElements() ;
            for(Dom.XmlNode nod :childs){
                Dom.XMLNode[] childs1= nod.getChildElements() ;
                for(Dom.XmlNode nod1 :childs1){
                    // System.debug('-->'+nod1.getText()+'--Key'+nod1.getName());
                    for(Dom.XmlNode nod11 :nod1.getChildElements()){
                        //   Dom.XmlNode[] finalLevel = nod11.getChildren();
                        for(Dom.XmlNode finalLevel12 :nod11.getChildElements()){
                            if(finalLevel12.getName()=='bedrooms'){
                                rm.Bedrooms  =Decimal.valueOf( finalLevel12.getText());
                            }
                            if(finalLevel12.getName()=='bathrooms'){
                                rm.Bathrooms  =  Decimal.valueOf(finalLevel12.getText());
                                
                            }
                        }
                        
                        
                    }
                }
            }
            
            System.debug('rmrmrm'+rm);  
            
            l.Bathrooms__c = rm.Bathrooms;
            l.Bedrooms__c = rm.Bedrooms;   
            
            update l ;
            
        }
        else {
            //l.responseErrorBody__c = 'Time: ' +system.now() +' - ' +responseJSON;    
            update l;   
        }
    }
    //This inner class is to handle the mapping in reference to the response.
    public class ResponseModel{
        public Decimal Bathrooms{get;set;}
        public Decimal Bedrooms{get;set;}
    }
    
}

 

All Answers

Raj VakatiRaj Vakati
Try this code .. and its working for me 
 
global class ZillowCallout{
    
    //  @future (callout=true) 
    public static void getFields(Id lid) {
        string zwsid = 'X1-ZWz188ya5jca2z_4hjdy';
        Lead l=[select id,Name,Street,City,State,PostalCode from Lead where id=:lid];
        string addy = l.Street;
        string citystatezip = l.City + ',' + l.State + ',' + l.PostalCode;  
        //Construct HTTP request and response
        //Http request method, Header, and Endpoint
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://www.zillow.com/webservice/GetDeepSearchResults.htm?zws-id=' + zwsid + '&address=' + addy + '&citystatezip=' + citystatezip);  
        req.setMethod('GET');  
        req.setHeader('Content-Type','application/json');
        req.setHeader('Authorization', 'BASIC' + 'X1-ZWz188ya5jca2z_4hjdy'); 
        //Http response method 
        Http http = new Http();
        HTTPResponse res = http.send(req);
        string respoXML = res.getBody();
        //System.debug(respoXML);
        if(res.getStatusCode() < 300) {  
            
            ResponseModel rm = new ResponseModel() ;
            
            Dom.Document doc = new Dom.Document();
            doc.load(respoXML);//Here xmlstring data is from the above XML shown above 
            //Retrieve the root element for this document.
            Dom.XMLNode node= doc.getRootElement();
            
            Dom.XMLNode[] childs = node.getChildElements() ;
            for(Dom.XmlNode nod :childs){
                Dom.XMLNode[] childs1= nod.getChildElements() ;
                for(Dom.XmlNode nod1 :childs1){
                    // System.debug('-->'+nod1.getText()+'--Key'+nod1.getName());
                    for(Dom.XmlNode nod11 :nod1.getChildElements()){
                        //   Dom.XmlNode[] finalLevel = nod11.getChildren();
                        for(Dom.XmlNode finalLevel12 :nod11.getChildElements()){
                            if(finalLevel12.getName()=='bedrooms'){
                                rm.Bedrooms  =Decimal.valueOf( finalLevel12.getText());
                            }
                            if(finalLevel12.getName()=='bathrooms'){
                                rm.Bathrooms  =  Decimal.valueOf(finalLevel12.getText());
                                
                            }
                        }
                        
                        
                    }
                }
            }
            
            System.debug('rmrmrm'+rm);  
            
            l.Bathrooms__c = rm.Bathrooms;
            l.Bedrooms__c = rm.Bedrooms;   
            
            update l ;
            
        }
        else {
            //l.responseErrorBody__c = 'Time: ' +system.now() +' - ' +responseJSON;    
            update l;   
        }
    }
    //This inner class is to handle the mapping in reference to the response.
    public class ResponseModel{
        public Decimal Bathrooms{get;set;}
        public Decimal Bedrooms{get;set;}
    }
    
}

 
This was selected as the best answer
George Laird 12George Laird 12
@raj vakati , thank you!  I actually figured out how to do it before you posted your answer, but I think your approach is much better!  Thank you so much!
vinayak rastevinayak raste
i am also tring the same but getting error at line ' HTTPResponse res = http.send(req);'...please help
 
vinayak rastevinayak raste
please tell me...from where you are giving id of the record to this method