+ Start a Discussion
Tom MujoTom Mujo 

API Endpoint Issue with merged fields

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?

Best Answer chosen by Admin (Salesforce Developers) 
_Prasu__Prasu_

If I am not wrong following is your future method call:

 

  IncidentCouncilUpdater.updateIncident(Inc.Id);
Consider if you have written following future method  public void updateIncident(Id incidentID)
Incident__c myCoordinates = [SELECT latitude__c, longitude__c
                             FROM Incident__c
                             WHERE Id = :incidentID;
Above should be the query

All Answers

_Prasu__Prasu_

String strEndPoint = 'http://www.uk-postcodes.com/latlng/' + fieldname1 + '/' + fieldname2 + '.xml';

req.setEndpoint(strEndPoint );

 

You may not be able to refer them directly as latitude__c,longitude__c

Class must be containing instance of Incident__c object.

 

For ex. incident 

so fieldname1 should be incident.latitude__c and fieldname2 will should be  incident.longitutde__c

Tom MujoTom Mujo

Thanks for coming back so quickly - oddly that didn't work though. 

 

Heres the part of my class...

 

 

  //Create end point from fields in the database
  String StrEndPoint = 'http://www.uk-postcodes.com/latlng/'+Incident__c.latitude__c+','+Incident__c.longitude__c+'.xml';
  System.debug('StrEndPoint: ' + StrEndPoint);
  
    //construct an HTTP request
    HttpRequest req = new HttpRequest();
    req.setHeader('Content-Type', 'text/xml');
    //req.setEndpoint('http://www.uk-postcodes.com/latlng/53.24354,-2.34567.xml');
    req.setEndpoint(strEndPoint);
    req.setMethod('GET');
    req.setTimeout(60000);

 

 

 

 

And here's the debug....

 

 

15:34:59.245|CALLOUT_REQUEST|[21,24]|System.HttpRequest[Endpoint=http://www.uk-postcodes.com/latlng/Latitude__c,Longitude__c.xml, Method=GET]
15:34:59.578|CALLOUT_RESPONSE|[21,24]|System.HttpResponse[Status=Not Found, StatusCode=404]

 

Am I missing something obvious? Should I be querying the record for the data first? 

 

_Prasu__Prasu_

indeed you will require to make a query and bring values for those fields first.

Tom MujoTom Mujo

Oh dear - not my strong point at all.

 

How would I get the right select statement put together?

 

What criteria would I use to pull data from a record that has just set off the trigger?

 

COmplete stab in the dark but is this anywhere near it?

 

 

  Incident__c Lat = [SELECT Incident__c.latitude__c
                    FROM Incident__c
                    WHERE Incident__c(Id);

 

  Incident__c Lat = [SELECT Incident__c.latitude__c                    

                                   FROM Incident__c                    

                                   WHERE Incident__c(Id);

 

I just want the record that has started the trigger - how do I state that in the where clause?

 

Thanks for the swift responses.

JA-DevJA-Dev

I haven't tested this out, but give this a shot:

 

 

Incident__c myCoordinates = [SELECT latitude__c, longitude__c
                             FROM Incident__c
                             WHERE Id = :trigger.new[0].Id];

 

 

And then you reference each coordinate:

 

 

String myLat = myCoordinates.latitude__c;

 

Hope this helps.

 

Tom MujoTom Mujo

Thanks for coming back to me but that select stateemnt produces a null exception on the tigger new{0) section.

 

So frustrating - I feel I am so close and your post above makes sense to call the results from the statement.

 

The code justs seems to think that there is no data in the asved record after insert.

 

Any more thoughts on the select statement?

Tom MujoTom Mujo

Just another thought here. The iD of my newly created record is - a00T0000002jWZZ - this ID is holding my coordinates.

 

However when I debug the ID i get the same reference plus three more characters - a00T0000002jWZZIA2

 

Just wondered if that was forcing my class to search for a record that didn't exist because it was adding three extra characters.

_Prasu__Prasu_

What type of trigger you have written?  I will like to know on which object and its event. I you have written that on Incident then i dont think you will need to make any query.

Tom MujoTom Mujo

This is the trigger..... what do you think?

 

 

 

trigger XMLUpdater on Incident__c (after insert) {

  System.debug('Making future call to update account');
  for (Incident__c Inc : Trigger.New) {
    //Call future method to update account
    //with data from external server.
    //This is a async calls, it returns right away, after
    //enqueuing the request.

    IncidentCouncilUpdater.updateIncident(Inc.Id);
  }

}

 

 

 

 

_Prasu__Prasu_

If I am not wrong following is your future method call:

 

  IncidentCouncilUpdater.updateIncident(Inc.Id);
Consider if you have written following future method  public void updateIncident(Id incidentID)
Incident__c myCoordinates = [SELECT latitude__c, longitude__c
                             FROM Incident__c
                             WHERE Id = :incidentID;
Above should be the query
This was selected as the best answer
Tom MujoTom Mujo

 

 

That works spot on.

 

Thank you very much.

_Prasu__Prasu_

Glad to help you!