+ Start a Discussion
Prashant GulvePrashant Gulve 

write Apex Trigger on Lead object, there is 2 fileds Phone and Mobile Phone.if I make changes in phone same change needs to be happened in Mobile Phone filed as well.please help

Apex Trigger on Lead object, 2 Fileds Phone and Mobile Phone
if I make changes or updates/ modify any thing on phone same changes
needs to be happened to Mobile Phone as well?
Please Assist..
Best Answer chosen by Prashant Gulve
CharuDuttCharuDutt
Hii Prashant
Try Below Trigger
trigger leadtrigger on Lead (before insert,before update) {
    if(trigger.IsBefore){
        if(trigger.IsInsert){
    for(lead  l : trigger.new){
        if(l.Phone!= null){
            l.MobilePhone = l.Phone;
        		}
        	}
        }
        if(trigger.IsUpdate){
    for(lead  l : trigger.new){
        if(l.Phone!= null && l.Phone != trigger.oldMap.get(l.Id).Phone){
            l.MobilePhone = l.Phone;
        		}
        	}
        }
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Prashant
Try Below Trigger
trigger leadtrigger on Lead (before insert,before update) {
    if(trigger.IsBefore){
        if(trigger.IsInsert){
    for(lead  l : trigger.new){
        if(l.Phone!= null){
            l.MobilePhone = l.Phone;
        		}
        	}
        }
        if(trigger.IsUpdate){
    for(lead  l : trigger.new){
        if(l.Phone!= null && l.Phone != trigger.oldMap.get(l.Id).Phone){
            l.MobilePhone = l.Phone;
        		}
        	}
        }
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
Vadde Amaresh 2Vadde Amaresh 2
Hii Prashant
Try Below Trigger


trigger LeadTrigger on Lead (before insert,before Update) {
    
 if(trigger.isbefore & trigger.isupdate){
    
   // update Mobile Phone
   
    for(lead led :trigger.new){
        if(led.phone != null ){
            led.MobilePhone = led.phone;
        }
    }
     }
}



Please Mark It As Best Answer If It Helps
Thank You!
AMARESH
Prashant GulvePrashant Gulve
Hi CharuDatt and Amaresh,
 Thank you so much for your help both code working fine for me. Both are the best answer..
Thank you
Prashant Gulve
Prashant GulvePrashant Gulve
Yes, i have already Marked it as a Best Answer..
​​​​​​ Thank you..
Venu GopalVenu Gopal
Hi Prashant,

trigger syncphonetomobilephone on Lead (before update) {
    
    if(trigger.isbefore && trigger.isupdate){
    syncphonetomobilephonehandler.sync(trigger.new);
    }
}

public class syncphonetomobilephonehandler {
    public static void sync(list<lead> leadlist){
        list<lead> leadstoupdate=new list<lead>();
        for(lead l:leadlist)
        {
            l.MobilePhone=l.Phone;
            leadstoupdate.add(l);
        }
      
    }

}
Hope this answer helps!
Venu