You need to sign in to do that
Don't have an account?
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
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;
}
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?
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?
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.
thank you