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
Tom MujoTom Mujo 

HTTP Get XML Issue and Insert of Data

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;
        
  
        }
    }

BritishBoyinDCBritishBoyinDC

Did you get this to work? I have done something similar with the BitLy API, so I think it can be done.

 

Couple of things though - I didn't think you could call an webservice from a trigger like this - because of the nature of web services, you have to call them with the @future set...so you can call a class set to work in the @future mode, but it won't then update the record in the UI after save, but if you refresh the record, it probably would then appear.

 

So I would move the code you have to a separate class that is marked as @future, and then pass in  Trigger.new[0].Id to that class. However, since you can't call more than 10 callouts per execution,  you can't ever make this work in bulk....but it will work for one record...