Multiple Field Changes

Liz Abraham
Tera Contributor

Hello,

 

I have a requirement where I have 20 fields on a form and if I change the value of any of fields , I have to set a justification field to mandatory  and set it to focus and have the user provide a reason before saving the form. 

 

I used a business rule to check if the field changes and display a message but the mandatory or focus part i  can't do it in a BR. Any suggestions?

10 REPLIES 10

Sainath N
Mega Sage
Mega Sage

@Liz Abraham : As this will be a client-side transaction, you could use either a UI policy or client script to make the field mandatory if any of the 20 fields change on the form. UI policy is preferable.

 

https://docs.servicenow.com/bundle/vancouver-platform-administration/page/administer/form-administra...

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Alp Utku
Mega Sage

You can create onSubmit Catalog Client Script. I am providing sample script below

 

g_form.setMandatory('justification_field_name',true) ;
g_form.setValue('other_field_name_to_set_as_focus', 'focus') ;

Liz Abraham
Tera Contributor

so let's say in those 20 fields, some are empty and some are not and the user adds text or removes text. how do  I capture  that change in the UI policy? In other words I could make any change.  I did not see options for conditions unlike the business rule hat had the option of  ' if <field> changes '. 

 

Also @Alp Utku  , I wanted to set focus on the justification field and not set a value. how can I set the cursor to the justification field?

 

Thanks!

If i understand you correctly, you want to have the Business Rules "changes" functionality for several fields in some kind of client script.
This cannot be achieved by a UI Policy, but via the following clientScript:

function onLoad() {
	var justificationFieldName = 'justification'; // field name of the field which is mandatory
	var justificationMessage = 'A justification is required'; // message to inform the user why the field is suddenly mandatory
	var fieldsWithJustification = ['short_description', 'description', 'state']; // list of fields which require a justification

	var fieldChangedStates = {};
	// Note: There is also a standard method called 'onUserChangeValue', but this does not trigger when the value
	// gets changed back to its original value (in this case we'd need to reset the mandatoryness)
	g_form.getFormElement().on('glideform:onchange', function (event) {
		if (event.memo.id) {
			// fieldname is not just the field, but the field plus the table, e.g. "task.short_description"
			// => we need to strip the tablename from the fieldname to to a faster comparison
			var field = event.memo.id.replace(g_form.getTableName() + '.', '');
			fieldChangedStates[field] = event.memo.modified;

			var clearAll = true;
			if (Object.values(fieldChangedStates).includes(true)) {
				// if any of the fields are changed, the justification field is mandatory, a field message is shown and the field is focused
				g_form.setMandatory(justificationFieldName, true);

				if (justificationMessage) {
					g_form.hideFieldMsg(justificationFieldName, clearAll);
					var scrollToView = true;
					g_form.showFieldMsg(justificationFieldName, justificationMessage, 'warning', scrollToView);
				}

				g_form.getControl(justificationFieldName, true).focus();
			} else {
				// none of the fields are changed => justification is not mandatory (and we clear previously shown messages)
				g_form.setMandatory(justificationFieldName, false);
				if (justificationMessage) {
					g_form.hideFieldMsg(justificationFieldName, clearAll);
				}
			}
		}
	});
}

 You can fully configure the client script with the lines on top. Keep in mind that this uses an undocumented event-handling function.

There is one approach that could be used, but it involves creating a dozen of client script ("onChange" for every field). If you really need to go with OOTB, please let me know and I'll explain the approach in detail.

 

Please let us know if this was helpful or you need any further assistance.