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
Matt Smith 49Matt Smith 49 

Help Writing a Trigger to Update Fields on the Same Object

Hello everyone,

First post here...  I need some assistance with updating a set of custom fields on the Contact object when one custom field is changed on the Contact object.  When the Degree Program field is updated, I want to use a trigger to update other related fields such as Degree Level, College, School, etc.  Basically,

Controlling field is Application_Program__c, a picklist field.  If Application_Program__c changes to value xxx, change the following fields to:

Picklist_Field_2__c change to aaa
Picklist_Field_3__c change to bbb
Picklist_Field_4__c change to ccc
Picklist_Field_5__c change to ddd
Picklist_Field_6__c change to eee
Picklist_Field_7__c change to fff
Picklist_Field_8__c change to ggg
Picklist_Field_9__c change to hhh
Text_Field_1__c change to abcdefg

I'm not a developer, I don't know Apex, and Process Builder is limiting me here.  I work in Higher Education, and we use the Contact object in Salesforce for a lot.  We have over 200 degree programs we've offered over the years, so I've had to split up that 200 into 10 different Process Builders to try to keep the editing time within Process Builder to a minimum.  Even still, it takes me 3 minutes of system processing time of Salesforce spinning and thinking to make a change in any of these Processes.  I'm concerned not just about the processing time to edit Process Builders, but to have them fire consistently and successfully when its 10 different Process Builders firing simultaneously, looking at the same field, and affecting other automations and operations.

Thanks in advance!

Cheers,
 

Matt

Matt Smith 49Matt Smith 49
Edit: While I don't know Apex, I can pick apart syntaxes pretty well to duplicate chunks of code and adjust the field updates accordingly.  Not asking for someone to write me a full trigger, but rather get me started and I can copy-paste to get all the field updates in there I need.
Suley KaboreSuley Kabore
Hello Matt,
I think the trigger  should look like this one below. But it could be more complex depending on the logic since it should update the other fields based on more that 200 possible values for the Application_Program__c value. Anyway, I guess this could help you get started.
have a good one.

trigger ContTrigger on Contact (before update) {
    //Create a list to hold the records that we are going to update
    List<Contact> contList = new List<Contact>();
    
    //Go through the updated records coming into the database and update their custom fields 
    //according to the updated Application_Program__c value
    for(Contact cont : Trigger.new)
    {
        if(cont. Application_Program__c = 'New Program')
        {
            cont.Picklist_Field_1__c = 'aaa';
            cont.Picklist_Field_2__c = 'bbb';
            cont.Text_Field_1__c = 'ccc';
            
            //Put each updated record in the list we created
            contList.add(cont);
        }
    }
    //Update the list in the database
    update contList;

}
 
Matt Smith 49Matt Smith 49
@Suley, thanks for the quick reply!  I tried the trigger you provided and had to make a few tweaks, line 9 for example requiring a boolean statement.  Here's what I have so far (some specifics put in where I had generic placeholders earlier).  Unfortunately, when I test the trigger, I'm getting the error message:

Error:Apex trigger AppProgFieldContinuity caused an unexpected exception, contact your administrator: AppProgFieldContinuity: execution of BeforeUpdate caused by: System.SObjectException: DML statement cannot operate on trigger.new or trigger.old: ()

Here's the code:

trigger AppProgFieldContinuity on Contact (before insert,before update) {
    //Create a list to hold the records that we are going to update
    List<Contact> contList = new List<Contact>();
    
    //Go through the updated records coming into the database and update their custom fields 
    //according to the updated Application_Program__c value
    for(Contact cont : Trigger.new)
    {
        if(cont.Application_Program__c == 'MA:Psychology')
        {
            cont.Application_Degree_Description__c = 'Master of Arts';
            cont.Application_Degree__c = 'MA';
            cont.Application_Level_Desc__c = 'Graduate';
            cont.Cluster_Name_New_2__c = 'GR-CPSA-SBS';
            cont.Cluster_Name_Old__c = 'GR-CAS-HP';
            cont.College_Old__c = 'College of Arts and Sciences';
            cont.College__c = 'College of Prof Studies & Adv';
            
            //Put each updated record in the list we created
            contList.add(cont);
        }
    }
    //Update the list in the database
    update contList;

}


What does "DML statement cannot operate on trigger.new" mean?
Matt Smith 49Matt Smith 49
I just deleted the line of update contList; and the comment above it and it worked!  Thanks!
Suley KaboreSuley Kabore
@Matt, You are right! There were few mistakes in my code because I didn't run it since I don't have the fields in my org to test it. I  am glad you got it working. Have a good one. Sent from Yahoo Mail on Android
Matt Smith 49Matt Smith 49
I'm getting an error with a value within the custom field of my if statement where the value contains an apostrophe.

if(cont.Application_Program__c == 'Ctf. Graduate Post Master's')

The apostrophe in "Master's" is causing the issue, ending the value of Application_Program__c to early.  How can I resolve this with including an apostrophe in the value for Application_Program__c?
Pankaj Sharma 53Pankaj Sharma 53
Hi,

I don't think we need to create contact list in this trigger actully trigger.new itself a list of contact.

So, In your above code


trigger AppProgFieldContinuity on Contact (before insert,before update) {
    for(Contact cont : Trigger.new)
    {
        if(cont.Application_Program__c == 'MA:Psychology')
        {
            cont.Application_Degree_Description__c = 'Master of Arts';
            cont.Application_Degree__c = 'MA';
            cont.Application_Level_Desc__c = 'Graduate';
            cont.Cluster_Name_New_2__c = 'GR-CPSA-SBS';
            cont.Cluster_Name_Old__c = 'GR-CAS-HP';
            cont.College_Old__c = 'College of Arts and Sciences';
            cont.College__c = 'College of Prof Studies & Adv';

            }
    }
    
}


I think that is sufficient code.
lingareddy kokkulingareddy kokku
I have a picklist feild as city like values are Kadapa Kurnool Hyderabad chittoor. When I leave it picklist value while saving a record it shows error like picklist value is mandatory for a feild. Could you pls advice how to write validation rule for this.
thank you
Barbara MulliganBarbara Mulligan
Well, not the easiest task. I'm glad that you were able to respond so quickly and you solved your problem. I often resort to outside help because I am a very slow learner. Back in college, I used https://writix.co.uk/pay-for-coursework to do homework quickly. I just pay for coursework and didn't care about my grades. It's great that we now live in a world where you can always find help on the Internet.
John Chris 25John Chris 25
It sounds like you are trying to use Process Builder to update multiple fields on the Contact object when the Degree Program field is changed. Unfortunately, Process Builder can be limiting and time-consuming to set up, especially when dealing with many different field updates. One alternative option to consider is using an Apex trigger. An Apex trigger is a piece of code that is executed before or after a record is saved to the database. With an Apex trigger, you can define specific actions to take place when the Degree Program field is updated, such as updating the other related fields you mentioned. If you are not familiar with Apex, you may want to consider reaching out to a developer (https://vanityroofing.ca (https://vanityroofing.ca/)) for assistance in creating the trigger.