Wednesday 19 April 2017

How to check Permission Set in Custom Button Salesforce

Sometimes you may get requirement as below,

1. Create a Custom Button (JavaScript) and assign it to the page layout.
2. Create a Custom Permission Set.
3. If the logged in user not assigned in the specified permission set, then just throw an alert like "You don't have sufficient Permission". Else navigate to another page or whatever based on your requirement.

Custom Button JavaScript Code:

  1. {!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")}
  2.  
  3. var result = sforce.connection.query("SELECT Id FROM PermissionSetAssignment WHERE PermissionSet.Name = 'Payment_Type_Permission_Set' AND AssigneeId = '{!$User.Id}'");
  4.  
  5. var psAssignment = result.getArray("records");
  6. if(psAssignment.length === 1){
  7. window.location.href = '/apex/PaymentChange?id={!Log__c.Id}';
  8. }
  9. else
  10. {
  11. alert('You do not have sufficient permission. Please contact your administrator.');
  12. }


1. Initialized the REQUIRESCRIPT resource for SOQL query.
2. Queried PermissionSet using sforce.connection.query. 
Where,
PermissionSet.Name is API name of the Permission Set.
AssigneeId use
3. In apex we will check the List size, here get records using getArray method and assign it to var.
4. Check variable length which is equal to list size.
5. Finally, do logic as need. 


References:

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...