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
chikkuchikku 

Error Issue in insert old and new values in apex trigger

Hi,
 I have an object called log__c in that fields have Description__c, Action__c
and Lookup with Contact object, when I change phone field of contact object then in  log__c object of  Description__c field  should be inserted that old phone is 123 and new phone  456 like that I need

I have used the below code but is updating multiple times and the description empty like below

User-added image
trigger oldvalue on Contact (after insert,after update) {   
    Set<Id> lstLogId = new Set<Id>();
    String OlpPhone;
    String NewPhone;
 	list<Log__c> logtimeUpdate = new list<Log__c>();
   if(trigger.IsAfter && (trigger.IsUpdate || trigger.IsInsert)){
       for(Contact Con : trigger.new ){
           if(con.Phone != Trigger.oldMap.get(con.Id).Phone){
               OlpPhone = Trigger.oldMap.get(con.Id).Phone;
               NewPhone = con.Phone;
               lstLogId.Add(Con.Id); 
           }
       }
   }
      list<Log__c> logtime =   [Select id,Name,Contact ,Description__c,Action__c FROM Log__c Where Contact In:lstLogId ];
    For(Log__c l : logtime){
        l.Action__c ='check';       
     l.Description__c ='\n phone number old value ='+OlpPhone+  '   New Phone = ' + NewPhone;
        logtimeUpdate.Add(l);
   
    }
    update logtimeUpdate;
}

 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Chikku,

You can modify the above trigger snippet to below way and check once:
 
trigger oldvalue on Contact (after insert,after update) {   
    Set<Id> lstLogId = new Set<Id>();
    String OlpPhone;
    String NewPhone;
 	list<Log__c> logtimeUpdate = new list<Log__c>();
//if it is a new record then there wouldn't be any record //created in the log__c object right so we would be //handling it differently

if(trigger.isafter )
{
if(trigger.isinsert)
{
list<log__c> insertlog = new list<log__c>();
for(Contact c : trigger.new)
{
Log__c templogrec= new log__c();
if(c.phone!=NULL)
{
templogrec.Action__c ='check'; 
templogrec.Description__c ='\n phone number new value ='+c.phone;
insertlog.Add(l);
}
insert(insertlog);
}}

if(trigger.isupdate)
{
set<id> idset = new set<id>();
map<id,string> mapstring = new map<id,string>();
for(Contact Con : trigger.new ){
           if(con.Phone != Trigger.oldMap.get(con.Id).Phone){
idset.Add(Con.Id); 
String Description ='\n phone number old value ='+Trigger.oldMap.get(con.Id).Phone+ ' New Phone = ' + con.phone; 
mapstring.put(con.id,Description)               
           }
list<log__c> llist =  [select id,description,Contact,Description__c,Action__c FROM Log__c Where Contact In :idset]
list<log__c> logupdate = new list<log__c>();
for(log__c l : llist)
{
l.action = 'check';
l.description__c = mapstring.get(l.contact);
logupdate.add(l)
}
update logupdate;
}
}
   
}

You can try checking this once and please note that here I am assuming that there are no automated flows or processes that are creating log records when a new contact records.

Let me know if it helps you and close your query by marking it as the best answer so that it can help others in the future.  

Thanks.