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
Sahaya Steffi JSahaya Steffi J 

Restrict ModifyAll permission using Triggers

I have a requirement where I should restrict ModifyAll permissions on Account Object for all profiles(except System Admin and Chatter Free User) using a Trigger. Any suggestion on this is appreciated.

Thanks.
Best Answer chosen by Sahaya Steffi J
Ajay K DubediAjay K Dubedi
Hi Sahaya,

Use bellow code trigger for restricting Modify permissions on Account Object 
trigger preventAccountModify on Account (before update) {

  
for(Account acc : trigger.old){
        acc.adderror('Account Cannot be Modify');
    }

}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com

All Answers

Ajay K DubediAjay K Dubedi
Hi Sahaya,

Use bellow code trigger for restricting Modify permissions on Account Object 
trigger preventAccountModify on Account (before update) {

  
for(Account acc : trigger.old){
        acc.adderror('Account Cannot be Modify');
    }

}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
This was selected as the best answer
Sahaya Steffi JSahaya Steffi J
Thanks Ajay for you Help!! I have modified it as below as per my requirement. 

 
trigger restrictModifyAll on Account (before update) {
    Id userProfileId = userinfo.getProfileId();
    String userProfileName = [SELECT ID, Name from Profile Where Id = : userProfileId].Name;
    
    if( userProfileName != 'System Administrator' && userProfileName !='Chatter Free User')
    {
        for(Account acc : trigger.old)
        {
            acc.adderror('Account Cannot be Modified');
        }
        
    }
}

 
Delphine TsirananaDelphine Tsiranana
Hi Sahaya,
Thanks for your question which is stirring me towards the use of a trigger to restrict permissions for one particular user.
They already have a session-based permission set that we can activate or de-activate via Flow and their own custom Profile.
But we also need them to only see records in our org that have been tagged with a particular Topic (https://help.salesforce.com/s/articleView?id=sf.collab_admin_topics.htm&language=en_US&r=https%3A%2F%2Fwww.google.com%2F&type=5 (https://help.salesforce.com/s/articleView?id=sf.collab_admin_topics.htm&language=en_US&r=https%3A%2F%2Fwww.google.com%2F&type=5)).
Is there a way of doing that via trigger that does not limit their developer role (they need to build a data trasnfer solution but can only test it on dummy data record)? A similar trigger on all of the objects where we want them to be able to see and modify a sample of records? Thanks in advance for any insight!