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
smita bhargavasmita bhargava 

Update multiple records in single rest api call

Hello
I am using rest api to update multiple records between two sfdc developer orgnzs using apex code.

Number of records to be updated is less than 50000.

Here is the approach I am following:
1. perform an HTTPGet to retrieve the records
2. Then perform HTTPPUT to update the records in destination.

HDFC is the destination orgnz.
@HTTPGET
webservice static List<Account> SendMultipleRecordsToSource()  
{
   List<Account> accList=[SELECT Id,Name,Phone FROM Account where Name LIKE '%LockHeed Martin%'];
   return accList;
}

@HTTPPUT
webservice static List<Account> UpdateMultipleAccounts()  
{
    RestRequest req=RestContext.Request;
    
    RestResponse res=RestContext.Response;
    
    string req_body=req.RequestBody.ToString();
    
    List<Account> FromSourceList;
        
    if (req_body != NULL)
    {
            
       FromSourceList=(List<Account>)JSOn.Deserialize(req_body,List<Account>.class);
            
       if (FromSourceList.size() > 0)
       {
         Upsert(FromSourceList);
       }
    }
    return FromSourceList;
}
Flipkart is the source orgnz
public PageReference FetchMultipleRecordsForUpdate()
     {
       if (accesstoken != NULL)  
       {
          Http h = new Http();
          
          HttpRequest req = new HttpRequest();
        
          req.setMethod('GET'); 
        
          req.setHeader('Authorization','Bearer '+Helper.GetAccessToken().access_token);
        
          req.setHeader('Content-Type','application/json; charset=UTF-8'); 
        
          req.setHeader('Accept','application/json');
        
          req.setEndpoint('https://ap2.salesforce.com/services/apexrest/AccountRest');   
            
          HttpResponse res;
          
          try
          {
           res=h.send(req);
           
          }   
          catch(System.CallOutException ex)
          {
            system.debug('--callout error:--'+ex);
          }         
           
          accList = (List<Account>)JSON.deserialize(res.getBody(),List<Account>.class);
            
          List<Account> newList=new List<Account>();
         
 for(Account a:accList)
          {
            a.Name='LockHeed Martin'+generateRandomString(2);
            a.Phone='3443244324';
            
            newList.add(a);
          }          
          accList=new List<Account>(newList);
          
          system.debug('--Final accList --'+accList );
        }  
         return null;
     }

a) Is it possible to perform a single rest api call for the above scenario? If yes please let me know.

b) If more than 50K records then what approach do we use?

smita




 
Om PrakashOm Prakash
Hi Smita,
1. Yes. It is possible by sending one additional parameter during your api call and update the logic in destination org.
if actionParam = 'getRecordOnly', then skip the update logic, if action='updateRecord' update logic will execute.
But your approach is good to use HTTPGET method for retrieve the records and HTTPPUT for upsert.

2. If more than 50K records then you need to go in asynchronous way.
Basic Approach:
Scheduled job will be in both Salesforce org who will be execute on different time interval.
When Api of destination org will be call for update the records then you need to execute a batch class and in response just return simple message like "request is queued for processing" instaed of updated records as response.
Batch job will update your data in destination org asynchronously.
Separate scheduled job which is running on particular time, will push the updated data in source org.

We need to keep all governor limits in mind while implementation.
This approach is suggested after keeping "Callout loop not allowed" in mind.
https://help.salesforce.com/articleView?id=000232321&type=1
smita bhargavasmita bhargava
Hi
Let me rephrase my question.
I need to update multiple records of destination. so the basic approach is to fetch the records from destionation into source, insert new values
and then send request back to destination wherein a DML would be performed in destination.

so in previous post in destinaiton side, I first created a HTTPGet and then performed and HTTPPUT in destination, so two webservice api calls

My question is can we handle this in a single api call?
In real time how is this implemented?

smita
Om PrakashOm Prakash
Hi, During API call of destination org send new values in request body as JSON from source org. On destination perform upsert operation with new field value mapping. In response return the updated records from destination to source org then upsert the records in source. I think it would work in one API call, same as what you have implemented currently by two api call.(Less than 50k)