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

Set a field mandatory based on a condition

(0) ShareShare
ReportReport
Posted on by 411

Hello mates,
I hope you 're all doing well, I am facing a situation that I have never encountered before

I would like to set a field required based on a condition ? I started writing a JS code but dont know if its correct or no
My requirement is to set the field "nameofsd" as required if the field "taskproject" is equal to "finished" or "finished" and if the field "team" is equal to "Team 1" or "Team 2" or "Team3"

function setFieldrequirementasrequired(executionContext)
{
    // Getting formContext
    var formContext = executionContext.getFormContext();
    var task = formContext.getAttribute("new_taskproject").getValue();
    var team = formContext.getAttribute("new_projectteam").getValue();
    if (task == "finished" || "signed" && team= "Team1" || "Team2" || "Team3")
    {
        formContext.getAttribute("nameofsd").setRequiredLevel("required");
    }
    else
    {
        //setting Email field requirement as none.
        formContext.getAttribute("nameofsd").setRequiredLevel("none"); 
    }
}
Thanks in advance mates :) 
I have the same question (0)
  • Suggested answer
    Nettsales Profile Picture
    20 on at

    Maybe it's too simple, buy why not just use business rules to do this.  It's drag and drop in the UI?

  • Archetype Profile Picture
    411 on at
    [quote user="Nettsales"]

    Maybe it's too simple, buy why not just use business rules to do this.  It's drag and drop in the UI?

    [/quote]

    Hi, thanks for your quick reply, unfortunately it's not achievable in my case :(

  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    assuming these two fields are text (because if they are of another type like lookups the syntax is different) the problem is how the conditions are written, the correct way would be

    if ((task == "finished" || task == "signed") && (team == "Team1" || team == "Team2" || team == "Team3"))

  • Archetype Profile Picture
    411 on at
    [quote user="Guido Preite"]

    assuming these two fields are text (because if they are of another type like lookups the syntax is different) the problem is how the conditions are written, the correct way would be

    Fullscreen
    1
    if ((task == "finished" || task == "signed") && (team == "Team1" || team == "Team2" || team == "Team3"))
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    [/quote]

    Hi Guido Preite,

    The format of "taskproject" field is an options group list
    The format of "team" field is a lookup

    Regards

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

    Hi,

    I updated your code. Please copy and try that -

    function setFieldrequirementasrequired(executionContext)

    {

       // Getting formContext

       var formContext = executionContext.getFormContext();

       var task = formContext.getAttribute("new_taskproject").getText();

       var team = formContext.getAttribute("new_projectteam").getValue();

       if ((task == "finished" ||  task=="signed") && (team[0].name= "Team1" || team[0].name=="Team2" || team[0].name=="Team3"))

       {

           formContext.getAttribute("nameofsd").setRequiredLevel("required");

       }

       else

       {

           //setting Email field requirement as none.

           formContext.getAttribute("nameofsd").setRequiredLevel("none");

       }

    }

    nameofsd should  have prefix which you are missing in your code like new_nameofsd

    docs.microsoft.com/.../gettext

    docs.microsoft.com/.../getvalue

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Archetype Profile Picture
    411 on at
    [quote user="Bipin Kumar"]

    Hi,

    I updated your code. Please copy and try that -

    function setFieldrequirementasrequired(executionContext)

    {

       // Getting formContext

       var formContext = executionContext.getFormContext();

       var task = formContext.getAttribute("new_taskproject").getText();

       var team = formContext.getAttribute("new_projectteam").getValue();

       if ((task == "finished" ||  task=="signed") && (team[0].name= "Team1" || team[0].name=="Team2" || team[0].name=="Team3"))

       {

           formContext.getAttribute("nameofsd").setRequiredLevel("required");

       }

       else

       {

           //setting Email field requirement as none.

           formContext.getAttribute("nameofsd").setRequiredLevel("none");

       }

    }

    nameofsd should  have prefix which you are missing in your code like new_nameofsd

    docs.microsoft.com/.../gettext

    docs.microsoft.com/.../getvalue

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

    [/quote]

    Hi Bipin Kumar,

    i try'd your code below and unfortunately it doesnt work :(.

    function setFieldrequirementasrequired(executionContext)

    {

       // Getting formContext

       var formContext = executionContext.getFormContext();

       var task = formContext.getAttribute("new_taskproject").getText();

       var team = formContext.getAttribute("new_projectteam").getValue();

       if ((task == "finished" or i have to write "698 690 007" ?  ||  task=="signed") && (team[0].name= "Team1" || team[0].name=="Team2" || team[0].name=="Team3"))

       {

           formContext.getAttribute("new_nameofsd").setRequiredLevel("required");

       }

       else

       {

           formContext.getAttribute("new_nameofsd").setRequiredLevel("none");

       }

    }

    pastedimage1649116010998v1.png

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

    Hi,

    Add alert in your code to check the valuue -

    alert("Task Value:"+task);

    alert("Team Value:"+team[0].name);

    If you want to use integer value then you should change below line

    var task = formContext.getAttribute("new_taskproject").getText();

    to

    var task = formContext.getAttribute("new_taskproject").getValue();

    Debug your JS code - carldesouza.com/.../

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Archetype Profile Picture
    411 on at
    [quote user="Bipin Kumar"]

    Hi,

    Add alert in your code to check the valuue -

    alert("Task Value:"+task);

    alert("Team Value:"+team[0].name);

    If you want to use integer value then you should change below line

    var task = formContext.getAttribute("new_taskproject").getText();

    to

    var task = formContext.getAttribute("new_taskproject").getValue();

    Debug your JS code - carldesouza.com/.../

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

    [/quote]

    Hi mate,

    Sorry doesnt work too :(, you will find below the code i'm using : 

    function setFieldrequirementasrequired(executionContext)

    {
    // Getting formContext

    var formContext = executionContext.getFormContext();

    var task = formContext.getAttribute("new_task").getValue();
    var team = formContext.getAttribute("new_projectteamid").getText();

    if ((task =="698690007" || task=="698690008"||) && (team=="Team1" || team=="Team2" || team=="Team3" || team== "Team4" || team=="Team5" || team=="Team6"))

    {
    formContext.getAttribute("new_nameofsd").setRequiredLevel("required");
    }

    else

    {
    formContext.getAttribute("new_nameofsd").setRequiredLevel("none");

    }

    alert("Task Value:"+task);
    alert("Team Value:"+team);

    }

  • Suggested answer
    Amit Katariya007 Profile Picture
    10,409 Super User 2025 Season 2 on at

    Hi,

    Are you Passing the Execution Context from the Form itself?

    docs.microsoft.com/.../clientapi-execution-context

    Thank you,

    Amit Katariya

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
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 74

#3
Martin Dráb Profile Picture

Martin Dráb 64 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans