SalesforceAdding/Assigning the list of Permission Sets to a User via APEX

Adding/Assigning the list of Permission Sets to a User via APEX

 User userRecord = [ SELECT Id,Name FROM User limit 1];
List<String> permissionSetNames =new permissionSetNames();
permissionSetNames.add('PermissionSet1');//replace with your'e permission set name
permissionSetNames.add('PermissionSet2');//same here
List<PermissionSetAssignment> permissionSetAssignments = new List<PermissionSetAssignment>();
List<PermissionSet> allPermissionSets = [SELECT Id, Name FROM PermissionSet WHERE Name IN :permissionSetNames];
System.assertEquals(permissionSetNames.size(), permissionSets.size());

for (PermissionSet eachPermissionSet :allPermissionSets) {
    permissionSetAssignments.add(
        new PermissionSetAssignment(
            AssigneeId              = userRecord.Id,
            PermissionSetId         = eachPermissionSet.Id
        )
    );
}
insert permissionSetAssignments;

Hope this helps!!

For any kind of assistance on this, please feel free to contact me on [email protected] 

do share and comment if you like it

- Advertisement -spot_img

More From UrbanEdge

Top Salesforce Flow Interview Questions & Answers 2024 Part – 3

Top Salesforce Flow Interview Questions & Answers 2024 Part...

Top Salesforce Flow Interview Questions & Answers 2024 Part – 2

This blog will explore some of the most commonly...

Best Practices for Lightning Web Components (LWC)

Lightning Web Components (LWC) is a modern framework by...

Top Salesforce Flow Interview Questions & Answers 2024 Part – 1

Flow Interview QuestionsWhat is Salesforce Flow?Types of Salesforce FlowsWhat...

Mastering Salesforce Flows: Streamline Your Business Processes with Ease

Understanding the benefits of using Salesforce FlowsKey components of...

Top Salesforce Static Code Analysis Tools: Enhancing Code Quality and Security

#Heading1Introduction2What is Salesforce?3The Importance of Code Analysis4Understanding Static Code...

Trigger Challenge #12: Understanding the Trigger on Account and its Impact on Salesforce Development

As a Salesforce developer, understanding how triggers work is...

Best Practices for Apex Triggers

Introduction Apex Triggers are a fundamental aspect of Salesforce development,...
- Advertisement -spot_img