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
kaustubh chandratrekaustubh chandratre 

Write a trigger on Contact. If the Phone field is empty and Mobile field is not empty then assign Mobile field value to the Phone field.

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Kaustubh,

Please try the below logic in Trigger.

Please note that i have added the logic in insert and update scenerio as well. And also in case of update if user is updating the mobile field and previously the phone was copied from the mobile field then the mobile field will also update.
 
trigger ContactTrugger on Contact (before insert,before update) {
    List<Contact> conlist= new List<Contact>();
    If(Trigger.isinsert && Trigger.isbefore){
    
    for(Contact c:Trigger.new){
        if(c.Phone==null && c.MobilePhone!=null){
            c.Phone=c.MobilePhone;
        }
        
    }
    }
    if(Trigger.isupdate && Trigger.isbefore){
        
        
        for (Contact c:Trigger.new){
            Contact oldcontact= Trigger.oldmap.get(c.id);
            if(c.Phone==null && c.MobilePhone!=null){
                c.Phone=c.MobilePhone;
            }
            if(c.phone!=null && c.MobilePhone !=oldcontact.MobilePhone && c.Phone==oldcontact.MobilePhone )
            {
                c.Phone=c.MobilePhone;
            }
        }
        
    }
}
Please let me know if you have any issues in the code.

If this solution helps, Please mark it as best answer.

Thanks,

 
CharuDuttCharuDutt
Hii Kaustabh
Try Below Trigger
trigger ContactTrigger on Contact (before insert,before update) {
    if(trigger.IsBefore){
        if(trigger.IsInsert){
            for(Contact con : trigger.new){
                if(con.phone == null && con.MobilePhone != null){
                    con.Phone = con.MobilePhone;
                }
            }
    	}
        if(trigger.IsUpdate){
        for(Contact con : trigger.new){
                if((con.phone == null && con.MobilePhone != null && con.MobilePhone != trigger.oldMap.get(con.Id).MobilePhone)
                   ||(con.phone != null && con.MobilePhone != null && con.MobilePhone != trigger.oldMap.get(con.Id).MobilePhone)){
                    con.Phone = con.MobilePhone;
                }
            }
    	} 
    } 
}
Please Mark It As Best Answer If It Helps
Thank You!
Suraj Tripathi 47Suraj Tripathi 47
Hii Kaustabh,
you can use the below code

Here is trigger code in which we call a apex
trigger yaquestion on Contact (after insert,after update) {
     if(trigger.Isafter && (trigger.Isinsert || trigger.Isupdate)){       
       // CopyBillingAddress.CopyAddressMailing(trigger.new);
       yaquest.YaMethod(trigger.new);
    }
}

Below is Apex code

public class yaquest {
    public static void YaMethod (list<contact> Conlist){
         set<Id>Conid=new set<Id>();
            for(contact con:Conlist)
            {
                Conid.add(con.id);
            }
        list<contact> Conobj=new list<contact>();
        Conobj=[select phone,MobilePhone from contact where phone=null AND id in :Conid];
        for(contact ConLoop:Conobj)
        {
            ConLoop.phone=ConLoop.MobilePhone;
        }
        update Conobj;
    }
}

Thanks