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
test_commtest_comm 

Apex code problem

Why does my code seem to be looping? I can't figure this one out...

All I'm looking to do is update an account record when the rep saves the record. So I'm looking to read from the same record and update the other fields on that record. Thanks for any help on this one.

 

Here's my code:

trigger RE_TRIGGER on Account (after update) {
	
	Account acc = new Account();
	acc = [Select Id FROM Account WHERE PersonEmail = 'roy.rogers@test.com'];
	//System.debug(acc.auto_policies__c );
	Integer counter = 0;
	Account a = new Account();
	Rules_Table__c rt = new Rules_Table__c();
	if(counter == 0){
		if(acc.Id != Null){
			a = [SELECT auto_policies__c FROM Account WHERE id=:acc.Id];
			rt = [SELECT auto_policy_multiplier__c FROM Rules_Table__c WHERE Name ='Customer Score Factors' ];
			System.Debug(a);
			System.Debug(rt);
			acc.Auto_Score__c = a.Auto_Policies__c + rt.Auto_Policy_Multiplier__c;
			counter++;
		}
		update acc;
		System.Debug('This is the updated Account Record Value: ' + acc);
		
	}

TEST CLASS:
@isTest
private class TestTrigger {

    static testMethod void myUnitTest() {
        
        Account a = [SELECT Id, LastName, Auto_Policies__c, Home_Policies__c, PEL_Policies__c FROM Account where PersonEmail = 'roy.rogers@test.com'];
     	
     	
    } 
}

Here are my errors:

Debug Log:

20.0 APEX_CODE,FINER;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;VALIDATION,INFO;WORKFLOW,INFO
11:41:15.415 (415243000)|EXECUTION_STARTED
11:41:15.415 (415339000)|CODE_UNIT_STARTED|[EXTERNAL]|01pE0000000HCZ0|TestTrigger.myUnitTest
11:41:15.415 (415433000)|METHOD_ENTRY|[2]|01pE0000000HCZ0|TestTrigger.TestTrigger()
11:41:15.415 (415478000)|STATEMENT_EXECUTE|[2]
11:41:15.415 (415523000)|STATEMENT_EXECUTE|[2]
11:41:15.415 (415566000)|METHOD_EXIT|[2]|TestTrigger
11:41:15.415 (415617000)|STATEMENT_EXECUTE|[4]
11:41:15.415 (415651000)|STATEMENT_EXECUTE|[6]
11:41:15.415 (415935000)|SOQL_EXECUTE_BEGIN|[6]|Aggregations:0|SELECT Id, LastName, Auto_Policies__c, Home_Policies__c, PEL_Policies__c FROM Account where PersonEmail = 'roy.rogers@test.com'
11:41:15.440 (440743000)|SOQL_EXECUTE_END|[6]|Rows:1
11:41:15.440 (440819000)|HEAP_ALLOCATE|[6]|Bytes:8
11:41:15.440 (440978000)|HEAP_ALLOCATE|[6]|Bytes:217
11:41:15.441 (441058000)|STATEMENT_EXECUTE|[8]
11:41:15.441 (441280000)|STATEMENT_EXECUTE|[9]
11:41:15.441 (441449000)|STATEMENT_EXECUTE|[10]
11:41:15.441 (441619000)|STATEMENT_EXECUTE|[11]
11:41:15.441 (441697000)|HEAP_ALLOCATE|[11]|Bytes:8
11:41:15.441 (441745000)|DML_BEGIN|[11]|Op:Update|Type:Account|Rows:1
11:41:15.441 (441944000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
11:41:15.513 (513114000)|CODE_UNIT_STARTED|[EXTERNAL]|01qE0000000CfKa|RE_TRIGGER on PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
11:41:15.513 (513238000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
11:41:15.513 (513288000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
11:41:15.513 (513393000)|STATEMENT_EXECUTE|[8]
11:41:15.513 (513436000)|STATEMENT_EXECUTE|[10]
11:41:15.513 (513506000)|HEAP_ALLOCATE|[10]|Bytes:0
11:41:15.513 (513578000)|STATEMENT_EXECUTE|[11]
11:41:15.513 (513742000)|SOQL_EXECUTE_BEGIN|[11]|Aggregations:0|Select Id FROM Account WHERE PersonEmail = 'roy.rogers@test.com'
11:41:15.523 (523588000)|SOQL_EXECUTE_END|[11]|Rows:1
11:41:15.523 (523664000)|HEAP_ALLOCATE|[11]|Bytes:8
11:41:15.523 (523715000)|HEAP_ALLOCATE|[11]|Bytes:32
11:41:15.523 (523793000)|STATEMENT_EXECUTE|[13]
11:41:15.523 (523862000)|STATEMENT_EXECUTE|[14]
11:41:15.523 (523918000)|HEAP_ALLOCATE|[14]|Bytes:0
11:41:15.523 (523972000)|STATEMENT_EXECUTE|[15]
11:41:15.524 (524028000)|HEAP_ALLOCATE|[15]|Bytes:0
11:41:15.524 (524094000)|STATEMENT_EXECUTE|[16]
11:41:15.524 (524180000)|STATEMENT_EXECUTE|[16]
11:41:15.524 (524219000)|STATEMENT_EXECUTE|[17]
11:41:15.524 (524387000)|STATEMENT_EXECUTE|[17]
11:41:15.524 (524429000)|STATEMENT_EXECUTE|[18]
11:41:15.524 (524561000)|SOQL_EXECUTE_BEGIN|[18]|Aggregations:0|SELECT auto_policies__c FROM Account WHERE id=:acc.Id
11:41:15.532 (532122000)|SOQL_EXECUTE_END|[18]|Rows:1
11:41:15.532 (532214000)|HEAP_ALLOCATE|[18]|Bytes:8
11:41:15.532 (532271000)|HEAP_ALLOCATE|[18]|Bytes:88
11:41:15.532 (532360000)|STATEMENT_EXECUTE|[19]
11:41:15.532 (532538000)|SOQL_EXECUTE_BEGIN|[19]|Aggregations:0|SELECT auto_policy_multiplier__c FROM Rules_Table__c WHERE Name ='Customer Score Factors' 
11:41:15.538 (538252000)|SOQL_EXECUTE_END|[19]|Rows:1
11:41:15.538 (538329000)|HEAP_ALLOCATE|[19]|Bytes:8
11:41:15.538 (538413000)|HEAP_ALLOCATE|[19]|Bytes:97
11:41:15.538 (538490000)|STATEMENT_EXECUTE|[20]
11:41:15.538 (538697000)|USER_DEBUG|[20]|DEBUG|Account:{Id=001E000000E5wJ8IAJ, Auto_Policies__c=10}
11:41:15.538 (538765000)|STATEMENT_EXECUTE|[21]
11:41:15.538 (538953000)|USER_DEBUG|[21]|DEBUG|Rules_Table__c:{Auto_Policy_Multiplier__c=1.50, Id=a05E0000000W0xJIAS}
11:41:15.539 (539019000)|STATEMENT_EXECUTE|[22]
11:41:15.539 (539308000)|HEAP_ALLOCATE|[22]|Bytes:-4
11:41:15.539 (539363000)|STATEMENT_EXECUTE|[23]
11:41:15.539 (539443000)|STATEMENT_EXECUTE|[25]
11:41:15.539 (539520000)|HEAP_ALLOCATE|[25]|Bytes:8
11:41:15.539 (539584000)|DML_BEGIN|[25]|Op:Update|Type:Account|Rows:1
11:41:15.539 (539750000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
11:41:15.578 (578513000)|CODE_UNIT_STARTED|[EXTERNAL]|01qE0000000CfKa|RE_TRIGGER on PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
11:41:15.578 (578620000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
11:41:15.578 (578667000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
11:41:15.578 (578767000)|STATEMENT_EXECUTE|[8]
11:41:15.578 (578809000)|STATEMENT_EXECUTE|[10]
11:41:15.578 (578872000)|HEAP_ALLOCATE|[10]|Bytes:0
11:41:15.578 (578933000)|STATEMENT_EXECUTE|[11]
11:41:15.579 (579081000)|SOQL_EXECUTE_BEGIN|[11]|Aggregations:0|Select Id FROM Account WHERE PersonEmail = 'roy.rogers@test.com'
11:41:15.586 (586761000)|SOQL_EXECUTE_END|[11]|Rows:1
11:41:15.586 (586829000)|HEAP_ALLOCATE|[11]|Bytes:8
11:41:15.586 (586877000)|HEAP_ALLOCATE|[11]|Bytes:32
11:41:15.586 (586947000)|STATEMENT_EXECUTE|[13]
11:41:15.587 (587012000)|STATEMENT_EXECUTE|[14]
11:41:15.587 (587077000)|HEAP_ALLOCATE|[14]|Bytes:0
11:41:15.587 (587135000)|STATEMENT_EXECUTE|[15]
11:41:15.587 (587196000)|HEAP_ALLOCATE|[15]|Bytes:0
11:41:15.587 (587250000)|STATEMENT_EXECUTE|[16]
11:41:15.587 (587323000)|STATEMENT_EXECUTE|[16]
11:41:15.587 (587358000)|STATEMENT_EXECUTE|[17]
11:41:15.587 (587442000)|STATEMENT_EXECUTE|[17]
11:41:15.587 (587477000)|STATEMENT_EXECUTE|[18]
11:41:15.587 (587614000)|SOQL_EXECUTE_BEGIN|[18]|Aggregations:0|SELECT auto_policies__c FROM Account WHERE id=:acc.Id


11:41:16.477 (1477650000)|CODE_UNIT_FINISHED|RE_TRIGGER on PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
11:41:16.478 (1478850000)|FATAL_ERROR|Internal Salesforce.com Error
11:41:16.784 (1478921000)|CUMULATIVE_LIMIT_USAGE
11:41:16.784|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 28 out of 100
  Number of query rows: 28 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 10 out of 150
  Number of DML rows: 10 out of 10000
  Number of script statements: 113 out of 200000
  Maximum heap size: 0 out of 3000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

11:41:16.784 (1478921000)|CUMULATIVE_LIMIT_USAGE_END

11:41:16.794 (1488198000)|CUMULATIVE_LIMIT_USAGE_END

11:41:16.488 (1488304000)|CODE_UNIT_FINISHED|RE_TRIGGER on PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
11:41:16.488 (1488596000)|DML_END|[25]
11:41:16.488 (1488772000)|EXCEPTION_THROWN|[25]|System.DmlException: Update failed. First exception on row 0 with id 001E000000E5wJ8IAJ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, RE_TRIGGER: maximum trigger depth exceeded
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]: []
11:41:16.489 (1489726000)|FATAL_ERROR|System.DmlException: Update failed. First exception on row 0 with id 001E000000E5wJ8IAJ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, RE_TRIGGER: maximum trigger depth exceeded
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]: []

Trigger.RE_TRIGGER: line 25, column 3
11:41:16.795 (1489802000)|CUMULATIVE_LIMIT_USAGE
11:41:16.795|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 4 out of 100
  Number of query rows: 4 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 2 out of 150
  Number of DML rows: 2 out of 10000
  Number of script statements: 17 out of 200000
  Maximum heap size: 0 out of 3000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

11:41:16.795 (1489802000)|CUMULATIVE_LIMIT_USAGE_END

11:41:16.489 (1489909000)|CODE_UNIT_FINISHED|RE_TRIGGER on PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
11:41:16.490 (1490961000)|DML_END|[11]
11:41:16.491 (1491123000)|EXCEPTION_THROWN|[11]|System.DmlException: Update failed. First exception on row 0 with id 001E000000E5wJ8IAJ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, RE_TRIGGER: execution of AfterUpdate

caused by: System.DmlException: Update failed. First exception on row 0 with id 001E000000E5wJ8IAJ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, RE_TRIGGER: maximum trigger depth exceeded
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]: []

Trigger.RE_TRIGGER: line 25, column 3: []
11:41:16.493 (1493848000)|FATAL_ERROR|System.DmlException: Update failed. First exception on row 0 with id 001E000000E5wJ8IAJ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, RE_TRIGGER: execution of AfterUpdate

caused by: System.DmlException: Update failed. First exception on row 0 with id 001E000000E5wJ8IAJ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, RE_TRIGGER: maximum trigger depth exceeded
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]
PersonAccount trigger event AfterUpdate for [001E000000E5wJ8]: []

Trigger.RE_TRIGGER: line 25, column 3: []

Class.TestTrigger.myUnitTest: line 11, column 9
External entry point
11:41:16.799 (1493935000)|CUMULATIVE_LIMIT_USAGE
11:41:16.799|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 1 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Number of script statements: 5 out of 200000
  Maximum heap size: 0 out of 3000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

11:41:16.799 (1493935000)|CUMULATIVE_LIMIT_USAGE_END

11:41:16.494 (1494004000)|CODE_UNIT_FINISHED|TestTrigger.myUnitTest
11:41:16.494 (1494040000)|EXECUTION_FINISHED

 

Jake GmerekJake Gmerek

You are updating an account from within an account trigger.  When you update that account it is calling that trigger again, but the first iteration of the trigger is still running.  This happens again and again until you get that trigger depth error.

 

The general way to work around this is to put your counter check (ie your first if statement) in a static variable that will control it.  Look here:

 

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_classes_static.htm?SearchType=Stem

 

for a better idea of how to do that.

CaptainObviousCaptainObvious

Assuming that Auto Score changes only when certain criteria is met...


Consider having the trigger update the account only when a change in Auto Score is detected (use Trigger.old and Trigger.new to compare the values).

 

When Auto Score changes and the rep saves the record, the trigger will execute and update the account. This will 'trigger' another execution of the trigger, but since the updated Auto Score will now remain unchanged, the account will not be updated- preventing further execution of the trigger.