• Tom Mujo
  • NEWBIE
  • 25 Points
  • Member since 2010

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 7
    Replies

Can anyone help with this?

 

I thought this would be the simplest part of my day but it isn't. I have just created a class that pulls down nodes from an XML file from an external server and inserted that into SFDC.

 

That all works fine when I use a hardcoded end point.

 

But my issue is I need a dynamic endpoint because the URL I pass through the external server will always have different content based on latitude and longitude fields that I have stored in a custom object called Incident__C.

 

The idea is that the my xml parser class is called from a trigger that runs AFTER an insert so I should be able to pick up fields froma newly created Incident__c record to create and end point. Only I can't get it to work.

 

Any ideas - heres my endpoint from the class....

 

req.setEndpoint('http://www.uk-postcodes.com/latlng/latitude__c,longitude__c.xml');

 

It never pulls through the fields from the record that has just been triggered.

 

Thoughts? Do I need to reference the newly created Incident__c somehow for these fields to be picked up?

 

Do you have to treat merged fields differently in a class?

Guys,

 

I've been working away on pulling one child element from an HTTPresponse into SFDC and I am so nealr ythere. I am complete noob with this but I get the sense I am close.

 

I can pull the full XML into the record - now I just need to refine it down to a single filed. The issue is I am getting a Null error from the field - but I know its there!!!!!

 

Here's the XML.

 

 

 

<result>
  <postcode>WA16 9NG</postcode> 
- <geo>
  <lat>53.247372</lat> 
  <lng>-2.34704</lng> 
  <easting>376939</easting> 
  <northing>372317</northing> 
  <geohash>http://geohash.org/gcqqft6uqj84</geohash> 
  </geo>
- <administrative>
- <constituency>
  <title /> 
  <uri /> 
  <code /> 
  </constituency>
- <district>
  <title>Cheshire East Council</title> 
  <uri>http://statistics.data.gov.uk/id/local-authority/00EQ</uri> 
  <snac>00EQ</snac> 
  </district>
- <ward>
  <title /> 
  <uri>http://statistics.data.gov.uk/id/electoral-ward/00EQMG</uri> 
  <snac>00EQMG</snac> 
  </ward>
  </administrative>
  </result>

 I am trying to get the title of the district into my SFDC.

 

So clearly the data is there. However, here's my debug...

 

 

9:0:4.895|USER_DEBUG|[30,5]|DEBUG|ukpostcode: XMLNode[ELEMENT,result,null,null,null,[XMLNode[TEXT,null,null,null,null,null,
	,], XMLNode[ELEMENT,postcode,null,null,null,[XMLNode[TEXT,null,null,null,null,null,WA16 9NG,]],null,], XMLNode[TEXT,null,null,null,null,null,
	,], XMLNode[ELEMENT,geo,null,null,null,[XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,lat,null,null,null,[XMLNode[TEXT,null,null,null,null,null,53.247372,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,lng,null,null,null,[XMLNode[TEXT,null,null,null,null,null,-2.34704,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,easting,null,null,null,[XMLNode[TEXT,null,null,null,null,null,376939,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,northing,null,null,null,[XMLNode[TEXT,null,null,null,null,null,372317,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,geohash,null,null,null,[XMLNode[TEXT,null,null,null,null,null,http://geohash.org/gcqqft6uqj84,]],null,], XMLNode[TEXT,null,null,null,null,null,
	,]],null,], XMLNode[TEXT,null,null,null,null,null,
	,], XMLNode[ELEMENT,administrative,null,null,null,[XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,constituency,null,null,null,[XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,title,null,null,null,null,null,], XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,uri,null,null,null,null,null,], XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,code,null,null,null,null,null,], XMLNode[TEXT,null,null,null,null,null,
		,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,district,null,null,null,[XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,title,null,null,null,[XMLNode[TEXT,null,null,null,null,null,Cheshire East Council,]],null,], XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,uri,null,null,null,[XMLNode[TEXT,null,null,null,null,null,http://statistics.data.gov.uk/id/local-authority/00EQ,]],null,], XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,snac,null,null,null,[XMLNode[TEXT,null,null,null,null,null,00EQ,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,ward,null,null,null,[XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,title,null,null,null,null,null,], XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,uri,null,null,null,[XMLNode[TEXT,null,null,null,null,null,http://statistics.data.gov.uk/id/electoral-ward/00EQMG,]],null,], XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,snac,null,null,null,[XMLNode[TEXT,null,null,null,null,null,00EQMG,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,]],null,], XMLNode[TEXT,null,null,null,null,null,
	,]],null,], XMLNode[TEXT,null,null,null,null,null,
,]],null,]

 

9:0:4.895|METHOD_EXIT|[30,5]|System.debug(String)
9:0:4.895|METHOD_ENTRY|[33,63]|dom.XmlNode.getName()
9:0:4.895|METHOD_ENTRY|[33,22]|dom.XmlNode.getChildElement(String, NULL)
9:0:4.895|METHOD_EXIT|[33,22]|dom.XmlNode.getChildElement(String, NULL)
9:0:4.895|EXCEPTION_THROWN|[33,22]|System.NullPointerException: Attempt to de-reference a null object
9:0:4.895|METHOD_EXIT|[33,63]|dom.XmlNode.getName()
9:0:4.895|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Look at all those nulls before the data is presented. Is that normal - is that what's causing my exception?

 

Here's my class - any help you can give me would be very much appreciated....

 

 

public class IncidentCouncilUpdater {

  //Future annotation to mark the method as async.
  @Future(callout=true)
  public static void updateIncident(String id, String name) {

    //construct an HTTP request
    HttpRequest req = new HttpRequest();
    req.setEndpoint('http://www.uk-postcodes.com/latlng/53.24354,-2.34567.xml');
    req.setMethod('GET');
    req.setTimeout(60000);
    
    //send the request
    Http http = new Http();
    HttpResponse res = http.send(req);
    
    // Log the XML content  
    Dom.Document doc = res.getBodyDocument();
    String district_title;
    
    //Retrieve the root element for this document.     
    Dom.XMLNode ukpostcode = doc.getRootElement();
    
    // print out specific elements by finding the specific node location 
    dom.XmlNode location = doc.getRootElement()
    .getChildElement('administrative',null)
    .getChildElement('district',null);  
    
    System.debug('ukpostcode: ' + ukpostcode);  
    
    // gets the content from the XML
    district_title = ukpostcode.getChildElement('title',null).getText();
   
      //update Incident
      Incident__c Inc = new Incident__c(Id=id);
      Inc.XMLLong__c = district_title;
    

 

Thanks in advance guys.

 

 

I need help guys.

 

First of all is it possible to call data from a URL web service and insert that back into a record from a trigger? If not how do I go about getting a single field from a web service and inserting it into a custom object record?

 

This what I have at the moment. I have data coming through the API which is where I get my merged fields from. Then I want this URL to return me a field called district which I then want inserting back into the record that has just ran the trigger.

 

Nothing happening although the code is being accepted. Any thoughts here? DO I need to upsert, update? SOmeone mentioned to me that I need the ID of the incident__c but if the trigger has just been launch form a specific record on Incident__c why would I need to do that? If I do need to do it what am I querying?

 

 

 

trigger LatLong on Incident__c (before update) {

    // Pass in the URL for the request  
    
    // For the purposes of this sample,assume that the URL  
    
    // returns the XML shown above in the response body  
    
    public void parseResponseDom(String url){
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        // url that returns the XML in the response body  
    
        req.setEndpoint('http://www.uk-postcodes.com/latlng/{!latitude__c}.{!longitude__c}.xml');
        req.setMethod('GET');
        HttpResponse res = h.send(req);
        Dom.Document doc = res.getBodyDocument();
        
        //Retrieve the root element for this document.  
    
        Dom.XMLNode result = doc.getRootElement();
        
        String district = result.getChildElement('district', null).getText();
        // print out specific elements  
        
        System.debug('district: ' + district);

        for(Dom.XMLNode child : result.getChildElements()) {

        System.debug(child.getText());

        }
        
        Incident__c I = [select ID from Incident__c 
             ];
             
        I.Council_Name_Text__c = district;
       
        update I;
        
  
        }
    }

Can anyone help with this?

 

I thought this would be the simplest part of my day but it isn't. I have just created a class that pulls down nodes from an XML file from an external server and inserted that into SFDC.

 

That all works fine when I use a hardcoded end point.

 

But my issue is I need a dynamic endpoint because the URL I pass through the external server will always have different content based on latitude and longitude fields that I have stored in a custom object called Incident__C.

 

The idea is that the my xml parser class is called from a trigger that runs AFTER an insert so I should be able to pick up fields froma newly created Incident__c record to create and end point. Only I can't get it to work.

 

Any ideas - heres my endpoint from the class....

 

req.setEndpoint('http://www.uk-postcodes.com/latlng/latitude__c,longitude__c.xml');

 

It never pulls through the fields from the record that has just been triggered.

 

Thoughts? Do I need to reference the newly created Incident__c somehow for these fields to be picked up?

 

Do you have to treat merged fields differently in a class?

Guys,

 

I've been working away on pulling one child element from an HTTPresponse into SFDC and I am so nealr ythere. I am complete noob with this but I get the sense I am close.

 

I can pull the full XML into the record - now I just need to refine it down to a single filed. The issue is I am getting a Null error from the field - but I know its there!!!!!

 

Here's the XML.

 

 

 

<result>
  <postcode>WA16 9NG</postcode> 
- <geo>
  <lat>53.247372</lat> 
  <lng>-2.34704</lng> 
  <easting>376939</easting> 
  <northing>372317</northing> 
  <geohash>http://geohash.org/gcqqft6uqj84</geohash> 
  </geo>
- <administrative>
- <constituency>
  <title /> 
  <uri /> 
  <code /> 
  </constituency>
- <district>
  <title>Cheshire East Council</title> 
  <uri>http://statistics.data.gov.uk/id/local-authority/00EQ</uri> 
  <snac>00EQ</snac> 
  </district>
- <ward>
  <title /> 
  <uri>http://statistics.data.gov.uk/id/electoral-ward/00EQMG</uri> 
  <snac>00EQMG</snac> 
  </ward>
  </administrative>
  </result>

 I am trying to get the title of the district into my SFDC.

 

So clearly the data is there. However, here's my debug...

 

 

9:0:4.895|USER_DEBUG|[30,5]|DEBUG|ukpostcode: XMLNode[ELEMENT,result,null,null,null,[XMLNode[TEXT,null,null,null,null,null,
	,], XMLNode[ELEMENT,postcode,null,null,null,[XMLNode[TEXT,null,null,null,null,null,WA16 9NG,]],null,], XMLNode[TEXT,null,null,null,null,null,
	,], XMLNode[ELEMENT,geo,null,null,null,[XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,lat,null,null,null,[XMLNode[TEXT,null,null,null,null,null,53.247372,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,lng,null,null,null,[XMLNode[TEXT,null,null,null,null,null,-2.34704,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,easting,null,null,null,[XMLNode[TEXT,null,null,null,null,null,376939,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,northing,null,null,null,[XMLNode[TEXT,null,null,null,null,null,372317,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,geohash,null,null,null,[XMLNode[TEXT,null,null,null,null,null,http://geohash.org/gcqqft6uqj84,]],null,], XMLNode[TEXT,null,null,null,null,null,
	,]],null,], XMLNode[TEXT,null,null,null,null,null,
	,], XMLNode[ELEMENT,administrative,null,null,null,[XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,constituency,null,null,null,[XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,title,null,null,null,null,null,], XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,uri,null,null,null,null,null,], XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,code,null,null,null,null,null,], XMLNode[TEXT,null,null,null,null,null,
		,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,district,null,null,null,[XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,title,null,null,null,[XMLNode[TEXT,null,null,null,null,null,Cheshire East Council,]],null,], XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,uri,null,null,null,[XMLNode[TEXT,null,null,null,null,null,http://statistics.data.gov.uk/id/local-authority/00EQ,]],null,], XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,snac,null,null,null,[XMLNode[TEXT,null,null,null,null,null,00EQ,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,], XMLNode[ELEMENT,ward,null,null,null,[XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,title,null,null,null,null,null,], XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,uri,null,null,null,[XMLNode[TEXT,null,null,null,null,null,http://statistics.data.gov.uk/id/electoral-ward/00EQMG,]],null,], XMLNode[TEXT,null,null,null,null,null,
			,], XMLNode[ELEMENT,snac,null,null,null,[XMLNode[TEXT,null,null,null,null,null,00EQMG,]],null,], XMLNode[TEXT,null,null,null,null,null,
		,]],null,], XMLNode[TEXT,null,null,null,null,null,
	,]],null,], XMLNode[TEXT,null,null,null,null,null,
,]],null,]

 

9:0:4.895|METHOD_EXIT|[30,5]|System.debug(String)
9:0:4.895|METHOD_ENTRY|[33,63]|dom.XmlNode.getName()
9:0:4.895|METHOD_ENTRY|[33,22]|dom.XmlNode.getChildElement(String, NULL)
9:0:4.895|METHOD_EXIT|[33,22]|dom.XmlNode.getChildElement(String, NULL)
9:0:4.895|EXCEPTION_THROWN|[33,22]|System.NullPointerException: Attempt to de-reference a null object
9:0:4.895|METHOD_EXIT|[33,63]|dom.XmlNode.getName()
9:0:4.895|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Look at all those nulls before the data is presented. Is that normal - is that what's causing my exception?

 

Here's my class - any help you can give me would be very much appreciated....

 

 

public class IncidentCouncilUpdater {

  //Future annotation to mark the method as async.
  @Future(callout=true)
  public static void updateIncident(String id, String name) {

    //construct an HTTP request
    HttpRequest req = new HttpRequest();
    req.setEndpoint('http://www.uk-postcodes.com/latlng/53.24354,-2.34567.xml');
    req.setMethod('GET');
    req.setTimeout(60000);
    
    //send the request
    Http http = new Http();
    HttpResponse res = http.send(req);
    
    // Log the XML content  
    Dom.Document doc = res.getBodyDocument();
    String district_title;
    
    //Retrieve the root element for this document.     
    Dom.XMLNode ukpostcode = doc.getRootElement();
    
    // print out specific elements by finding the specific node location 
    dom.XmlNode location = doc.getRootElement()
    .getChildElement('administrative',null)
    .getChildElement('district',null);  
    
    System.debug('ukpostcode: ' + ukpostcode);  
    
    // gets the content from the XML
    district_title = ukpostcode.getChildElement('title',null).getText();
   
      //update Incident
      Incident__c Inc = new Incident__c(Id=id);
      Inc.XMLLong__c = district_title;
    

 

Thanks in advance guys.