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
impalacrazyimpalacrazy 

trigger firing one all contact changes

So i have a trigger that takes the information from one title field and pushes it into another. It was working great but we noticed that the following was taking place (here is the example):

 

1. candidate is recruited and title is given in short_title__c  (PA) field

2. record is changed over to credentialing and short_title__c (PA) field is pushed into title__c (PA) field

3. user goes into credentialing record for contact and makes change to title__c (now says PA-C) and any field (last name, address, etc and saves)

4. title__c (PA) and other changes saved

 

So what i was trying to acomplish was that no matter what changes anyone makes to the title or any other field on the contact record that title__c is not over written with what is in short_title__c field. Below is the code

 

trigger setTitle on Contact (before update)
{
for(Contact c: trigger.new)

if(c.Title_Short_List_Job_Board__c != null && c.Title_Short_List_Job_Board__c != trigger.oldMap.get(c.id).Title__c)
{
c.Title__c = c.Title_Short_List_Job_Board__c;
}
}
Best Answer chosen by Admin (Salesforce Developers) 
Vinit_KumarVinit_Kumar

Hi,

 

Please try below :-

 

trigger setTitle on Contact (before insert,before update)
{
if(Trigger.IsInsert){
for(Contact c: trigger.new)

if(c.Title_Short_List_Job_Board__c != null && c.Title_Short_List_Job_Board__c != trigger.oldMap.get(c.id).Title__c)
{
c.Title__c = c.Title_Short_List_Job_Board__c;
}
}
if(Trigger.IsUpdate){
for(Contact c: trigger.new)

if(c.Title_Short_Li st_Job_Board__c != null && c.Title_Short_List_Job_Board__c != trigger.oldMap.get(c.id).Title__c && c.Title__c==trigger.oldMap.get(c.id).Title__c)
{
c.Title__c = c.Title_Short_List_Job_Board__c;
}
}
}

All Answers

Vinit_KumarVinit_Kumar

Are you getting any error or what is is the issue.

impalacrazyimpalacrazy

not getting any error messages. the problem is that when a user makes a change the vaule in the title__c field is over written by what is in the short_title__c field. So if i have or change title__c to PAC and hit save because short_title__c has PA as its value it changes title__c back to PA

Vinit_KumarVinit_Kumar

What I can see in the trigger is that you are updating the value for Title__c 

 

c.Title__c = c.Title_Short_List_Job_Board__c;

 

So,isn't this expected?

impalacrazyimpalacrazy

yes that is expected. But now if the title__c field is changed when you hit save the short_title__c field pushes the old value back into the title__c field.

 

The title__c field is used by the credentialing users on the contact and short_title__c is used by the recuriting users on teh contact. so the recruiter starts the process with a simple tile lets say NP. when the record type is changed over to credentialing the short title field value is being pushed over to the title__c field.

 

Now recruiting does no more with the title but credentialing team does. so they go in and change the title to CFNP and hit save. but the issue is that when they do the trigger is pushing short_title__c value back in.

 

i guess i need to figure out how to make the trigger only fire on a when record is created  so that it only fires once and not on every update.

Vinit_KumarVinit_Kumar

Hi,

 

Please try below :-

 

trigger setTitle on Contact (before insert,before update)
{
if(Trigger.IsInsert){
for(Contact c: trigger.new)

if(c.Title_Short_List_Job_Board__c != null && c.Title_Short_List_Job_Board__c != trigger.oldMap.get(c.id).Title__c)
{
c.Title__c = c.Title_Short_List_Job_Board__c;
}
}
if(Trigger.IsUpdate){
for(Contact c: trigger.new)

if(c.Title_Short_Li st_Job_Board__c != null && c.Title_Short_List_Job_Board__c != trigger.oldMap.get(c.id).Title__c && c.Title__c==trigger.oldMap.get(c.id).Title__c)
{
c.Title__c = c.Title_Short_List_Job_Board__c;
}
}
}

This was selected as the best answer
impalacrazyimpalacrazy

Thanks this did the trick.