- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2020 08:23 AM
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:
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2020 12:02 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2020 12:02 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2020 12:24 PM
Brad,
Thank you! That works!
I am trying to compare it to my original onChange attempt. One of the big differences I see is I was attempting to use a Call Back function. Do you think that might have been part of the problem? Or do you think it was something else?
Just curious, so I can avoid these issues down the road.
Thanks again! This had been driving me crazy for two days!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2020 12:32 PM
I've only used getReference with a callback function to populate another variable based on the value I just selected, so I'm not sure why it would cause problems, but it's also not something that is needed in this case.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2020 11:24 AM
Hi ,
Here are links which will help you.
If it helps,Please mark ✅ Correct and 👍 Helpful.
Warm Regards,
Milind
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2020 11:51 AM
I had already come across those, and have been trying to incorporate those concepts into my attempts, and none have worked (I read lots of old posts, trying to find ones what fit my situations, and that is how I came up with the two code blocks I posted).
However, there are two main points in which my problem differs from most of what is contained in those replies:
1. These fields I am trying to make mandatory on the task are Catalog Item Variables that I am showing on my Task. They are NOT fields from the sc_task or sc_req_item tables.
2. I only want to apply these rules to this particular Task. I don't want a Script or Policy that affects all sc_task forms or applies to all the "Close Task" buttons.
So if you look at the two script attempts I posted, you can see that I am using a lot of the logic applied in those and other past articles I found, but I am trying to limit it to that particular Catalog Item or tasks with a certain Short Description.
I figure that must be something wrong in my logic somewhere, but I cannot seem to find what.