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 

Unable to insert old and new values in apex trigger

Hi,
 I have an object called log__c in that fields have Description__c, Action__c
with 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
trigger oldvalue on Contact (after insert,after update,  before insert) {   

 Log__c  logtime =new Log__c();
        logtime=[Select id,Name,Contact ,Description__c,Action__c FROM Log__c ];
   if(trigger.isbefore && trigger.isUpdate ){
     for(integer i=0; i<trigger.new.size();i++){
     if(Trigger.New[i].Phone!=Trigger.Old[i].Phone){
        
     logtime.Action__c ='check';       
     logtime.Description__c +='\n phone number old value='+Trigger.Old[i].phone+  '   New  phone ' + Trigger.New[i].phone;
   
              insert logtime;         
     }   }
     }
}

Please help
 
chikkuchikku
Hi CharuDutt,

It throws error like this Method does not exist or incorrect signature: void Add(List<Log__c>) from the type List<Log__c>

User-added image
chikkuchikku
Hi Hi CharuDutt,
It works but there is some error, it is updating more  times and there is nothing update in Description__c is empty 
Action__c is title label name,

User-added image
Can you please help it 
 
chikkuchikku
Hi CharuDutt,

There is any way to avoid this issue 
chikkuchikku
@CharuDutt yes I have updated and checked it printing multiple times and I have checked the Description Field Name is correct only :(

there is any solution there?

 
chikkuchikku
It is possible to insert rather than update @CharuDutt or any resolution there ? to avoid multi-times update and description are empty?
chikkuchikku
@CharuDutt AM using Contact object Phone only

User-added image
chikkuchikku
In your code, u didn't mention Newmap , only mention oldmap
chikkuchikku
 @CharuDutt  Yes, but the only issue is it updating multiple times and description is empty :(
chikkuchikku
Have u got any resolved solution @CharuDutt