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
RajnisfRajnisf 

REST API callout understanding

Hi,
i have to integrate salesforce with 3rd party which provides rest apis endpoint. We need to implement everything in the salesforce end (coding etc)
My requirement is : i have to sync data from 3rd party object obj1 to salesforce Accounts.
for example:
3rd party fields                 Salesforce fields
obj1.name                =        account name
obj1.id                     =         objid

Client wants to first implement the syncing between these objects from 3rd party to salesforce
then the automation process.. (for example : once obj1 record created in 3rd party it comes to salesforce as a new account)

My question is ...can we do the callout without any trigger in salesforce.
Please help
Thanks 
 
VamsiVamsi

Yes, you can perform callouts using @future methods,batch apex,queueable apex.. and then schedule these class to run at specific times using schedulable interface
RajnisfRajnisf
public class projectrequestnew {
     
  
    public void getCalloutResponseContents() {
        String result = null;  
    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('endpointurl');
    req.setMethod('GET');

    HttpResponse res = h.send(req);
        if(res.getStatusCode() == 200){
            result = res.getBody();
        }
    //return result;
  }

 
}
 
global with sharing class SchedulerForApex implements Schedulable, Database.AllowsCallouts

{
    global void execute(SchedulableContext sc)        

  {
      projectrequestnew b = new projectrequestnew();
       b.getCalloutResponseContents();
  }

Public static void SchedulerMethod() 

    {
    string con_exp='0 0 1 * * ?';

    System.schedule('projectrequestnewTest', con_exp, new SchedulerForApex());
                                     
    }   

}

Thanks for replying........here is the code...
VamsiVamsi
Hi,

Annotate the method getCalloutResponseContents() with @future(callout=true) as follows 
 
public class projectrequestnew {
     
    @future(callout=true)
    public void getCalloutResponseContents() {
        String result = null;  
    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('endpointurl');
    req.setMethod('GET');

    HttpResponse res = h.send(req);
        if(res.getStatusCode() == 200){
            result = res.getBody();
        }
    //return result;
  }

 
}

 
RajnisfRajnisf
Error: Compile Error: Static method cannot be referenced from a non static context: void projectrequestnew.getCalloutResponseContents(
VamsiVamsi
Sorry, didn't notice that method don't have a static keyword. Please use static keyword with that method. Methods with the future annotation must be static methods, and can only return a void type. The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types. Methods with the futureannotation cannot take sObjects or objects as arguments. Virus-free. www.avast.com
RajnisfRajnisf
Thanks a lot...
can u help to save the third party data to sf account object... i knw how to display the data in vf page.....bt instead i want to save the data in account object...
public class projectrequestnew {
   public List<wraptt> llst{get;set;}
   
   @future(callout=true)
    public static void getCalloutResponseContents() {
        String result = null;  
    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('endpoint');
    req.setMethod('GET');

    HttpResponse res = h.send(req);
        if(res.getStatusCode() == 200){
            result = res.getBody();
        }
         llst = (List<wraptt>)JSON.deserialize(res.getbody(),List<wraptt>.class);
    //return result;
  }
  
   public class wraptt{ 
        public String schedule{get;set;}
        public integer id{get;set;}
        public String message{get;set;}
        public String data{get;set;}
                  public wraptt(String  u, integer i, string t, string b, string p){                   
                   schedule = u;
                   id = i;
                   message = t;
                   data = p;                  
                   }
        }
 
}


 
VamsiVamsi
Hi,

You need to map the response data based on the fields existing on account object. Make sure with the field data types while mapping . I mean update Account record if it already exists or create a new account based on resposne data also make sure to capture the 3rd party record id on Account. So that it would be helpful to identily uniqueness for updates ...!!!