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
khalid ameen 9khalid ameen 9 

If an account has a rating then the user can’t change the account rating.

i want to write a trigger on it
CharuDuttCharuDutt
Hii Khalid 
Try Below Trigger
trigger AccountRating on Account (before update) {
    for(Account Acc : Trigger.new){
        If(Trigger.oldMap.get(Acc.Id).Rating!=Null){
            if(Acc.Rating != Trigger.oldMap.get(Acc.Id).Rating){
                Acc.AddError('Cannot Change rating');
            }
        }
    }

}
Please Mark It As Best Answer If It Helps
Thank You

 
Shatrughna SalunkeShatrughna Salunke
Hi Khalid,

try below code 

   // Call helper calss from Trigger in before udpate  event
     AccountTriggerHelper.checkRatiing(Trigger.newMap, Trigger.oldMap); 
    
    //Helper class Method
    public static void checkRatiing(Map<Id, Account> newMap, Map<Id, Account> oldMap) {
        for (Account acc : newMap.values()) {
            if (acc.Rating != null && acc.Rating != oldMap.get(acc.Id).Rating) {
                 acc.addError( 'User can’t change the account rating' );
            }
         }
    }
    
Thanks,
Shatrughna
Suraj Tripathi 47Suraj Tripathi 47
Hello 
Khalid
you can use this trigger :
trigger conditionRating on Account (before update) {
    for(Account acc : trigger.old){
        If(acc.Rating!=Null){            
                Acc.AddError('Cannot Change rating');
              }
        }

}

you can get references from this :
https://salesforce.stackexchange.com/questions/170716/making-a-field-as-un-editable-conditionally

If you find your Solution then mark this as the best answer

Thank you!

Regards,
Suraj Tripathi