Use-case: Trying to update a text field after an insert. Also trying to get the Record Name field(text field) to a specific format using another auto generated field and a picklist field.
Simple trigger:
trigger customTrigger on Custom_obj__c (after insert, after update) { customTriggerHelper helper = new customTriggerHelper(); if(Trigger.isInsert){ if(Trigger.isAfter){ helper.updateROField(Trigger.New); } } if(Trigger.isAfter){ if(Trigger.isUpdate){ helper.updateNameField(Trigger.New); } } } Helper class:
public class customTriggerHelper { public void updateROField(List<Custom_obj__c> records){ //List<Custom_obj__c> toUpdateList = new List<Custom_obj__c>(); Custom_obj__c record = [Select Id, Name, harField__c From Custom_obj__c Where ID In : records Order By CreatedDate Desc LIMIT 1]; if(record.harField__c == NULL){ System.debug('Inside updateROField method'); record.harField__c = record.Name; update record; } } public void updateNameField(List<Custom_obj__c> records){ //List<Custom_obj__c> toUpdateList = new List<Custom_obj__c>(); Custom_obj__c record = [Select Id, Name, AutoGenerateField__c, harField__c, pickList__c From Custom_obj__c Where ID In : records Order By CreatedDate Desc LIMIT 1]; if(record.harField__c != '' && record.AutoGenerateField__c != '' && record.pickList__c != ''){ System.debug('Inside updateNameField method'); String picklistVal = record.pickList__c; String firstFourChars = picklistVal.substring(0,4); String concat = firstFourChars + ' - ' + record.AutoGenerateField__c; record.Name = concat; update record; } } } Error:
execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id a09f4000006fXAfWWe; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, customTrigger: maximum trigger depth exceeded Custom_obj__c
What am I doing wrong here? Looks pretty simple and straightforward, but I am unable to move forward.
harFieldfield in your SOQL. Additionally you may like to verify if the FLS on the field is appropriate and that it can be updated.harFielddoes not look like a correct Salesforce Field name.harFieldin my SOQL. Also I do have edit access to that field.CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY?