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
KbhaskarKbhaskar 

Apex Code to add permissions to Permission Sets

hi, 
i'm trying to update permission set from apex code but still it's incorrect can anyone help me out !
Error: actionradioab Compile Error: Variable does not exist: SobjectType at line 40 column 9

 
}
       public pagereference Assign()   
          {
        if(selectedValue == 'readselected')          
         {
        //update Object permission for Opportunity 
        actionradioab op = new actionradioab();
        op.permissionSetID = parameterValue;
        op.SobjectType = 'Opportunity';
        op.PermissionsCreate = false;
        op.PermissionsRead = true;
        op.PermissionsWrite = false;
        update op;

        }
        }
}

User-added image
 
Best Answer chosen by Kbhaskar
Shashi PatowaryShashi Patowary
Hi Bhaskar,

You can update permission set prividege using code similar to this -

       PermissionSet ps =[select id,ProfileId,SystemModstamp,UserLicenseId FROM PermissionSet where name='MyPermission'];
       ObjectPermissions op = new ObjectPermissions();
        op.parentid = ps.id;
        op.SobjectType = 'Opportunity';
        op.PermissionsCreate = false;
        op.PermissionsRead = true;
        op.PermissionsEdit = false;
        insert op;

Once you insert a record for a SobjectType, you can query the same record in ObjectPermissions  to make firther change.

Hope this will help you.

As a best practice please mark an answer as 'Solved' if it helps you.

Regards,
Shashi

All Answers

Shashi PatowaryShashi Patowary
Hi Bhaskar,

You can update permission set prividege using code similar to this -

       PermissionSet ps =[select id,ProfileId,SystemModstamp,UserLicenseId FROM PermissionSet where name='MyPermission'];
       ObjectPermissions op = new ObjectPermissions();
        op.parentid = ps.id;
        op.SobjectType = 'Opportunity';
        op.PermissionsCreate = false;
        op.PermissionsRead = true;
        op.PermissionsEdit = false;
        insert op;

Once you insert a record for a SobjectType, you can query the same record in ObjectPermissions  to make firther change.

Hope this will help you.

As a best practice please mark an answer as 'Solved' if it helps you.

Regards,
Shashi
This was selected as the best answer
KbhaskarKbhaskar
thanks Shashi !