Sunday, 21 August 2011

Able To Run As Another User

There are times that you don't want to give permissions to specific users permanently, so it would be useful if you could temporarily turn on and off running code under a certain user.

Eg: to allow users to create data in Custom Settings requires to either turn on Customize Applications permissions or to give Delegated System Admin privileges to Non System Admin users.

The only problem with Customize Applications and Delegated System Admin privileges is that it opens the door to greater permissions for those users.

The answer will be to temporarily give permissions in code using System.runas() but this is only allowed in testmethods

If it was possible to run code like below in normal code

Profile prID = [Select id From Profile where Name Like 'System Administrator' LIMIT 1];
        list<User> usr = [Select id From User where and IsActive=true limit 1];//take 1 user which will always exist
        if (usr.size() > 0){
                object1__c new
object1 = new object1__c ();

No comments:

Post a Comment

Note: only a member of this blog may post a comment.