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
Jyosi jyosiJyosi jyosi 

Trigger to Copy the old value from one field to another field

Hello Everyone,

i need to copy the old values(Look up field into another field ).
Here whenever i save the record its getting the new id value in another field.
I need to excute this part of code whenver this feilds changes QCC_DSP__c/QAM_DSP__c
Suppose if the field changes happens on any other field is part of code should not execute.


 public void onManagerUpdate(List<Contact> contactList) {
     set<Id> CntIds= new Set<Id>();
     for (Contact Mancontacts : contactList){
        if (Mancontacts.QCC_DSP__c != null || Mancontacts.QAM_DSP__c!=null) 
        {
            Mancontacts.Manager_Login__c = Mancontacts.QCC_DSP__c;
            Mancontacts.Manager_Login__c = Mancontacts.QAM_DSP__c;
            Mancontacts.QAM_DSP_End_Date__c=system.today();
            
        }
      }
       //update ManagerUpdate;
  }

Thanks for all the help.

Regards,
Jyo
Best Answer chosen by Jyosi jyosi
KaranrajKaranraj
Try the below code,
trigger fieldChanged on Contact(before update){
	for(Contact con: Trigger.New){
		if(con.QCC_DSP__c != Trigger.oldMap.get(con.Id).QCC_DSP__c){
          con.Manager_Login__c = Trigger.oldMap.get(con.Id).QCC_DSP__c
		}
        if(con.QAM_DSP__c != Trigger.oldMap.get(con.Id).QAM_DSP__c){
           //Store the old value in different field
        }
	}
}

 

All Answers

KaranrajKaranraj
Try the below code,
trigger fieldChanged on Contact(before update){
	for(Contact con: Trigger.New){
		if(con.QCC_DSP__c != Trigger.oldMap.get(con.Id).QCC_DSP__c){
          con.Manager_Login__c = Trigger.oldMap.get(con.Id).QCC_DSP__c
		}
        if(con.QAM_DSP__c != Trigger.oldMap.get(con.Id).QAM_DSP__c){
           //Store the old value in different field
        }
	}
}

 
This was selected as the best answer
Jyosi jyosiJyosi jyosi
Thanks it worked but if i want to add this in Apex class and call it in trigger how can i do it.

Regards,
Jyo
Balajee S 10Balajee S 10
//CLASS COSE:
public class MyClass{  
 public static void TestMethod(){    
         //Your code in the class    
}
}

//TRIGGER CODE:
Trigger testTrigger on Account(before insert){  
   MyClass.TestMethod();
}
Adila Hilal 9Adila Hilal 9
Hi,

I am trying to call an Apex method from Process builder to update a ProfileID='System Administrator' with a field value in  LastProfileId that changes every time we edit a user profile to system admin, actually through a formula it poulates with user last profile.My question is how to refer this fileld in my code?
list<user> users = [SELECT Id, IsActive,LastProfileId, FROM User where LastprofileId in :[select id ,LastProfileID where LastProfieID = 'LastProfileID__c'
Profile p = [select id from profile where name='System Administrator'];
for(User u:users){ 
 u.LastprofileId= p.id; 

update users;
Adila Hilal 9Adila Hilal 9
Hi,

In my code where I have mentioned 'LastProfileID__c', I need guidance is it right way to refer a field which has a dynamic value evertime a user is edited.