Asked by:
Set Required Script not working

Question
-
I'm having a problem getting this script to work. I have already checked all the schema names and everything looks correct. I have it as an onChange Event for the Request Type field. Input would be greatly appreciated!
function setrequire()
{
var type = Xrm.Page.getAttribute("new_opportunitytype").SelectedOption().text;
var requesttype = Xrm.Page.getAttribute("orbus_requesttype").SelectedOption().text;
if (type == "Standard / Stock Items" && requesttype == "Rendering Only")
{
Xrm.Page.getAttribute("orbus_endclientname").setRequiredLevel("required");
{
}Thursday, November 13, 2014 3:42 PM
All replies
-
use getText() instead of SelectectOption().text, because it handle better null situations
and both fields must be optionset, not lookup
function setrequire() { var type = Xrm.Page.getAttribute("new_opportunitytype").getText(); var requesttype = Xrm.Page.getAttribute("orbus_requesttype").getText(); if (type == "Standard / Stock Items" && requesttype == "Rendering Only") { Xrm.Page.getAttribute("orbus_endclientname").setRequiredLevel("required"); } // this now corrected }
http://www.crmanswers.net/2014/07/dont-use-getselectedoptiontext.html
My blog: www.crmanswers.net - Rockstar 365 Profile
- Edited by Guido PreiteMVP Thursday, November 13, 2014 4:23 PM
Thursday, November 13, 2014 3:49 PM -
Still allows me to save the form without entering the end client name.Thursday, November 13, 2014 4:01 PM
-
Both fields are option set typesThursday, November 13, 2014 4:02 PM
-
I noticed that in your code the one { in the end is wrong. It should be } } (the first to close the if, the second to close the function) in the end, not { }
However debug your code and add some alert to check what is happening:
function setrequire() { alert("start"); var type = Xrm.Page.getAttribute("new_opportunitytype").getText(); alert("type is: " + type); var requesttype = Xrm.Page.getAttribute("orbus_requesttype").getText(); alert("requesttype is: " + requesttype); if (type == "Standard / Stock Items" && requesttype == "Rendering Only") { alert("set to required");
Xrm.Page.getAttribute("orbus_endclientname").setRequiredLevel("required"); } alert("done"); }
My blog: www.crmanswers.net - Rockstar 365 Profile
- Edited by Guido PreiteMVP Thursday, November 13, 2014 4:22 PM
Thursday, November 13, 2014 4:21 PM -
Please ensure that "Standard / Stock Items" and "Rendering Only" are in same case as in form.
Regards, Saad
Friday, November 14, 2014 5:24 AM