Conditionally setting a field as mandatory on a UI page

Farah5
Tera Contributor

I have a Risk Assessment that was built through Risk Assessment Designer. In that designer, there is the option to set a field as mandatory or not. I have one field that is not mandatory, but my requirements are that based on the response on a previous field, that field should then become mandatory. 

The Risk Assessment is being rendered on a UI page, which is grabbing the desired assessment id and then rendering the assessment instance questions. 

I know that using g_form.setMandatory() wouldn't work if I was building these fields from scratch, but since they are coming from an existing table and have a value associated with them, is there a way to use out of the box ServiceNow commands to conditionally make this field mandatory? Right now nothing I'm doing seems to work, but maybe I'm just doing it wrong. I am accessing the map that is pulling together all of the assessment instance questions. Once I get that map, I'm searching for that specific field using the values that were assigned when building them in the Risk Assessment Designer. Here's my code:

 

var questionIdMap = '' + document.getElementById('jvar_question_id_map').value;
questionIdMap = JSON.parse(questionIdMap);


var pastDueDescriptionId = questionIdMap['past_due_description'];
g_form.setMandatory(pastDueDescriptionId, true); // doesn't work

 

I've also tried to get the field and just setting the "required" attribute, but this doesn't do anything either. It does set the attribute, but doesn't actually make the field mandatory. I need the field to be mandatory, and I need that little red start next to it and I need the form to not submit unless that field is populated. Is this something would end up involving a lot of customization?

1 REPLY 1

Allen Andreas
Administrator
Administrator

Hi,

Unfortunately, there isn't a traditional way to make it mandatory.

You'd have to set the attribute to required, and then check the value and if supplied, set not required. For submission, you'd have check field for value as well, in script.

Example: https://community.servicenow.com/community?id=community_question&sys_id=c217a403db31a3406c1c02d5ca96...

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!