10 Oct’17

Dynamics 365, EnterpriseCheck Security Roles of Logged in user using JavaScript


In this blog, we will be demonstrating how to use JavaScript to check user security roles. This is applicable in organization where, there are users with multiple security roles. And only users with a specific role assigned to them should be allowed to carry on certain tasks.


There is an organization with two users,”User A” is assigned a custom role and “User B” is not assigned a custom role. Only “User A” should be able to Save the form as he/she has the custom role.


Step 1: Create two users “User A” and “User B”.

Step 2:   Create a custom security role “CustomRoleA”.

User A” has the following security roles:

  • Salesperson
  • Sales App Access
  • CustomRoleA

“User B” has the following security roles:

  • Salesperson
  • Sales App Access

Step 3: Now we write a JavaScript code to check the logged in users security roles and accordingly allow only that user to save the record who has the “CustomRoleA” security role. If a user without that security role tries to save the a newly created record, an error notification is shown and record is not saved.

Step 4: Save this as a new JavaScript web resource in Dynamics 365 and we will make this code run on the “Contact” form which will be triggered on the form onSave event.

Step 5: Now when “User B” makes a new record and tries to save it the following error message is shown.

The record can’t be saved. The search results as shown in the below image states that there is no such record.

Step 6:  When “User A” tries to make a new contact and save, the permission is granted to save the record and in the search the record is shown as shown in the below two images.


By checking the security roles different functionalities can be restricted for any user like show/hide fields or buttons.

Written by

Clinton D'Mello

Trainee Software Engineer

2 thoughts on “Check Security Roles of Logged in user using JavaScript”

  1. Good blog and informative 🙂
    Few suggestions
    1. Use latest WebAPI in JS code
    2. Always prefer to use asynchronous request in web call.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.

Want to streamline your business processes?

  • This field is for validation purposes and should be left unchanged.

Recent Articles

  • How to hide the Delete button in a page in Microsoft Dynamics NAV

    21 March’ 2018

    Introduction: By default, Delete button is visible on every page in the Home tab but if we do not want the user to dele...

    Read more
  • Managing mailbox through Email Archiving

    20 March’ 2018

    Introduction: Email can fill your Outlook Inbox quickly – new messages, replies, and forwards. Before you know it, yo...

    Read more
  • Workaround to Report.SAVEASPDF in NAV 2018

    20 March’ 2018

    Objective: In NAV 2017 Emailing the PDF by running the report using Report.SAVEASPDF is now not allowed in NAV 2018. Th...

    Read more
  • Dynamics 365 Client Diagnostics

    15 March’ 2018

    Main cause of Performance Issues: Bandwidth and latency are the primary characteristics which affects the performance o...

    Read more
  • Closing Blind Shift in MPOS and CPOS in Dynamics 365 for Retail

    15 March’ 2018

    Manual For closing Blind shift in POS: Blind shifts terminate the currently ongoing shift and when logged in a new shif...

    Read more