Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to make variable mandatory upon closing SCTASK

Al-jhon
Kilo Sage

Hello All,

Currently we ar using this script to make a variable mandatory upon closing SCTASK.


function onSubmit() {


//Type appropriate comment here, and begin script below
if(g_form.getValue('state') == 3){//Closed Complete
if((g_form.getValue('what_needs_to_be_changed') == 'Name') && (g_form.getValue('short_description') == 'Update Account Details: Fulfill Request')){//to make this variable mandatory when only this task closes


g_form.setMandatory('new_userid', true); //set the variable for new user form mandatory
var newUserid = g_form.getValue('new_userid');
var newID = newUserid.trim();
g_form.setValue('new_userid', newID);

g_form.setMandatory('new_cn', true); //set the variable for new user form mandatory
var newCN = g_form.getValue('new_cn');
var newCN2 = newCN.trim();
g_form.setValue('new_cn', newCN2);


if(g_form.getValue('new_userid') == '' || ('new_cn') == ''){ //if new user form is empty or blank it will give error message
alert('Please input the New Userid and CN');
return false;
}
}
}
}


but we have a problem on the short description, if the requestor/opened by user language is not english the script will return false since we set it in english. 


If i will set the RITM "stage" as condition, how would i do that?

tried to change the line here to:

if((g_form.getValue('what_needs_to_be_changed') == 'Name') && (g_form.getValue('stage') == 'Fulfillment')){//to make this variable mandatory when only this task closes

 

but didn't work 😞


Please help ^__^

2 ACCEPTED SOLUTIONS

Ahmmed Ali
Giga Sage
Giga Sage

Hello,

 

Are the fields new_userid, new_cn and what_needs_to_be_changed the catalog task table fields for catalog item variables. I think those are catalog item variables and if yes, then you need to read their value as g_form.getValue("variables.VARIABLE_NAME"). ex: g_form.getValue("variables.new_userid").

 

Regarding short description language issue, you can create a display business rule on SCTASK table and add requested item's stage value to the g_scratchpad. Then in your client script, you can use same g_scratchpad value in the condition.

 

This video provides details example about g_scratchpad usage How To Use g_scratchpad in ServiceNow | ServiceNow Training Videos - YouTube

 

Thank you,

Ali

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

View solution in original post

Hello @Al-jhon 

 

Couple of updates needed on client script:

1. uncheck the "Applies on requested item" field

2. in script include, you are reading request_item sys_id with parameter as sysparm_item. so in client script you need to pass value in that parameter. Update line 8 of client script as below:

gStage.addParam("sysparm_item",g_form.getValue("request_item"));

 

Thank you,

Ali

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

View solution in original post

9 REPLIES 9

Hello @Ahmmed Ali , im trying to create script include and the data passing is null.

can you please take a look on my codes and check?

Script Includes:

Aljhon_0-1666775590730.png


Catalog client script:

Aljhon_1-1666775651037.png

the addinfomessage gives me null value.



Hello @Al-jhon 

 

Couple of updates needed on client script:

1. uncheck the "Applies on requested item" field

2. in script include, you are reading request_item sys_id with parameter as sysparm_item. so in client script you need to pass value in that parameter. Update line 8 of client script as below:

gStage.addParam("sysparm_item",g_form.getValue("request_item"));

 

Thank you,

Ali

If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali

@Ahmmed Ali you're the man! 🙂 Thank you so much!

Hello @Ahmmed Ali 


I have a little problem with my recent code:

Aljhon_0-1668015848909.png


the return false; is not working. Closing task still proceeds but it makes the variables mandatory and the alert displays. 

where should i put the return false; line to avoid closing the SCTASK?

Hello @Ankur Bawiskar 

Do you have solution for this?

I can't prevent form to submit with GlideAjax. check my code....



function onSubmit() {
//Type appropriate comment here, and begin script below




var gStage = new GlideAjax('VSHMandatoryVariablesAJAX');

gStage.addParam('sysparm_name', 'getStage');
gStage.addParam('sysparm_item', g_form.getValue('request_item'));
gStage.getXML(_response);



function _response(response){
var answer = response.responseXML.documentElement.getAttribute('answer');
alert(answer);


if(g_form.getValue('state') == 3){//close complete

if((g_form.getValue('what_needs_to_be_changed') == 'Name') && (answer == 'fulfillment')){
if((g_form.getValue('new_userid') == "") && ('new_cn') == ""){//if new user form is empty or blank it will give error message

g_form.setMandatory('new_userid', true); //set the variable for new user form mandatory
g_form.setMandatory('new_cn', true); //set the variable for new user form mandatory

alert('Please input the New Userid and CN');
return false;

}
}
}
}
}