+ Start a Discussion

Bulk data from dataloader cause callout limit issue

Hi Devs,

I had a requirement to fetch the external data and update back the response in saleforce system based on trigger action.
The design is as follows,
When zipcode field in opportunity is changed or populated ,the trigger calls the external api and passes zipcode value .
The external api returns state,city,country based on zipcode .
The external api just accepts 1 zipcode at a time .
The design is implemented and working fine when opportunity is changed from UI .
Since there could be many opportunity getting updated/updated in bulk  , I have enclosed a HTTP.send(req) in for loop .
If there are more than 100 records getting updated , more that 100 callouts gettting called in single transaction and will throw callout limit exception .

Any better design to avoid the error and functionality works fine even for bulk data.
Manish  ChoudhariManish Choudhari
Hi Sanju,

The current design is not bulkified and you will always hit governer limit when updating bulk records. Instead, you can follow below steps:

1. You can create a checkbox on opportunity, let's say "Update_Address_Required__c"
2. Whenever an opportunity gets inserted or pincode is updated, in your trigger, make this checkbox true.
3. Create a Batch Apex, which will opportunity records having this checkbox as true, and make callout in your batch apex. Once the record is updated from callout result, make this checkbox as false.
4. Schedule this Batch Apex to run every hour or every day, based on your requirement.

This way you can avoid hitting the governor limits.

Also, it can be better if you modify the 3rd party to accept more than 1 pincode at a time.

Let me know if this helps.

**Please mark this as best answer if this answers your query.**

Manish Choudhari
14x Certified Salesforce Architect
Certification link:
My Blog: http://sfdcfacts.com/
Youtube Channel: https://www.youtube.com/SFDCFacts
LinkedIn: https://www.linkedin.com/in/manish-choudhary/
Trailhead: https://trailhead.salesforce.com/en/me/manish-choudhari
Twitter: https://mobile.twitter.com/manish_sfdc