You need to sign in to do that
Don't have an account?
Revati 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.
Apex 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); } } }
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.
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
.
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