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
    Ky0l 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
    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
    meelamri 13,212 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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans