Skip to main content

Notifications

Announcements

No record found.

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

Rule to set a field mandatory based on a condition

Posted on by 411

Dear all,

I'm trying to implement a JS rule code to set a field as mandatory or not, below the code that i am using, but unfortunately not working, do you have any idea where it is not working ? 

new_projecttask = drop down list values
msdyn_projectteamid = relation lookup field
new_fieldtobemandatoryornot =
Free text

function setFieldrequirementasrequired(executionContext)
{
   var formContext = executionContext.getFormContext();
   var task = formContext.getAttribute("new_projecttask").getValue();
   var team = formContext.getAttribute("msdyn_projectteamid").getValue();

   if ((task == "698 690 007" ||  task== "100 000 000" ||  task== "698 690 008") && (team[0].name= "TeamName1" || team[0].name== "TeamName2" || team[0].name== "TeamName3"))

   {

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

   }

   else

   {

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

   }

}

Thanks and happy new year :) .

Regards,

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Rule to set a field mandatory based on a condition

    Hi,

    So it is not working after adding on change event handler on Project Team??

    Try adding alert in your code for debugging...

    carldesouza.com/.../

  • Haig Liu Profile Picture
    Haig Liu Microsoft Employee on at
    RE: Rule to set a field mandatory based on a condition

    Hi Archetype,

    As far as I know,

    the attribute type of id type is usually Uniqueidentifier instead of string.

    And other types are string.

  • Archetype Profile Picture
    Archetype 411 on at
    RE: Rule to set a field mandatory based on a condition

    Hi Kumar,

    Thanks you, it is better the field is mandatory accordingly to the project task field, unfortunately it seems to be that the project team is not taken into consideration in the condition.

    Only the project task it taken into consideration, if i pick a random team i will always get the same result :

    (For information var team  is a string value) may it cause an issue ? 

    function setFieldrequirementasrequired(executionContext)
    {
       var formContext = executionContext.getFormContext();
       var task = formContext.getAttribute("new_projecttask").getValue();
       var team = formContext.getAttribute("msdyn_projectteamid").getValue();
    
       if ((task == 698690007 ||  task== 100000000 ||  task== 698690008) && (team[0].name= "TeamName1" || team[0].name== "TeamName2" || team[0].name== "TeamName3")) {
    
           formContext.getAttribute("new_fieldtobemandatoryornot").setRequiredLevel("required");
    
       }
    
       else {
    
           formContext.getAttribute("new_fieldtobemandatoryornot").setRequiredLevel("none");
    
       }
    
    }

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Rule to set a field mandatory based on a condition

    Hi,

    You need to add on change event handler on Team field change too.

    I hope you have only added on change event on project Task field but not on Project Team field

  • Archetype Profile Picture
    Archetype 411 on at
    RE: Rule to set a field mandatory based on a condition

    Hi Guido,

    Thanks you very much it is partially working now with the below code : 

    function setFieldrequirementasrequired(executionContext)
    {
       var formContext = executionContext.getFormContext();
       var task = formContext.getAttribute("new_projecttask").getValue();
       var team = formContext.getAttribute("msdyn_projectteamid").getValue();
    
       if ((task == 698690007 ||  task== 100000000 ||  task== 698690008) && (team[0].name= "TeamName1" || team[0].name== "TeamName2" || team[0].name== "TeamName3")) {
    
           formContext.getAttribute("new_fieldtobemandatoryornot").setRequiredLevel("required");
    
       }
    
       else {
    
           formContext.getAttribute("new_fieldtobemandatoryornot").setRequiredLevel("none");
    
       }
    
    }

    Issue 1 : I am facing a blocking error message that indicates me the field "new_nomdu" must be filled but only when i am in the first condition    
    if ((task == 698690007 ||  task== 100000000 ||  task== 698690008)

    but if i select a random team it is not taking into consideration the "&&" team values that i have indicated.

    Regards,

  • Suggested answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Rule to set a field mandatory based on a condition

    you need to remove the spaces inside the numbers.

    regarding the null check, just add a condition like

    team != null

    before checking team[0].name

  • Archetype Profile Picture
    Archetype 411 on at
    RE: Rule to set a field mandatory based on a condition

    Hi Guido,

    Thanks for your quick response, I have changed :

    From :

    if ((task == "698 690 007" ||  task== "100 000 000" ||  task== "698 690 008") && (team[0].name= "TeamName1" || team[0].name== "TeamName2" || team[0].name== "TeamName3"))

    To :

    if ((task == 698 690 007 ||  task== 100 000 000 ||  task== 698 690 008) && (team[0].name= "TeamName1" || team[0].name== "TeamName2" || team[0].name== "TeamName3"))

    But not working unfortunately, i'm getting the below message error :

    Web resource method does not exist: setFieldrequirementasrequired

    Regarding the second point, how could i please check i the team variable is not null before accessing the [0] position ?

    Thanks

  • Suggested answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: Rule to set a field mandatory based on a condition

    the dropdown list values (optionset/choice) is an integer, so the check should be

    task == 698690007

    also make sure the team variable is not null before accessing the [0] position

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans