1. I have an object called log__c in that fields have Description__c, Action__c(label name :Title)
  2. Lookup with Contact object, when I change the phone field of contact the object then in log__c object with Description__c field should be inserted that old phone is 123 and new phone 456 like this my requirement.

I have tried this code but is updating multiple times with check and the Description field is empty, In Description, I need to show old and new values which changed respectively.

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;
    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;
    update logtimeUpdate;


enter image description here

  • 2
    Any specific reason you're not simply using Field History Tracking for this? And BTW, the code is obviously flawed since you don't correctly track the old and new phone numbers. Every iteration in the second loop will use the last values obtained in the first loop for these two variables.
    – Phil W
    Jun 7, 2021 at 19:20

1 Answer 1


You should probably use Field History Tracking for this, but in the meantime, here's a corrected version of your code:

trigger oldvalue on Contact (after insert, after update) {   
    Set<Id> contactsWithUpdate = new Set<Id>();
    list<Log__c> logtimeUpdate = new list<Log__c>();

    if (Trigger.isAfter && (Trigger.isUpdate || Trigger.isInsert)) {
        for (Contact contact : Trigger.new) {
            if ((Trigger.isInsert && contact.Phone != null) ||
                (contact.Phone != Trigger.oldMap.get(contact.Id).Phone)) {

    for (Log__c log :
            [SELECT Contact__c, Description__c, Action__c
                FROM Log__c
                WHERE Contact__c IN :contactsWithUpdate]) {
        String oldPhone = Trigger.isInsert ? '' : Trigger.oldMap.get(log.Contact__c).Phone;
        String newPhone = Trigger.newMap.get(log.Contact__c).Phone;

        log.Action__c = 'check';       
        log.Description__c += '\n phone number old value =' + oldPhone + ', New Phone = ' + newPhone;

    update logtimeUpdate;

(Since you included a carriage return in the description, I've guessed you want to simply append the new text to an existing description.)

This also has a further problem: this doesn't cater for creating the Contact-specific Log__c records in the first place. That could be addressed here too, but I've not done so since I don't know if you have something else handling this.

  • I have added your code but still facing the same error description is empty and updating multiple times, the same issue facing.
    – user97180
    Jun 8, 2021 at 4:27
  • What is creating the Log__c record for a contact?
    – Phil W
    Jun 8, 2021 at 5:53
  • Log__c object is used for Tracking history purpose of contact
    – user97180
    Jun 8, 2021 at 6:00
  • Yes, but what is creating them? The above trigger code only updates these objects, it doesn't create them.
    – Phil W
    Jun 8, 2021 at 7:18
  • log__c is nothing to create, it used for lwc screen only showing the history
    – user97180
    Jun 8, 2021 at 11:08

You must log in to answer this question.