Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

MS dynamics set required JavaScript

Posted on by 140

Hello, Guys, I have this script that sets a field required if another field value == submit, I debugged it and var field is equal submit, but the resolution field does not become required right a away it only becomes required when you save the record 

don't know what I am doing wrong need some help and feedback 

function setRequired()
{

// Access the field on the form
var field = Xrm.Page.data.entity.attributes.get("new_reportstatus").getText();
if(field == "Submit")
{
Xrm.Page.getAttribute("new_resolution").setRequiredLevel("required");
}
else
{
Xrm.Page.getAttribute("new_resolution").setRequiredLevel("none");
}
}

  • msboy Profile Picture
    msboy 140 on at
    RE: MS dynamics set required JavaScript

    Thank you, everyone, it worked

  • Verified answer
    LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: MS dynamics set required JavaScript

    Hi msboy,

    Did you try my suggestion? Have you resolved your problem?

    If my answer helped you, please mark my answer as verified so that we could help many other users with the same issue.

    Best Regards,

    Leo

  • Verified answer
    LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: MS dynamics set required JavaScript

    Hi partner,

    If you want to use js code, you should check the following points:

    1.If "new_reportstatus" is a text field, you could try var field =Xrm.Page.getAttribute("new_reportstatus").getValue() instead.

       If "new_reportstatus" is an options set field, you could try var field =Xrm.Page.getAttribute("new_reportstatus").getText() instead.

    2.According to your description, you may set the js function when "OnLoad" the form, you should add this function to "new_reportstatus" feild onchange event.

    Hope it helps.

    Best Regards,

    Leo

  • msboy Profile Picture
    msboy 140 on at
    RE: MS dynamics set required JavaScript

    Business rule would not work because it’s a multiple select option set

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: MS dynamics set required JavaScript

    Hi,

    I am not sure why you are not using business rules, use busineas rules no code approach.

    Following things you need to check.

    1. If I am not wrong "new_reportstatus" is an optionset field so always check with value instead of hard code text , text may be changed.

    var optval= Xrm.Page.getAttribute("new_reportstatus").getValue();

    if(optval == "86000000")

    {

    // set field required

    }

    2. In which event you are triggering the method , You need to trigger on change  of the new_reportstatus field or in the onload to see the field required before save.

    3. In addition when you debug your code required field  line is executing?

  • Verified answer
    TylerS.Dev Profile Picture
    TylerS.Dev 725 on at
    RE: MS dynamics set required JavaScript

    You probably have your function set to trigger during OnSave event.

    You should register your function on "new_reportstatus" field during OnChange event using form editor, see my screenshot below:

    87827.Capture.PNG

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

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

Featured topics

Product updates

Dynamics 365 release plans