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
Arjun y 7Arjun y 7 

Need to hit third party server 20 times from salesforce

Hi All,

Can you please help me how to hit 20 times third party server from salesforce.

My requirement is third party server contains 20000 records. I need to fetch those 20000 records into salesforce and then need to update those 20000 records in salesforce.

We have a third party url which pulls the 1000 records per one transaction. In this manner, we need to hit 20 times to fetch all records. 

Can any one provide me the best approach for this requirement. 
Best Answer chosen by Arjun y 7
Daniel BallingerDaniel Ballinger
One way to make 20 sequental callouts from Apex to an external URL is to use Batch Apex (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm). You can use one that returns Iterable to get 20 iterations. Make sure you use Database.AllowCallouts.
 
public with sharing class CalloutBatchApex implements Database.Batchable<Integer>, Database.AllowCallouts {
    public Iterable<String> start(Database.BatchableContext BC) {
        return new List<Integer> { 1, 2, 3 };
    }

    public void execute(Database.BatchableContext info, List<integer> iteration) {
        // Make the callout to the web service and import the records to Salesforce for the iteration.
    }

    public void finish(Database.BatchableContext info) {}
}

If you need to track state between the executions you may need to make it Stateful as well - Database.Stateful.

All Answers

Temoc MunozTemoc Munoz
Hi Arjun.

1. Do you need the process to be synchronous or asynchronous?
2. I'm assuming you're using REST services, is this correct?
3. Are you doing extra data manipulation when extracting the data from the third party server?

You have several options here:

If the process is asynchronous, you can use BULK API https://developer.salesforce.com/docs/atlas.en-us.api_asynch.meta/api_asynch/
If the process is synchronous, you can use REST or SOAP API. https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/
If you're doing extra manipulation with the extracted data, you can use Apex web services. https://developer.salesforce.com/page/Apex_Web_Services_and_Callouts

Please take a look at this url for more information
https://developer.salesforce.com/docs/atlas.en-us.integration_patterns_and_practices.meta/integration_patterns_and_practices/integ_pat_pat_summary.htm
 
Daniel BallingerDaniel Ballinger
One way to make 20 sequental callouts from Apex to an external URL is to use Batch Apex (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm). You can use one that returns Iterable to get 20 iterations. Make sure you use Database.AllowCallouts.
 
public with sharing class CalloutBatchApex implements Database.Batchable<Integer>, Database.AllowCallouts {
    public Iterable<String> start(Database.BatchableContext BC) {
        return new List<Integer> { 1, 2, 3 };
    }

    public void execute(Database.BatchableContext info, List<integer> iteration) {
        // Make the callout to the web service and import the records to Salesforce for the iteration.
    }

    public void finish(Database.BatchableContext info) {}
}

If you need to track state between the executions you may need to make it Stateful as well - Database.Stateful.
This was selected as the best answer
Arjun y 7Arjun y 7
Hi Daniel,

Thank You for the response. It worked fine for me.

I am stuck with test class for the above scenario. Could you please help me with any sample examples.

Thank You!!