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
Kumar Rao 1Kumar Rao 1 

SOAP Integration - Update failed. First exception on row 0 with id 00Q25000001rN6JEAU; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, DistanceCalc: execution of AfterUpdate

Dear Experts,

I'm struggling to solve SOAP Integration issue, please help me out.

Requirement: in Lead Object, to calculate the vehicle milage from starting postcode to destination postcode I'm integrating salesforce with 3rd party application which takes both post codes and calculated the distance in meters and returns. Then the milage value must be inserted in lead record.

Below is the code, but the error encountering stating can't update the record...can some one please help.

Trigger:
Trigger DistanceCalc on Lead(after update){         
Lead currentRecord = Trigger.old[0];   
DistanceCalc.webServiceCal(currentRecord.id);     
}

Helper Class
global class DistanceCalc {
    
  @future(callout=true)
    public static void webServiceCal(ID leadId){
    
     Lead weblead = [select id,Pick_Up_Post_Code__c,Destination_Postal_Code__c,Distance_miles__c from lead where id=:leadId];
     
     try{
             ServicesPostcodeanywhereCoUk.ArrayForWayPoints wpoint = new ServicesPostcodeanywhereCoUk.ArrayForWayPoints();     
            ServicesPostcodeanywhereCoUk.PostcodeAnywhere_Soap pcode = new ServicesPostcodeanywhereCoUk.PostcodeAnywhere_Soap();
            ServicesPostcodeanywhereCoUk.DistancesAndDirections_Interactive_Distance_v1_00_ArrayOfResults result = pcode.DistancesAndDirections_Interactive_Distance_v1_00('XXXXXXXXXXNT72',weblead.Pick_Up_Post_Code__c,weblead.Destination_Postal_Code__c,wpoint,'Fastest');
       
        Integer meter = result.DistancesAndDirections_Interactive_Distance_v1_00_Results[0].TotalDistance;
        Integer mile = meter/1600;
        weblead.Distance_miles__c = mile;
          update weblead;
          }catch(system.asyncexception e){          
              system.debug(e);
          }    
    }
}

Error Msg:

19:08:42.075 (75862045)|EXECUTION_STARTED 19:08:42.075 (75894489)|CODE_UNIT_STARTED|[EXTERNAL]|FutureHandler - state load 19:08:42.084 (84355771)|CODE_UNIT_FINISHED|FutureHandler - state load 19:08:42.085 (85895156)|EXECUTION_FINISHED 19:08:42.121 (121106934)|EXECUTION_STARTED 19:08:42.121 (121116616)|CODE_UNIT_STARTED|[EXTERNAL]|01p250000009btj|DistanceCalc.webServiceCal 19:08:42.121 (121519240)|HEAP_ALLOCATE|[72]|Bytes:3 19:08:42.121 (121607696)|HEAP_ALLOCATE|[77]|Bytes:152 19:08:42.121 (121633884)|HEAP_ALLOCATE|[342]|Bytes:408 19:08:42.121 (121655810)|HEAP_ALLOCATE|[355]|Bytes:408 19:08:42.121 (121676872)|HEAP_ALLOCATE|[467]|Bytes:48 19:08:42.121 (121712120)|HEAP_ALLOCATE|[139]|Bytes:6 19:08:42.121 (121734214)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:1 19:08:42.121 (121740533)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:2 19:08:42.121 (121759073)|METHOD_ENTRY|[1]|01p250000009btj|DistanceCalc.DistanceCalc() 19:08:42.121 (121764487)|STATEMENT_EXECUTE|[1] 19:08:42.121 (121769588)|STATEMENT_EXECUTE|[1] 19:08:42.121 (121774844)|METHOD_EXIT|[1]|DistanceCalc 19:08:42.121 (121781685)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4 19:08:42.121 (121792289)|VARIABLE_SCOPE_BEGIN|[4]|leadId|Id|false|false 19:08:42.121 (121880156)|VARIABLE_ASSIGNMENT|[4]|leadId|"00Q25000001rN6JEAU" 19:08:42.121 (121896733)|STATEMENT_EXECUTE|[4] 19:08:42.121 (121899444)|STATEMENT_EXECUTE|[6] 19:08:42.121 (121904730)|HEAP_ALLOCATE|[6]|Bytes:108 19:08:42.121 (121917598)|HEAP_ALLOCATE|[6]|Bytes:4 19:08:42.121 (121929301)|HEAP_ALLOCATE|[6]|Bytes:7 19:08:42.121 (121986983)|HEAP_ALLOCATE|[50]|Bytes:5 19:08:42.122 (122018224)|HEAP_ALLOCATE|[56]|Bytes:5 19:08:42.122 (122030120)|HEAP_ALLOCATE|[64]|Bytes:7 19:08:42.122 (122982242)|SOQL_EXECUTE_BEGIN|[6]|Aggregations:0|SELECT id, Pick_Up_Post_Code__c, Destination_Postal_Code__c, Distance_miles__c FROM lead WHERE id = :tmpVar1 19:08:42.128 (128078335)|SOQL_EXECUTE_END|[6]|Rows:1 19:08:42.128 (128104279)|HEAP_ALLOCATE|[6]|Bytes:8 19:08:42.128 (128123657)|HEAP_ALLOCATE|[6]|Bytes:97 19:08:42.128 (128253559)|HEAP_ALLOCATE|[6]|Bytes:8 19:08:42.128 (128266465)|HEAP_ALLOCATE|[6]|Bytes:30 19:08:42.128 (128356443)|HEAP_ALLOCATE|[6]|Bytes:16 19:08:42.128 (128365787)|VARIABLE_SCOPE_BEGIN|[6]|weblead|Lead|true|false 19:08:42.128 (128405376)|VARIABLE_ASSIGNMENT|[6]|weblead|{"s":1,"v":{"Id":"00Q25000001rN6JEAU","Pick_Up_Post_Code__c":"RG215RN","Destination_Postal_C (6 more) ...":"SO140PH"}}|0x359d3954 19:08:42.128 (128412469)|STATEMENT_EXECUTE|[8] 19:08:42.128 (128415233)|STATEMENT_EXECUTE|[8] 19:08:42.128 (128417342)|STATEMENT_EXECUTE|[9] 19:08:42.130 (130272618)|HEAP_ALLOCATE|[9]|Bytes:20 19:08:42.130 (130300242)|CONSTRUCTOR_ENTRY|[9]|01p250000009btZ|<init>() 19:08:42.130 (130346035)|VARIABLE_SCOPE_BEGIN|[10]|this|ServicesPostcodeanywhereCoUk.ArrayForWayPoints|true|false 19:08:42.130 (130421527)|VARIABLE_ASSIGNMENT|[10]|this|{}|0x1ed104d3 19:08:42.130 (130438340)|HEAP_ALLOCATE|[12]|Bytes:6 19:08:42.130 (130456883)|VARIABLE_SCOPE_BEGIN|[2]|this|System.ApexBaseClass|true|false 19:08:42.130 (130474895)|VARIABLE_ASSIGNMENT|[2]|this|{}|0x1ed104d3 19:08:42.130 (130486741)|STATEMENT_EXECUTE|[10] 19:08:42.130 (130489537)|STATEMENT_EXECUTE|[11] 19:08:42.130 (130491759)|STATEMENT_EXECUTE|[12] 19:08:42.130 (130497744)|HEAP_ALLOCATE|[12]|Bytes:4 19:08:42.130 (130540908)|HEAP_ALLOCATE|[12]|Bytes:9 19:08:42.130 (130551103)|HEAP_ALLOCATE|[12]|Bytes:39 19:08:42.130 (130556621)|HEAP_ALLOCATE|[12]|Bytes:1 19:08:42.130 (130560909)|HEAP_ALLOCATE|[12]|Bytes:2 19:08:42.130 (130565120)|HEAP_ALLOCATE|[12]|Bytes:5 19:08:42.130 (130602146)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:28 19:08:42.130 (130623640)|VARIABLE_ASSIGNMENT|[12]|this.WayPoints_type_info|{"s":1,"v":"List of size 6 too large to display"}|0x1ed104d3 19:08:42.130 (130628311)|STATEMENT_EXECUTE|[13] 19:08:42.130 (130633779)|HEAP_ALLOCATE|[13]|Bytes:4 19:08:42.130 (130658227)|HEAP_ALLOCATE|[13]|Bytes:4 19:08:42.130 (130675350)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:16 19:08:42.130 (130692057)|VARIABLE_ASSIGNMENT|[13]|this.apex_schema_type_info|{"s":1,"v":["http://services.post (19 more) ...","true","false"]}|0x1ed104d3 19:08:42.130 (130696879)|STATEMENT_EXECUTE|[14] 19:08:42.130 (130701973)|HEAP_ALLOCATE|[14]|Bytes:4 19:08:42.130 (130734314)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8 19:08:42.130 (130747801)|VARIABLE_ASSIGNMENT|[14]|this.field_order_type_info|{"s":1,"v":["WayPoints"]}|0x1ed104d3 19:08:42.130 (130754773)|CONSTRUCTOR_EXIT|[9]|01p250000009btZ|<init>() 19:08:42.130 (130759172)|VARIABLE_SCOPE_BEGIN|[9]|wpoint|ServicesPostcodeanywhereCoUk.ArrayForWayPoints|true|false 19:08:42.130 (130783554)|VARIABLE_ASSIGNMENT|[9]|wpoint|{"apex_schema_type_inf (1 more) ...":"0x6e97fb34","field_order_type_inf (1 more) ...":"0x4085a95d","WayPoints_type_info":"0x329c23aa"}|0x1ed104d3 19:08:42.130 (130789502)|STATEMENT_EXECUTE|[10] 19:08:42.131 (131097040)|HEAP_ALLOCATE|[10]|Bytes:2 19:08:42.131 (131122455)|HEAP_ALLOCATE|[10]|Bytes:36 19:08:42.131 (131136582)|CONSTRUCTOR_ENTRY|[10]|01p250000009btZ|<init>() 19:08:42.131 (131162459)|VARIABLE_SCOPE_BEGIN|[46]|this|ServicesPostcodeanywhereCoUk.PostcodeAnywhere_Soap|true|false 19:08:42.131 (131198577)|VARIABLE_ASSIGNMENT|[46]|this|{}|0x5b63217 19:08:42.131 (131207563)|VARIABLE_SCOPE_BEGIN|[2]|this|System.ApexBaseClass|true|false 19:08:42.131 (131224149)|VARIABLE_ASSIGNMENT|[2]|this|{}|0x5b63217 19:08:42.131 (131232480)|STATEMENT_EXECUTE|[46] 19:08:42.131 (131235118)|STATEMENT_EXECUTE|[47] 19:08:42.131 (131239198)|HEAP_ALLOCATE|[47]|Bytes:100 19:08:42.131 (131253007)|VARIABLE_ASSIGNMENT|[47]|this.endpoint_x|"https://services.pos (80 more) ..."|0x5b63217 19:08:42.131 (131256842)|STATEMENT_EXECUTE|[48] 19:08:42.131 (131259088)|STATEMENT_EXECUTE|[49] 19:08:42.131 (131261158)|STATEMENT_EXECUTE|[50] 19:08:42.131 (131263115)|STATEMENT_EXECUTE|[51] 19:08:42.131 (131265102)|STATEMENT_EXECUTE|[52] 19:08:42.131 (131267024)|STATEMENT_EXECUTE|[53] 19:08:42.131 (131268918)|STATEMENT_EXECUTE|[54] 19:08:42.131 (131273747)|HEAP_ALLOCATE|[54]|Bytes:4 19:08:42.131 (131313019)|HEAP_ALLOCATE|[54]|Bytes:28 19:08:42.131 (131331907)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:12 19:08:42.131 (131348326)|VARIABLE_ASSIGNMENT|[54]|this.ns_map_type_info|{"s":1,"v":["http://services.post (19 more) ...","ServicesPostcodeanyw (8 more) ..."]}|0x5b63217 19:08:42.131 (131356043)|CONSTRUCTOR_EXIT|[10]|01p250000009btZ|<init>() 19:08:42.131 (131360355)|VARIABLE_SCOPE_BEGIN|[10]|pcode|ServicesPostcodeanywhereCoUk.PostcodeAnywhere_Soap|true|false 19:08:42.131 (131384076)|VARIABLE_ASSIGNMENT|[10]|pcode|{"endpoint_x":"https://services.pos (80 more) ...","ns_map_type_info":"0x54a937ce"}|0x5b63217 19:08:42.131 (131389526)|STATEMENT_EXECUTE|[11] 19:08:42.131 (131394044)|HEAP_ALLOCATE|[11]|Bytes:19 19:08:42.131 (131435470)|HEAP_ALLOCATE|[11]|Bytes:7 19:08:42.131 (131474632)|METHOD_ENTRY|[11]|01p250000009btZ|ServicesPostcodeanywhereCoUk.PostcodeAnywhere_Soap.DistancesAndDirections_Interactive_Distance_v1_00(String, String, String, ServicesPostcodeanywhereCoUk.ArrayForWayPoints, String) 19:08:42.131 (131639283)|VARIABLE_SCOPE_BEGIN|[55]|this|ServicesPostcodeanywhereCoUk.PostcodeAnywhere_Soap|true|false 19:08:42.131 (131667250)|VARIABLE_ASSIGNMENT|[55]|this|{"endpoint_x":"https://services.pos (80 more) ...","ns_map_type_info":"0x54a937ce"}|0x5b63217 19:08:42.131 (131674838)|VARIABLE_SCOPE_BEGIN|[55]|Key|String|false|false 19:08:42.131 (131681738)|VARIABLE_ASSIGNMENT|[55]|Key|"WW94-FK29-RX91-NT72" 19:08:42.131 (131685725)|VARIABLE_SCOPE_BEGIN|[55]|Start|String|false|false 19:08:42.131 (131690955)|VARIABLE_ASSIGNMENT|[55]|Start|"RG215RN" 19:08:42.131 (131694754)|VARIABLE_SCOPE_BEGIN|[55]|Finish|String|false|false 19:08:42.131 (131699872)|VARIABLE_ASSIGNMENT|[55]|Finish|"SO140PH" 19:08:42.131 (131703577)|VARIABLE_SCOPE_BEGIN|[55]|WayPoints|ServicesPostcodeanywhereCoUk.ArrayForWayPoints|true|false 19:08:42.131 (131721554)|VARIABLE_ASSIGNMENT|[55]|WayPoints|{"apex_schema_type_inf (1 more) ...":"0x6e97fb34","field_order_type_inf (1 more) ...":"0x4085a95d","WayPoints_type_info":"0x329c23aa"}|0x1ed104d3 19:08:42.131 (131728081)|VARIABLE_SCOPE_BEGIN|[55]|DistanceType|String|false|false 19:08:42.131 (131734136)|VARIABLE_ASSIGNMENT|[55]|DistanceType|"Fastest" 19:08:42.131 (131739732)|STATEMENT_EXECUTE|[55] 19:08:42.131 (131742157)|STATEMENT_EXECUTE|[56] 19:08:42.131 (131945409)|HEAP_ALLOCATE|[56]|Bytes:52 19:08:42.131 (131962747)|CONSTRUCTOR_ENTRY|[56]|01p250000009btZ|<init>() 19:08:42.131 (131988854)|VARIABLE_SCOPE_BEGIN|[26]|this|ServicesPostcodeanywhereCoUk.DistancesAndDirections_Interactive_Distance_v1_00_element|true|false 19:08:42.132 (132111331)|VARIABLE_ASSIGNMENT|[26]|this|{}|0x60572028 19:08:42.132 (132123310)|VARIABLE_SCOPE_BEGIN|[2]|this|System.ApexBaseClass|true|false 19:08:42.132 (132142781)|VARIABLE_ASSIGNMENT|[2]|this|{}|0x60572028


Trigger.DistanceCalc: line 5, column 1 19:08:42.624 (624939087)|FATAL_ERROR|System.AsyncException: Future method cannot be called from a future or batch method: DistanceCalc.webServiceCal(Id)
 
Best Answer chosen by Kumar Rao 1
James LoghryJames Loghry
You have a case of trigger You have a case of trigger You have a case of trigger You have a case of trigger You have a case of trigger recursion.

You are calling a future method after update of a trigger, where the method then does an update on the lead as well and causes the trigger to re-fire and recall the future method.  Since you cant call a future method from another future method, this throws an exception and causes your code to break.

You'll need to handle the recursion by utilizing static variables (see here for an example: https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US)) or a trigger handler class such as the one found here: https://github.com/kevinohara80/sfdc-trigger-framework

All Answers

scottbcovertscottbcovert
Hi Kumar,

You're running into a recursion issue. After a lead record is updated the web service is called, which updates the lead record, which then calls the web service again. You may want to consider implementing a trigger framework (https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices) to better handle situations like this, but in the meantime you should be able to easily avoid the recursion by using a conditional statement to only call the web service when the Distance_miles__c field is null.

I would also highly recommend reading up on trigger best practices to avoid future headaches. For example, currently your trigger is not bulkified meaning it would not handle data loads and/or mass updates properly as you only call the web service for the first updated lead record.

Best,
Scott
James LoghryJames Loghry
You have a case of trigger You have a case of trigger You have a case of trigger You have a case of trigger You have a case of trigger recursion.

You are calling a future method after update of a trigger, where the method then does an update on the lead as well and causes the trigger to re-fire and recall the future method.  Since you cant call a future method from another future method, this throws an exception and causes your code to break.

You'll need to handle the recursion by utilizing static variables (see here for an example: https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US)) or a trigger handler class such as the one found here: https://github.com/kevinohara80/sfdc-trigger-framework
This was selected as the best answer
Vivek Manchanda 9Vivek Manchanda 9
Hello Kumar,

Did you manage to write the test class for ServicesPostcodeanywhereCoUk api, I am quite stuck with this.

Thanks
Viv