3

I have a general question regarding REST API (either JSON or XML) that I am trying to figure out before I go knee deep. Let's say I save a lead and send a webservices call to an external system that will enrich the lead information, can I be able to get that information back once the processing is done i.e. constantly listen to the external system then save it back to Salesforce? I was under the impression that the external system has to send a call back to salesforce with the information and post it back. Might there be any code out there that my help me with this?

Thanks.

1
  • what is the source for this lead? Do you need to make the call after lead insert? What is meant by enrich do you need to verify this lead info with any external webservices or do you need to update this leads with the new info? – Salesforce Learner Sep 23 '15 at 1:26
1

Your Questions:

  1. Let's say I save a lead and send a webservices call to an external system that will enrich the lead information, can I be able to get that information back once the processing is done i.e. constantly listen to the external system then save it back to Salesforce?

Yes, this is possible from trigger events based on some filter like lead source and checking for the fields that are null which you are trying to enrich are trying once you make a call to the external webservice URL it will return a JSON which you can derserialize and map it to the lead object fields however you need an external Id for the salesforce Id to update your leads.

Lets assume a lead a inserted in salesforce and at this if you wanna make a callout I would suggest to go with the callout trigger.

Let's assume there is Lead Name "Test" for which you need to integrate with Webservice.

POST Method:

You need to build a simple JSON for the webservice :

{Lead Name:"Test"}

GET Method:

You need to genareat a URL in your code like:

www.testwebserviceurl.com/test/

Sample Trigger and class which uses the POST and GET methods to make an webservice callout with the serialization and deserialization of JSON based on the requirement.

Apex trigger:

  trigger leadUpdate on lead (after insert) {
    for (lead ld : Trigger.new) {
    if (any condition) {
    leadIntegrationupdate.leadupdate(ld.Id);
     System.debug('ld.Id');
 }
}

HTTP Class with POST Method:

 global class leadIntegrationupdate{
  @future (callout=true)
   public static void leadupdate(id ldid) {
     //Construct HTTP request and response
     //Http request method,Endpoint and setBody
     HttpRequest req = new HttpRequest();
     req.setHeader('Content-Type','application/json');
     String endpoint = 'Endpoint URL FOR External service';
     req.setMethod('POST');
     req.setEndpoint(endpoint); 
     system.debug('LD ID' +ldid);
     //SOQL to construct JSON string in set body
     Lead led=[select id,Name from Lead where id=:ldid];
     system.debug('serialized JSON'+JSON.serialize(led));
     String JsonString=JSON.serialize(led);
     system.debug(JsonString);
     //Http response method 
     Http http = new Http();
     HTTPResponse res = http.send(req);
     System.debug(res.getBody());

   }
 }
  1. I was under the impression that the external system has to send a call back to salesforce with the information and post it back. Might there be any code out there that my help me with this?

Everytime you post the Lead Name, your webservice can return you the JSON response which you can deserializa and map it to the lead fields. Hope the above code can give you a start and you need to work mostly on getting webservice URL and a sample JSON response for deserialization.

1

We can make callout in following way.

   public class HttpCalloutSample { 
         public String getCalloutResponseContents(String url) {

    // Instantiate a new http object
    Http h = new Http();

     // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
    HttpRequest req = new HttpRequest();
    req.setEndpoint(url);
    req.setMethod('GET');

    // Send the request, and return a response
    HttpResponse res = h.send(req);
    return res.getBody();
  }
}

As you can see from above code when you make callout SFDC listen for response from external service so if you are receiving any response in JSON or XML format it can be accessed using HttpResponse class getBody method.

2
  • Wouldn't the response be like a code, i.e. 200 for success, 4XX for failure? What if the receiving system has to take some time to process the data? Will I have to have another class to constantly call so as to save the data back into salesforce? Regarding the code above, can it handle more that 11 webservices calls? I know there is a limitation to the number of call that can be made via salesforce. – Kenn K Sep 22 '15 at 18:14
  • if you will call res.getStatusCode() method you will get status as well. here are the different methods from this class. developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – Himanshu Sep 22 '15 at 18:16
0

I think you can consider <apex:actionPoller to do this. Basically Actionpoller acts as a timer in visuaflorce page. It is used to send an AJAX request to the server depending on the timeinterval that you specify.

You can put together a solution that uses the where it's action runs a query to determine the status of the webservice output in your controller which is initially called with an @future method call.

2
  • Technically, I won't be using visualforce pages. The idea is to be "sending constant requests" to the external system to see if the records have been processed. If TRUE, I send a post call to write back to salesforce. Do you know if there is an actionPoller for a webservice class not requiring apex? – Kenn K Sep 22 '15 at 19:14
  • Apex does not have a native 'sleep' type of call, so you must do this via a client / VF polling approach. Call the external webservice with @future (callout=true) in your controller and poll the update regularly from VF page. – Saroj Bera Sep 22 '15 at 19:24
0

Explanding on Himanshu's answer, you can create an after insert/update trigger on the Lead object, and have the trigger call a @future annotated method which, in turn, calls a web service at the external system via an HTTP callout. Note that you cannot perform a callout directly from a trigger, only asynchronously via a future method.

If the external system can give you a response in a timely fashion, it can do so in the response to the HTTP callout. The future can then go ahead and enrich the Lead record.

If, on the other hand, the external system takes significant time to do its processing, you could set up a web service, or Apex REST method, at the Salesforce side for the external system to call with the relevant data, including the Lead record id(s), at the appropriate time. In this case, the Apex REST Method would update the Lead record.

SOAP equivalents exist for all of the above mechanisms, but the REST versions tend to be easier to use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.