You need to sign in to do that
Don't have an account?
chikku
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
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
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; }
You can modify the above trigger snippet to below way and check once:
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.