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:

24 comments:

  1. Thanks for sharing a good information about salesforce . .......This article is very useful to learners. Sales force online training

    ReplyDelete
  2. very usefull informatation.and iam looking for more posts like this please keep updating us........

    ReplyDelete
  3. Truly a very good article on how to handle the future technology. After reading your post, thanks for taking the time to discuss this, I feel happy about and I love learning more about this topic

    Saleforce Training in Gurgaon
    Saleforce Developer Training in Gurgaon
    Salesforce lightning training in Gurgaon
    Salesforce Einstein training in Gurgaon
    Salesforce integration training in Gurgaon

    ReplyDelete
  4. Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work. reverse phone lookups

    ReplyDelete
  5. I found your article on checking permission settings quite informative. By the way, if you're in Vizag, our self-drive cars in Vizag offer convenient and flexible travel options.

    ReplyDelete
  6. For the most current and accurate information on Kenya trade statistics, I recommend checking Import Globals. For more information about global import export data visit our website.
    Kenya Import Data

    ReplyDelete

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...