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
sfdc_1sfdc_1 

i want to assign 100 different profile with same permission set through code in salesforce

i want to assign 100 different profile with same permission set through code in salesforce 
Best Answer chosen by sfdc_1
ManojjenaManojjena
Hi ajpsskb,

Please chekc below code and let me know if it works !!
 
List<PermissionSetAssignment> permissionSetList = new List<PermissionSetAssignment>();
Id permissionSetId=[SELECT id FROM permissionSet WHERE Name ='permissionSetName'].id;
Set<String> profileSet= new Set<String>{'xxx','yyy','ZZZ'};
for (User u : [SELECT ID,UserRole.Name,Profile.Name,IsActive FROM User WHERE  UserRole.Name IN:profileSet AND IsActive = true]){ // Add fields as per your requirement...
    PermissionSetAssignment psa = new PermissionSetAssignment (PermissionSetId = permissionSetId, AssigneeId = u.Id);
    permissionSetList.add(psa);
}
try{
    upsert permissionSetList;
}catch(exception e){
    system.debug('exception caught' + e);
}

Thanks ,
Manoj

All Answers

ManojjenaManojjena
Hi ajpsskb,

Please chekc below code and let me know if it works !!
 
List<PermissionSetAssignment> permissionSetList = new List<PermissionSetAssignment>();
Id permissionSetId=[SELECT id FROM permissionSet WHERE Name ='permissionSetName'].id;
Set<String> profileSet= new Set<String>{'xxx','yyy','ZZZ'};
for (User u : [SELECT ID,UserRole.Name,Profile.Name,IsActive FROM User WHERE  UserRole.Name IN:profileSet AND IsActive = true]){ // Add fields as per your requirement...
    PermissionSetAssignment psa = new PermissionSetAssignment (PermissionSetId = permissionSetId, AssigneeId = u.Id);
    permissionSetList.add(psa);
}
try{
    upsert permissionSetList;
}catch(exception e){
    system.debug('exception caught' + e);
}

Thanks ,
Manoj
This was selected as the best answer
sfdc_1sfdc_1
thanks manoj it'sworking fine