You need to sign in to do that
Don't have an account?
Pathak 1
Apex trigger to assign a permission set to User
HI , We have Community set up in our org. My req is if a Particular Checkbox is checked on a Contact then It should assign a Permission set to that Community user.
I have written below code but it throws MIXED DML EXCEPTION -
I have written below code but it throws MIXED DML EXCEPTION -
trigger AssignPermissionSet on Contact (after Update) { for (contact c :trigger.new) { if(c.Is_SUper_User__c == true) { user u = [SELECT ID from User WHERE ContactId = :c.Id]; PermissionSetAssignment psa = new PermissionSetAssignment (PermissionSetId = 'XXXXXXXXXXXc94', AssigneeId = U.Id); insert psa; } } }I have heard to resolve this we have to use Future annotation, But can someone help with the class how should I accomplish this?
LBK code is not bulkified as there is SOQL inside for loop and DML operation as well. And you are getting error because you can not pass Object inside future method as perameter.
Use below code.
Class. Trigger. Let me know if this helps :)
Thanks!
Amit Singh
All Answers
Here is the class and the trigger you need.
Class Trigger Let me know if this help.
@future method accepts only primitive datatypes.
Try these.
APEX Class Trigger
Let me know if you face any issues with these.
LBK code is not bulkified as there is SOQL inside for loop and DML operation as well. And you are getting error because you can not pass Object inside future method as perameter.
Use below code.
Class. Trigger. Let me know if this helps :)
Thanks!
Amit Singh
You can even use the code below to bulkify the SOQL call.
And the @future method as it is from my previous response.
@Amit, After changing the method to Public I got the requirement working as required.
Thanks
Please call insert method into ContactTrigger and User Trigger.
Pass the Parameters ContactIds in Trigger like as below
Trigger Class:-
List<Contact> conList = new List<Contact>();
for(Contact c:Trigger.new){
conList.add(c.id);
}
contactTriggerHandler.assignPermissionSetToUsers(conList);
Apex Class:-