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
krishna23krishna23 

Field is not writeable: Case.ContactMobile

trigger casePhone on Account (After insert,After update) {
    for(Account a : trigger.new){
    list<case> kca = New list<case>([select id,ContactMobile from case where ContactMobile != null And Accountid =: a.id]);
    for(case ca : kca){
        ca.ContactMobile = a.Phone;
        
    }
  }  
}
HARSHIL U PARIKHHARSHIL U PARIKH
Try using the following trigger,
 
trigger casePhone on Account (After insert,After update) 
{ 
    List<Case> caseFinalList = New List<Case>(); 
    for(Account a : trigger.new)
    {
        list<case> kca = New list<case>();
        kca = [select id, Contact.MobilePhone from case where Contact.MobilePhone != null And Accountid =: a.id];
        
        If(kca.size() > 0)
        {
            for(case ca : kca)
            {
                ca.Contact.MobilePhone = a.Phone;
                caseFinalList.add(ca);
            }
        }     
   } 
   
   try{
       If(!caseFinalList.IsEmpty()){
           update caseFinalList ;
       }
   }
   Catch(Exception e){
       System.debug(e.getMessage());
   } 
}

If it solves the puzzle the please mark it best answer since it will help other users with similar issues!
krishna23krishna23
Thanks Govind...  Could you please tell me reason why it is not taking contactmobile.
HARSHIL U PARIKHHARSHIL U PARIKH
It's because the API name of a mobile field on Contact is MobilePhone and not ContactMobile that's why.

Kishore, if you think the questionis solved the would you mind marking it best answer since it will help our community users!
Appreciated!
krishna23krishna23
I don't want to update contact phone, I want to update case phone when ever the accout phone updated
HARSHIL U PARIKHHARSHIL U PARIKH
Oh.. Wht's the API name of the field on case which holds the contact's phone?

Is it Contact_Mobile__c ?
HARSHIL U PARIKHHARSHIL U PARIKH
If case is associated with an Account and there is a field on case object which need to show the exact number as it's associated account has then you do not need trigger to do the job. You can make simple formula field named Account_Phone__c = Account.Phone

Hope it help!
PreyankaPreyanka
Hello Kishore,

I think you want to update the ContactMobile (Contact Mobile) field of case object but this is a standard field and it actually refers to Contact's MobilePhone (Mobile) field. If you update the MobilePhone(Mobile) field of Contact then it automatically update the ContactMobile field of Case. You don't need to write any code for this.

Hence for this scenario you need to update the MobilePhone field of Contact and it automatically update ContactMobile of Case.

Find the code below:
trigger casePhone on Account (After insert,After update) {
    list<case> kca = New list<case>();
    list<Contact> kcon = New list<Contact>();
    list<Contact> lstConupdate = New list<Contact>();
    Set<Id> cas = new Set<Id>();
    
    for(Account a : trigger.new){       
        kca = [select ContactId, Contact.MobilePhone from case where ContactMobile != null and Accountid =: a.id];
    
        for(Case cs: kca){
            cas.add(cs.ContactId);
        }
    
        kcon = [select id,MobilePhone from Contact where id in: cas];
        for(Contact ca : kcon ){
            ca.MobilePhone = a.Phone;
            lstConupdate.add(ca);                  
        }    
    }
    update lstConupdate;
}

But if you dont want to update contact's Mobile number then you can create a custom formula field in case.

Thanks
Preyanka