Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Set a field mandatory based on a condition

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 :) 
  • Suggested answer
    Amit Katariya007 Profile Picture
    Amit Katariya007 8,525 Super User 2024 Season 1 on at
    RE: Set a field mandatory based on a condition

    Hi,

    Are you Passing the Execution Context from the Form itself?

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

    Thank you,

    Amit Katariya

  • Archetype Profile Picture
    Archetype 411 on at
    RE: Set a field mandatory based on a condition
    [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
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Set a field mandatory based on a condition

    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
    Archetype 411 on at
    RE: Set a field mandatory based on a condition
    [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
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Set a field mandatory based on a condition

    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
    Archetype 411 on at
    RE: Set a field mandatory based on a condition
    [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
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Set a field mandatory based on a condition

    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
    Archetype 411 on at
    RE: Set a field mandatory based on a condition
    [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
    Nettsales Profile Picture
    Nettsales 112 on at
    RE: Set a field mandatory based on a condition

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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans