2

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.

8
  • 2
    Taking a quick look, it seems you don't have the harField field in your SOQL. Additionally you may like to verify if the FLS on the field is appropriate and that it can be updated. Commented Nov 14, 2018 at 18:33
  • 1
    I would add that harField does not look like a correct Salesforce Field name. Commented Nov 14, 2018 at 18:40
  • @JayantDas Yes, I do have the harField in my SOQL. Also I do have edit access to that field. Commented Nov 14, 2018 at 19:18
  • 1
    @SebastianKessel Sure, its a custom field. Commented Nov 14, 2018 at 19:20
  • What's the rest of the stack trace after CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY? Commented Nov 14, 2018 at 19:29

1 Answer 1

3

You don't need to use After Insert /Update trigger to update the record in whose context the trigger is run. You can alter those values in Before trigger.

Thus your trigger will be:

trigger customTrigger on Custom_obj__c (before insert, before update) { customTriggerHelper helper = new customTriggerHelper(); if(Trigger.isInsert || Trigger.isUpdate){ if(Trigger.isBefore){ helper.updateROFieldAndName(Trigger.New); } } } 

Also your handler code:

public class customTriggerHelper { public void updateROFieldAndName(List<Custom_obj__c> records){ for(Custom_obj__c rec : List<Custom_obj__c> records){ rec.harField__c = record.Name; if(String.isNotBlank(rec.AutoGenerateField__c) && String.isNotBlank(rec.pickList__c)){ record.harField__c = record.Name ; record.Name = rec.pickList__c.substring(0,4) + '-' + record.AutoGenerateField__c; } } } } 
7
  • This helps me with the error message. But how can I get my two requirements fire with this? 1. Updating the harField__c field. 2. Updating Record Name field with the specific format? Basically I am trying to capture the Record Name field in harField__c prior to this field getting updated to a new specific format. Thoughts? Commented Nov 14, 2018 at 20:16
  • @AustinEvans You wanna store old value of Name in harField__c? Commented Nov 14, 2018 at 20:21
  • Yes, like whenever the user inputs a value in the Record Name field, I am trying to store it in a field and prior to commiting to the database, I am trying to update the Record Name field with a specific format. Commented Nov 14, 2018 at 20:22
  • So if I understand it rightly, User enter Name field as well as harField__c, You populate the Name field in some format and backup the Name field in harField__c field? Commented Nov 14, 2018 at 20:25
  • Use would only enter Name field. Then name field will be formatted to a specific format and the harField will be populated with the old Name field(entered by user). Commented Nov 14, 2018 at 20:28

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.