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);
}
}
}
}

  • Ky0l Profile Picture
    10 on at
    RE: JavaScript - double "if" not working?

    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);

       }

       }

       }

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: JavaScript - double "if" not working?

    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.

  • Suggested answer
    meelamri Profile Picture
    13,216 User Group Leader on at
    RE: JavaScript - double "if" not working?

    Hi, 

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

    Good Luck ! 

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Siv Sagar Profile Picture

Siv Sagar 149 Super User 2025 Season 1

#2
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 53 Super User 2025 Season 1

#3
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 32 Super User 2025 Season 1

Overall leaderboard

Product updates

Dynamics 365 release plans