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
Revati BhavsarRevati Bhavsar 

How to change account name if it is read only ?

I've created an Inactive custom field on account. When the “Inactive” check box is checked and saved on the Account,  the Account’s name should be renamed by conacatenating '###Inactive###'  in front of its name. E.g Account Acme Inc. should get renamed to ###Inactive### Acme Inc. I have written an apex class method for it and that method is called in trigger. But it is not getting executed as the account name field is read only and not editable. Following is the apex code and trigger.
public Class AP_Accounts {
    
    
    public static void RenameAcc(List<Account> lstAcc)
    {
        for(Account acc : lstAcc) 
        {                 
               if(acc.Inactive__c == True)
               {
               
                    String accName = '##Inactive##';
                    accName +=acc.Name;                      
                    acc.Name = accName;
      
            }  
            System.Debug('Account name'+acc.name);

        }             
        
        try {
            UPDATE lstAcc;
        }
        catch(Exception e) {
        }
    }
}

Apex Trigger:
rigger AccountBeforeInsert on Account (before insert, before update,after insert, after update) {
    List<Account> lstAcc = new List<Account>();
    
    if(Trigger.isAfter)
    {
       if (Trigger.isInsert) 
       {

            AP_Accounts.RenameAcc(Trigger.new);

       }

      }    
}

 
SandhyaSandhya (Salesforce Developers) 
Hi Revathi,

where did you make Account name field as read-only?
As far as I know, trigger won't update read-only field.

And also I see in your code you are using After insert and After update and doing DML for trigger.new


In Trigger.New the records are read only in that context as they have been written, but not committed, to the database.

So update your code as below.
Trigger AccountBeforeInsert on Account (before insert, before update) {
    List<Account> lstAcc = new List<Account>();
    

            AP_Accounts.RenameAcc(Trigger.new);

       

      }
 
public Class AP_Accounts {
    
    
    public static void RenameAcc(List<Account> lstAcc)
    {
        for(Account acc : lstAcc) 
        {                 
               if(acc.Inactive__c == True)
               {
               System.debug('inactive');
                    String accName = '##Inactive##';
                    acc.Name=acc.Name+accName; 
                                    System.debug('inactive'+acc.Name);
                   
                    //acc.Name = accName;
      
            }  
            System.Debug('Account name'+acc.name);

        }             
        
        try {
            UPDATE lstAcc;
        }
        catch(Exception e) {
        }
    }
}


Please refer below link for more information.

https://success.salesforce.com/answers?id=90630000000gu4ZAAQ

https://success.salesforce.com/ideaView?id=0873000000076pL


Hope this helps you!

Thanks and Regards
Sandhya

 
Revati BhavsarRevati Bhavsar
Thank you Sandhya. It is working now using before trigger
SandhyaSandhya (Salesforce Developers) 
Hi Revati,

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya