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
thomasarnold81thomasarnold81 

GET HTTP XML Updates on Custom Objects

Hi Guys,

 

I think I am pretty close here.

 

The GET HTTP should send out a custom built URL from two fields on a custom object (not all together different from a Case style page). The return information is XML and I am trying to pull down a specific field and insert that back onto into the custom record.

 

Any ideas where I am going wrong. I am certainly struggling with the update part of the APEX code. 

 

Any thoughts would be helpful;

 

 

trigger LatLong on Incident__c (before insert, before update) {
    
    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 postcode = result.getChildElement('postcode', null).getText();
        // print out specific elements  
        
        System.debug('postcode: ' + postcode);
        for(Dom.XMLNode child : result.getChildElements()) {
        System.debug(child.getText());
        }
        
        Incident__c I = [select Council_Name_Text__c from Incident__c 
             where Council_Name_Text__c = null limit 1];
             
        I.Council_Name_Text__c = postcode;
       
        update I;
        
  
        }
    }
aalbertaalbert

To perform an update, you need the Id of the record. So make sure you query for the Id field in our Incident SOQL query.

thomasarnold81thomasarnold81

Ok thats makes sense.

 

However, I forgot to point out that I am pulling this data in through the API from a mobile phone.

 

There is also another trigger that works off the data I am trying to input here which works on a before insert as the data comes through the API.

 

Would it make sense for me to just combine the two as I am a bit confused as to what order triggers happen on objects?

 

Any thoughts guys?

 

Ideally I want the data to come through the api, both triggers then run to fill in the blanks that I need from an external data source and to grab some internal field update info, then I want the object saving. Is that the best way to do it?

 

I am assuming before insert is the trigger command I need here.

 

Thanks.

thomasarnold81thomasarnold81

hang on I think I have an issue. 

 

If I am running this trigger before an insert then there is no ID to hang this from. 

 

How do I couple this trigger with the data that is coming through the API without having an ID?

dnakonidnakoni

Is your   public void parseResponseDom(String url){ } inside the trigger? How are you calling that method?

 

Also, how are you merging the {!latitude__c} and {!longitude__c} values?

thomasarnold81thomasarnold81

Hi Daniel,

 

Thanks for replying.

 

The fields are taken from the custom object but I suppose if they haven't been inserted yet then they aren't there to merge is one point I think you are making. That data is coming through the API but hasn't been saved at this point.

 

I suppose I could an after insert instead?

 

Secondly, in terms of your first question I am a little out of my depth. I copied this code from the GET HTTP example on the developer force site so I take it I need to be doing something else with that function.

 

Does it need to be created as a separate class?