I am trying to call @future
method from trigger and got CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY
Error, because of update operation within the @future
method. Class and triggers given below.
Error Message
Update failed. First exception on row 0 with id 003o0000019gMu6AAE; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, xrx_mf_crm.RepWebserviceCall: execution of AfterUpdate
caused by: System.AsyncException: Future method cannot be called from a future o.
Class
@future(callout=true)
public static void AuthCallout(Set<Id> contactIds) {
List<Contact> ContactUpdate = [SELECT id FROM Contact where Id IN :contactIds];
String ContactID;
HttpRequest req = new HttpRequest();
req.setTimeout(60000);
req.setHeader('Accept','*/*');
req.setHeader('Content-Type','application/json'); // Content Type
req.setMethod('GET');
for (Contact c : ContactUpdate)
{
ContactID = c.id;
req.setEndpoint('https://xxx/xxxx/xxxxx/xxx/xxx-lookup?ContactID=' + ContactID);
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());
JSONParser parser = JSON.createParser(res.getBody());
String GMMID;
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'GCGMM')) {
// Get the value.
parser.nextToken();
// Compute the grand total price for all invoices.
GMMID = parser.gettext();
}
}
//ContactUpdate.IsFutureContext__c = true;
C.Group_ID__c = GMMID;
update c;
}
}
Trigger:
trigger ContactWebserviceCall on Contact (After insert) {
for (Contact co : Trigger.new)
{
If(co.Rep_ID__c.substring(0,3) == 'CRM')
{
ContactWebServiceCall. AuthCallout (Trigger.newMap.keySet());
}
}
}
@future
method anywhere in your provided code. If yourAuthCallout
method is the one with the@future
annotation, you'll also need to specifycallout=true
. Calling a future method inside of a loop is also a bad idea (the governor limit for @future calls is 50 per transaction).@future
callout is simply calling an@future
annotated method. Those are limited to 50 per transaction, and if you have more than 50Contacts
being inserted together you're risking going over that limit. In other words, your trigger is not properly bulkified.