I am connecting to external system when i update the existing records in sfdc,automatically trigger call the webservice and get data from external system and pplaced in salesforce field but it showing an error
Update failed. First exception on row 0 with id a1Uf0032323234; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, qlineUpdater: execution of AfterUpdate
caused by: System.AsyncException: Future method cannot be called from a future or batch method:
trigger qlineUpdater on Quote_Item__c (after update) {
for (Quote_Item__c ql : Trigger.old) {
QuolineUpdater.updateQline(ql.Id, ql.Name);
}
}
Apex Class
public class QuolineUpdater {
//Future annotation to mark the method as async.
@Future(callout=true)
public static void updateQline(String id, String name) {
//construct an HTTP request
HttpRequest req = new HttpRequest();
req.setEndpoint('http://www.xxxxxxxxx.com/salesforceApi/salesforce.txt');
req.setMethod('GET');
//send the request
Http http = new Http();
HttpResponse res = http.send(req);
//check the response
if (res.getStatusCode() == 200) {
Quote_Item__c ql = new Quote_Item__c (Id=id);
ql.Comment_On_Product__c = res.getBody();
update ql;
} else {
System.debug('Callout failed: ' + res);
}
}
}
Attribution to: Sathya
Possible Suggestion/Solution #1
Did you make an attempt to understand the error message ? It is self-explanitory.
caused by: System.AsyncException: Future method cannot be called from a future or batch method:
Your design does this:
Trigger -> Do async call out
async call out-> call trigger
This would result in a recursive, never ending operation if you do not build-in conditions to regulate when what needs to be done.
update: You need to consider the following: Under what conditions do you want that trigger to call the external system ? Always ? Only if specific fields are updated ? I expect it should only call the external system has not been done before, so that is what you need to check first in your code.
Additionally, your current implementation will make a call out per every record updated, I would advise you, if possible to attempt to do the call out once for all records in your current trigger scope.
Attribution to: Samuel De Rycke
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/34664