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

Rule to set a field mandatory based on a condition

(0) ShareShare
ReportReport
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,

I have the same question (0)
  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    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

  • Archetype Profile Picture
    411 on at

    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
    54,086 Moderator on at

    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
    411 on at

    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
    Bipin D365 Profile Picture
    28,983 Moderator on at

    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
    411 on at

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

  • Haig Liu Profile Picture
    Microsoft Employee on at

    Hi Archetype,

    As far as I know,

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

    And other types are string.

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

    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/.../

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
Martin Dráb Profile Picture

Martin Dráb 47 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 38 Super User 2025 Season 2

#3
#ManoVerse Profile Picture

#ManoVerse 31

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans