Business Required Field IF Other Field Contains Data, ELSE No Constraint
-
Thursday, May 31, 2012 2:36 PM
I have a "next steps due" date field that I would like to set as Business Required only if text is entered into a field above it (multiple lines of text, no constraint). If there is no text entered, I would like the next steps due date field set as No Constraint. Is this possible? If it's a Java thing, could you please help me with the code?
Thanks in advance.
All Replies
-
Thursday, May 31, 2012 2:54 PMModerator
Probably start out with your date field being required and then add code this this to the multi-line field's OnChange event (replace with the actual field names):
function OnChange() { if (Xrm.Page.getAttribute("YourMultiLineField").getValue() != "") { Xrm.Page.getAttribute("YourDateField").setRequiredLevel("required"); } else { Xrm.Page.getAttribute("YourDateField").setRequiredLevel("none"); } }
Jason Lattimer
- Proposed As Answer by JLattimerMicrosoft Community Contributor, Moderator Thursday, May 31, 2012 2:54 PM
- Edited by JLattimerMicrosoft Community Contributor, Moderator Thursday, May 31, 2012 9:15 PM
- Edited by JLattimerMicrosoft Community Contributor, Moderator Thursday, May 31, 2012 9:16 PM
- Marked As Answer by jaspla Friday, June 01, 2012 1:50 PM
-
Thursday, May 31, 2012 4:05 PM
Thanks for the code. Sorry for the noob question, but how do I enter this? I went into the event handler properties of the multi-line field and I see the ActivityFeeds.Form.js as the only option under Library. I tried pasting into Function, but that only takes one line of text and pasting it into Parameters throws an error when previewing the form. I was reading about Xrm.Page, but I don't know how to access it.
I'm somewhat new to Dynamics so a little more detail would be much appreciated.
-
Thursday, May 31, 2012 4:32 PMModerator
Go to:
Settings -> Customizations -> Customize the System
Web Resources from the left navigation -> New
Provide a name
Set the Type = Script (JScript)
Select Text Editor
Paste in the code - you will need to find the names of your fields and replace "YourMultiLineField" and "YourDateField" with the actual names. You can find them be selecting them on the form and going to Change Properties and looking at the Name (not Display Name) field for each.
Save -> Publish
Go Back to your form
Form Properties -> Under Form Libraries -> Add -> Select the Web Resource you just created
Select the multi-line field -> Change Properties -> Events -> Add - > Select your new Web Resource file
Enter "OnChange" as the Function
Save and Publish the form
Test
Jason Lattimer
-
Thursday, May 31, 2012 6:58 PM
Jason, thanks for the extra info. When I test, the "next steps" due field defaults to Required. It should start as No Constraint, then if there is data entered into the multi-line field, switch the due date field to Required. Either way, I'm getting an error with the multi-line field "Object doesn't support property or method 'setRequiredLevel'. Any ideas on what could be causing this?
-
Thursday, May 31, 2012 9:17 PMModeratorI updated the code - think there was a typo.
Jason Lattimer
-
Friday, June 01, 2012 1:56 PM
For our purposes, I started with the date field No Constraint. Works like a charm! As soon as something is typed and they click anywhere else, the date field switches to required.
Is there additional code we could add so if someone changes their mind and deletes what they were typing, the date field reverts back to No Constraint? Also, instead of waiting for the user to click elsewhere before the requirement level changes, is it possible to change it as soon as they start typing? Thanks again for all your help!