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
cl0s3rcl0s3r 

Apex Trigger

I am missing a processing step regarding Apex Triggers.  I am attempting to create a trigger for the Account object. The logic is validating that the text field TrackerOrgType__c is not null then append the string to the multi select field Organization_Type__c field. Currently the error ,"Apex trigger AccountOrgUpdate caused an unexpected exception, contact your administrator: AccountOrgUpdate: execution of AfterUpdate caused by: System.ListException: List index out of bounds: 1: Trigger.AccountOrgUpdate: line 4, column 28".  I am not sure why the list is out of bounds when I am only working with 1 record. Would anyone like to shed some light on this?

 

trigger AccountOrgUpdate on Account (after insert, after update) {
		//Organization Type
		//TrackerOrgType
		Account ac = trigger.new[1];
		if (ac.TrackerOrgType__c != null){
			ac.Organization_Type__c = ';'+ac.TrackerOrgType__c;
			
		}
}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Use before instead of after in trigger like this (before insert, before update)

 

try this 

 

trigger AccountOrgUpdate on Account (before insert, before update) {
		//Organization Type
		//TrackerOrgType
		Account ac = trigger.new[0];
		if (ac.TrackerOrgType__c != null){
			ac.Organization_Type__c = ';'+ac.TrackerOrgType__c;
			
		}
}

 

 

All Answers

VPrakashVPrakash

Try this,

 

 

trigger AccountOrgUpdate on Account (after insert, after update) {
		//Organization Type
		//TrackerOrgType
		for(Account ac: trigger.new){
		if (ac.TrackerOrgType__c != null){
			ac.Organization_Type__c = ';'+ac.TrackerOrgType__c;
			
		}}
}
Shashikant SharmaShashikant Sharma

Use this instead

 

 

trigger AccountOrgUpdate on Account (after insert, after update) {
		//Organization Type
		//TrackerOrgType
		Account ac = trigger.new[0];
		if (ac.TrackerOrgType__c != null){
			ac.Organization_Type__c = ';'+ac.TrackerOrgType__c;
			
		}
}

  Butthis trigger of your will work only for single record, if you want to write it for bulk use this

 

 

trigger AccountOrgUpdate on Account (after insert, after update) {
		//Organization Type
		//TrackerOrgType
		for(Account ac : trigger.new) {
		if (ac.TrackerOrgType__c != null){
			ac.Organization_Type__c = ';'+ac.TrackerOrgType__c;
                } 
			
		}
}

 

 

 

cl0s3rcl0s3r

Now I am getting the error, "Review all error messages below to correct your data.
Apex trigger AccountOrgUpdate caused an unexpected exception, contact your administrator: AccountOrgUpdate: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.AccountOrgUpdate: line 15, column 4" when I attempt to use

trigger AccountOrgUpdate on Account (after insert, after update) {
		//Organization Type
		//TrackerOrgType
		Account ac = trigger.new[0];
		if (ac.TrackerOrgType__c != null){
			ac.Organization_Type__c = ';'+ac.TrackerOrgType__c;
			
		}
}

 

 

Shashikant SharmaShashikant Sharma

Use before instead of after in trigger like this (before insert, before update)

 

try this 

 

trigger AccountOrgUpdate on Account (before insert, before update) {
		//Organization Type
		//TrackerOrgType
		Account ac = trigger.new[0];
		if (ac.TrackerOrgType__c != null){
			ac.Organization_Type__c = ';'+ac.TrackerOrgType__c;
			
		}
}

 

 

This was selected as the best answer
Shashikant SharmaShashikant Sharma

The reason for this is in after trigger trigger.new is not updatable.

cl0s3rcl0s3r

That makes perfect since, but now I discovered it is overwriting the Organization_Type__c field where as I was attempting to append. Is there a way to accomplish this?

cl0s3rcl0s3r

I found that I can read in the Organization_Type__c into a Stirng variable and then reassign the Organization_Type__c field the variable plus the Tracker_Org_type__c.

trigger AccountOrgUpdate on Account (before insert, before update) {
		//Organization Type
		//TrackerOrgType
		Account ac = trigger.new[0];
		if (ac.TrackerOrgType__c != null){
			String acc = ac.Organization_Type__c;
			ac.Organization_Type__c = acc+';'+ac.TrackerOrgType__c;
			
		}
}