Catalog client script onSubmit to Check if a variable exist in variable ownership table.

Arnaud5
Tera Contributor

Dear Developers,

I have a variable in a variable set which is visible only on task and which is used in a switch in the workflow.

I need to write a catalog client script onSubmit on this variable with this condition :
- if the variable exist in variable ownership table ( table: sc_item_option_mtom) make it mandatory.
I think I need to use glideajax.

Please suggest what code i have to write , so that i can achieve this one.

Thanks in advance.

3 REPLIES 3

Omkar Mone
Mega Sage

Hi 

Why do you want it onSubmit? Async calls wont work in onSubmit client script if you are thinking to make use of GlideAjax. If you want to validate that, you can check it onLoad from where you can make an Ajax call using script include.

 

Let me know the use case as well.

 

Regards,

Omkar Mone

Arnaud5
Tera Contributor

Hi Omkar,

First, I want to check if the variable exist on variable ownership table. In that case, I want to force the user to fill the variable on the task before submetting. 

Hello

Script include will help you here.

I have something similar where check if the CI entered by the user is already in the system

Hope it helps

Client script

function onSubmit() {
var name = g_form.getValue('ci_name').toString();

var ou = g_form.getValue('domain_name');

var cnf = name.split(',');
for (var i=0; i < cnf.length; i++)
{
var ciname = cnf[i];
var ciname1 = ciname.trim();
var name1 = ciname1 +"."+ou;

if (name1 != '')
{

var gr = new GlideAjax('GetCI_VM');
gr.addParam('sysparm_name', 'GetCI');
gr.addParam('sysparm_ci_names', name1);
// gr.addParam('sysparm_ou',ou);
gr.getXMLWait();
var answer = gr.getAnswer();
var answerObject = JSON.parse(answer);
if (answerObject !='') {
swal({
title: 'Error',
html: true,
width:'75px',
text: "The Server name you used :- " + " " + answerObject + " " + "already exist in CMDB",
confirmButtonText: 'Close',
});
return false;
}
}
else {
return true;
}
}
}

 

Script include

====

GetCI: function() {
var answer;
var existingCIS = [];
var returnInfo = {};
var curci = this.getParameter('sysparm_ci_names');
var cinm = new GlideRecord('cmdb_ci');
cinm.addQuery('name', curci);
cinm.addEncodedQuery('install_statusNOT IN3,12,13,14,15');
cinm.query();

while (cinm.next()) {

existingCIS.push(cinm.name.toString());
}

if (existingCIS.length == 0 ) {
returnInfo.error_message = "NoError";
returnInfo.ci_names = existingCIS;

}
else {
returnInfo.error_message = "Error";
returnInfo.ci_names = existingCIS.join(",");

}
return JSON.stringify(existingCIS);

}
//type: 'GetCI_VM'


});

 

 

 

NOTE :- My code work mutliple value separate by commas.. You can change the code if you want for only one. You can exclude Jason and  return yes/no as answer