1

I'm new to Apex REST/SOAP web services world and I'm struggling with a project of integration. What I have a API documentation from client which contains cURL examples, fields and sample JSON response. My questions,

  1. How should I start and how can I call this API data inside Salesforce? I need ability to pull and push the data into Salesforce.

  2. Are there any changes that need to do in external API system or can I call web services directly in Salesforce if I have login ID and Password of API?

  3. In integration projects like this, what should be first step? What information will I need from client to start the integration?

I have read few articles online on REST but none of the article provide real time examples. Can you Apex Gurus please help? Any help is much appreciated.

1

If you want to make a call to an external API follow this steps buddy:

1) Include the new url as secured in your org. Setup -> Security Controls -> Remote Site Settings and add a new remote site. You must specify a name and a URL. eg. http://yourURL:8087.

Now, Salesforce recognize this site and trust on him.

2( Create your apex class using HttpRequest, this is just an example with a SOAP call:

   public static String SendRequest()
    {
    reqObj=new HttpRequest();
    resObj=new HttpResponse();
    httpRootObj = new Http();
    reqObj.setEndpoint(System.Label.YourEndpoint);
    reqObj.setMethod('GET');
    reqObj.setHeader('SOAPAction','');
    DOM.Document docObj= new DOM.Document();  // You must construct your xml structure according to the API your are invoking
    reqObj.setBody(docObj.toXmlString());
    reqObj.setTimeout(20000); 
    system.debug('XML MADE: '+docObj.toXmlString());
    try 
    {
            resObj= httpRootObj.send(reqObj);
            system.debug('RESPONSE FROM API: '+resObj.getBody());
            Integer iStatusCode=resObj.getStatusCode();
            if(iStatusCode==400 || iStatusCode==401 || iStatusCode==402 || iStatusCode==403 || iStatusCode==404)
            {
                //Send out an email as it failed                      
                return 'failure';
            }
            else
            {return resObj.getStatus();}

    }
    catch(Exception expObj) 
    {
        //your exception logic

        return 'failure';
    }
    return 'OK';
 }

3) At this stage your API must be working, whatever you want to do from here is up to you, remember that you must create your Apex Test class in order to deploy it in Prod.

I hope this small summary helps you.!!!

Welcome to Salesforce :)

  • Thank you Manuelando, this is very helpful. Last question, where is API Id and password is stored in this code? how will the API system know that I want to make a callout? Do I need ID and Password for API system to make this work? – Tom Simmons Aug 17 '16 at 10:50
  • That depends on the other API you want to use, but normally you can have that in the header of the document (DOM.Document docObj= new DOM.Document();) That's where the logic of your call resides. Just have a look to an example of a Dom.document and use it according to your needs. If this solve your question please marked it as a best answer. – MANUELAN00 Aug 17 '16 at 22:55
  • Answering your question, This is all you need from the Salesforce API to make a call...If the other API requires something else just adjust the service to your needs. – MANUELAN00 Aug 17 '16 at 22:58
  • Thank you very much Manuelando, you are awesome (no wonder you are a Barcelona fan :) – Tom Simmons Aug 18 '16 at 17:48
  • Hahaha...that's a good one. Happy to help you anytime...!!! – MANUELAN00 Aug 18 '16 at 23:28
-2

Here is a simple example:

// req.setBody('This is a request');
// req.setEndpoint('https://WXYZ.com');         
// HttpResponse res = new HttpResponse(); 

HTH.

  • 1
    This really doesn't answer the questions posed by Tom and doesn't include enough information to be of use. Can you expand it a little? – Matt Lacey Aug 16 '16 at 0:17

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.