Client Script Requiring Mandatory Fields upon closing not working

jmiskey
Kilo Sage

I have a process where a Catalog Item is submitted via the Service Portal, Approvals and Tasks are created via the Workflow attached to the Catalog Item.  I have run into an issue, that I cannot seem to figure out.

A Catalog Task gets created, and initially, there are only a few required fields that need to be populated.  However, I have a handful of variables from the Catalog Item that MUST be populated before the Task is closed.  Prior to closing, these fields should NOT be mandatory, as they may be Assigning the Task to someone initially without populating all the fields.

So, I was trying to use an onSubmit Client Script, like Pradeep Sharma showed here: https://community.servicenow.com/community?id=community_question&sys_id=703d4165dbab5f04fff8a345ca96...

First, here is what the upper section of my Client Script looks like:

find_real_file.png

 

amd here is what my code looks like:

function onSubmit() {
	var action = g_form.getActionName();
	var shortDescription = g_form.getValue('short_description');
	var st = g_form.getValue('state');
	if(((st == '3') || (st == '4')) && shortDescription.indexOf("Assign Risk Criteria") != -1)
	{
		g_form.setMandatory('variables.approved_expiration_date', true);
		g_form.setMandatory('variables.impact', true);
		g_form.setMandatory('variables.likelihood', true);
		g_form.setMandatory('variables.control_effectiveness', true);
		return false;
	}
}

However, it does not seem to work as needed.  Here are some of the issues:

1. If the 4 mandatory fields listed in the code are not all populated when they try to close the task, it does not update the task, however it does NOT return the message I have in the "Messages" box of my Client Script, and it shows the State as "Closed Complete", even though it hasn't saved it.  How do I make it pop-up the message on the screen, and revert the State back to what it was showing (since the record is not being saved).

2. If I populate all Mandatory fields shown above, and try to close my task, it does not close (no fields actually get updated).

What do I need to do in my code to ensure that:

1. If all Mandatory fields are populated, it closes the Task

2. If all Mandatory fields are NOT populated, it does not close the Task, and it reverts the State field back to the previous value, and it alerts the user that they are missing Mandatory fields?

Thanks 

 

1 ACCEPTED SOLUTION

This is your script that matches my logic/format.  I didn't think it mattered since you were getting the alert, but maybe the getReference is throwing it off. 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
 if (isLoading || newValue == '') {
  return;
 }

 if(newValue == 3 || newValue == 4 ){ // State = Closed Complete or Closed Incomplete 
  if(g_form.getValue('request_item.cat_item') == '90a3f78ddba58c10bc827afc0f961969'){ // Exception Request Form
   if(g_form.getValue('short_description') == 'Assign Risk Criteria'){
    alert("Script running");
    g_form.setMandatory('approved_expiration_date', true);
    g_form.setMandatory('impact', true);
    g_form.setMandatory('likelihood', true);
    g_form.setMandatory('control_effectiveness', true);
   }
  }
 }
}

This is assuming these 4 variables are on the task, and the values exactly match the Name of the variables (case-sensitive).  Are you viewing the task through the native UI and manually changing the State to Closed Complete, or doing something else?  Another thing to look for is any other client scripts UI policies, catalog client scripts, or catalog UI policies that might be interfering with this one.

View solution in original post

11 REPLIES 11

Brad Bowman
Kilo Patron
Kilo Patron

We use a script like this to make some variables mandatory before a task is closed.  Ours is an onChange client script when State changes, and it works well.  I think we first tried onSubmit and it didn't work as expected, with some of what you've experienced.  Get rid of the var action and return false lines as those aren't doing anything helpful.  If you want to display a message in addition to the standard one that will display when a save/update is attempted and a mandatory field is not populated, then in addition to the onChange script you can use an onSubmit script like you have, but within the if block add lines like

if(g_form.getValue('impact') == ''){ 
 g_form.showFieldMsg('approved_expiration_date', 'Expiration date must be populated in order to close task', 'error')
}

For each variable to show the message below the variable if that variable is not populated.

I initially tried an onChange script running on the State field, and couldn't get that to work right either.  It allowed me to close the records without populating the required fields.

Here is what that script attempt looks like:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}

	g_form.getReference('request_item', doAlert); // doAlert is our callback function

	function doAlert(reqItem) { // reference is passed into callback as first arguments
		var itemId = reqItem.cat_item;
		//first check to see if this is a "Exception Request Form" Catalog Item
		if(itemId == '90a3f78ddba58c10bc827afc0f961969'){	
			alert("Script running");
			g_form.setMandatory('variables.approved_expiration_date', true);
			g_form.setMandatory('variables.impact', true);
			g_form.setMandatory('variables.likelihood', true);
			g_form.setMandatory('variables.control_effectiveness', true);
		}

	}
}

It returned the alert I have in the code, so I know it is hitting that section.  But it doesn't appear to be doing anything to make those fields required.

What am I missing in my code?

When setting a field or variable as mandatory, just use

g_form.setMandatory('approved_expiration_date', true);

That does not seem to make any difference.  It still behaves the exact same way.