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

Jahnavi7
Mega Contributor

Hi Jmiskey,

 

Do you find out the solution?

If you find out can you help me?

Yes, I used the solution that is the "Accepted Solution".

It is a Client Script on the Catalog Task table that is set to run on the change of the State field.

So the top half of that Cliene Script looks like:

find_real_file.png