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 :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

JavaScript - double "if" not working?

(0) ShareShare
ReportReport
Posted on by 10

Hello all,

My JavaScript works without error or apparent issue, however it's not performing the last set of curly braces as expected, and wondering if someone more knowledgeable than I would know why its going wrong?
The test record is in the right status and I am using exclusively that security role GUID as my logged in user. Please find the code below:

function checkRole(executionContext) {
debugger;
var formContext = executionContext.getFormContext();
formContext.getControl('red_clarification').removeOption(283390000);

var userSecurityRoles = Xrm.Utility.getGlobalContext().userSettings.securityRoles;
var userRoleName = "{39847b08-e1c3-ea11-a812-000d3a86d6ea}"


// Checking if Security Role Id found in User Security Roles
for (var x = 0; x < userSecurityRoles.length; x++) {


if (userSecurityRoles[x]==userRoleName)
{
var status = formContext.getAttribute("red_status").getValue();
if (status == '283390001') {
formContext.getControl('red_clarification').addOption(283390000);
}
}
}
}

I have the same question (0)
  • Suggested answer
    meelamri Profile Picture
    13,216 User Group Leader on at

    Hi, 

    Please replace if (status == '283390001') by if (status == 283390001) 

    Good Luck ! 

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    Please refer to this API doc:https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/controls/addoption 

    The first parameter of addOption() function should be Object type, not Number type.

    So change your code in the last set of curly braces to  formContext.getControl('red_clarification').addOption({"text":"yourtext","value":283390000});

    And please make sure the result of condition(status == '283390001') is true, you can  console.log(status == '283390001');  to check.

  • Ky0l Profile Picture
    10 on at

    Thank you for the suggestions, I ended up with this:

    function checkRole(executionContext) {

       debugger;

       var formContext = executionContext.getFormContext();

        var userSecurityRoles = Xrm.Utility.getGlobalContext().userSettings.securityRoles;

       var userRoleName = "39847b08-e1c3-ea11-a812-000d3a86d6ea"

       var status = formContext.getAttribute("red_status").getValue();

        // Checking if Security Role Id found in User Security Roles

       for (var x = 0; x < userSecurityRoles.length; x++) {

       if (userSecurityRoles[x] == userRoleName && status == 283390001) {

       formContext.getControl('red_clarification').removeOption(283390000);

       }

       }

       }

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 > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 58 Most Valuable Professional

#2
#ManoVerse Profile Picture

#ManoVerse 42

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 38 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans