web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Javascript function is not working

(0) ShareShare
ReportReport
Posted on by 13

Hello team,

We have javascript to check user role. which was working fine earlier.. recently it is not working

Its is not reaching to function getCurrentUserRoles ().

Code

:function CheckUserRole()

{
debugger;
var currentUserId = Xrm.Page.context.getUserId();
var currentUserRoles = getCurrentUserRoles(currentUserId);


}

I have the same question (0)
  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Xrm.Page is deprecated. Use formcontext instead.

    Also, to get logged in user role name you should use below code from article.

    www.inogic.com/.../

    It does not require web api call.

    Please share your entire code to check this issue including getCurrentUserRoles method definition.

    Please mark my answer verified if i were helpful

  • Suggested answer
    AJ-22040756-0 Profile Picture
    on at
  • Dhara dharsandia Profile Picture
    13 on at

    We are still in web client so we are using xrm.page.only

  • Dhara dharsandia Profile Picture
    13 on at

    function CheckUserRole() {

    debugger;

    var currentUserId = Xrm.Page.context.getUserId();

    var currentUserRoles = getCurrentUserRoles(currentUserId);

    }

    // Get Roles of User only

    function getCurrentUserRoles(currentUserId){

    var userId = currentUserId.slice(1, -1);

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/systemuserrolescollection?$select=roleid&$filter=systemuserid eq "+userId+"", true);

    req.setRequestHeader("OData-MaxVersion", "4.0");

    req.setRequestHeader("OData-Version", "4.0");

    req.setRequestHeader("Accept", "application/json");

    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

    req.onreadystatechange = function() {

       if (this.readyState === 4) {

           req.onreadystatechange = null;

           if (this.status === 200)

    {

               var results = JSON.parse(this.response);

    for (var i = 0; i < results.value.length; i++)

    {

                   var userRoleId = results.value[i].roleid;

    var Id=userRoleId.toUpperCase();

    if (Id ="BE962DBD-6807-EA11-A811-000D3AF2FD41")

                                    {

    Xrm.Page.getControl("discountamount").setDisabled(false);

    }

               }

           }

    else

    {

               Xrm.Utility.alertDialog(this.statusText);

           }

       }

    };

    req.send();

    }

  • cloflyMao Profile Picture
    25,210 on at

    Hi Dhara,

    if (Id === "BE962DBD-6807-EA11-A811-000D3AF2FD41") {
    
      // Do
    
    }

    Try to use triple equals.

    Regards,

    Clofly

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Use below updated code.

    function CheckUserRole() {
    
    debugger;
    
    var currentUserId = Xrm.Page.context.getUserId();
    
    var currentUserRoles = getCurrentUserRoles(currentUserId);
    
    }
    
    // Get Roles of User only
    
    function getCurrentUserRoles(currentUserId){
    
    var userId = currentUserId.slice(1, -1);
    
    var req = new XMLHttpRequest();
    
    req.open("GET", Xrm.Page.context.getClientUrl()   "/api/data/v9.1/systemuserrolescollection?$select=roleid&$filter=systemuserid eq " userId "", true);
    
    req.setRequestHeader("OData-MaxVersion", "4.0");
    
    req.setRequestHeader("OData-Version", "4.0");
    
    req.setRequestHeader("Accept", "application/json");
    
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    
    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
    
    req.onreadystatechange = function() {
    
       if (this.readyState === 4) {
    
           req.onreadystatechange = null;
    
           if (this.status === 200)
    
    {
    
               var results = JSON.parse(this.response);
    
    for (var i = 0; i < results.value.length; i  )
    
    {
    
                   var userRoleId = results.value[i].roleid;
    
    var Id=userRoleId.toUpperCase();
    
    if (Id =="BE962DBD-6807-EA11-A811-000D3AF2FD41")
    
                                    {
    
    Xrm.Page.getControl("discountamount").setDisabled(false);
    
    }
    
               }
    
           }
    	   else
    
    {
    
               Xrm.Utility.alertDialog(this.statusText);
    
           }
    
       }
    
    };
    
    req.send();
    
    }

    Please mark my answer verified if i were helpful

  • Dhara dharsandia Profile Picture
    13 on at

    Thanks..It is working now with == in my code.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans