- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2023 12:24 PM
Hello All,
I want to Abort(not let record submit) record submission through Record producer on Incident table if the selected
Configuration Item already exists with a particular incident. I have gone through same issues here but none of them were helpful. I have tried both catalog client onChange and onSubmit sctipt but none to avail.
Catalog onChange Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('global.checkCIExistIncident');
ga.addParam('sysparm_name', 'isCIAvailable');
ga.addParam('sysparm_ci',newValue);
//alert(g_form.getValue('cmdb_ci'));
//Tried only ga.getXML(submitRecord)
var myanswer = ga.getXML(submitRecord);
return myanswer;
function submitRecord(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
return answer;
}
}
Script include:
var checkCIExistIncident = Class.create();
checkCIExistIncident.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isCIAvailable: function() {
var incGr = new GlideRecord('incident');
incGr.addQuery('cmdb_ci',this.getParameter('sysparm_ci'));
incGr.query();
gs.log('inside the ci check si');
if(incGr.next()){
return true;
}else{
return false;
}
},
type: 'checkCIExistIncident'
});
Any input in this regard will be greatly appreciated.
Thank you,
Mahesh.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2023 12:38 AM
Hi @maheshkhatal ,
I have tried simulating your issue in my PDI today and finally found the working code for your issue. Please try with the following Script Include and Client Script.
Script Include:
var checkCIExistIncident = Class.create();
checkCIExistIncident.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isCIAvailable: function() {
gs.info("MAK: Hello");
var incGr = new GlideRecord('incident');
incGr.addQuery('cmdb_ci', this.getParameter('sysparm_ci'));
incGr.addQuery('active', true);
incGr.query();
if (incGr.next()) {
return 'true';
} else {
return 'false';
}
},
type: 'checkCIExistIncident'
});
Client Script:
function onSubmit() {
if (g_scratchpad.isFormValidAjax) {
g_scratchpad.isFormValidAjax = null;
return true;
}
g_scratchpad.isFormValidAjax = false;
var cmdb_ci = g_form.getValue('cmdb_ci'); //change variable as per your configuration
var ga = new GlideAjax('global.checkCIExistIncident');
ga.addParam('sysparm_name', 'isCIAvailable');
ga.addParam('sysparm_ci', cmdb_ci);
ga.getXMLAnswer(setAnswer);
return false;
function setAnswer(answer) {
if (answer) {
if (answer == 'true') {
g_form.addErrorMessage("There is an Incidnet for this CI");
return false;
} else {
var actionName = g_form.getActionName();
g_scratchpad.isFormValidAjax = true;
g_form.submit(actionName);
}
}
}
}
Please mark my answer helpful and accept as solution if it worked for you 👍✔️
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2023 07:58 PM
Hi @maheshkhatal ,
To prevent form submission you can use onSubmit client script, like the one below.
onSubmit Client Script:
function onSubmit(){
var cmdb_ci = g_form.getValue('cmdb_ci'); //change variable as per your configuration
var ga = new GlideAjax('global.checkCIExistIncident');
ga.addParam('sysparm_name', 'isCIAvailable');
ga.addParam('sysparm_ci',newValue);
var myanswer = ga.getXML(submitRecordProc);
function submitRecordProc(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'false'){
return false;
}else{
return true;
}
}
}
And slightly change your Script Include to the one like below.
var checkCIExistIncident = Class.create();
checkCIExistIncident.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isCIAvailable: function() {
var incGr = new GlideRecord('incident');
incGr.addQuery('cmdb_ci',this.getParameter('sysparm_ci'));
incGr.addQuery('active', true);
incGr.query();
gs.log('inside the ci check si');
if(incGr.next()){
return 'true';
}else{
return 'false';
}
},
type: 'checkCIExistIncident'
});
Please let me know if you need any further help!
Please mark my answer helpful and accept as solution if it helped you 👍✅
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2023 04:21 AM
I tried as suggested by you it still doesn't work when I submit. Thanks for giving suggestions though.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2023 04:57 AM - edited 09-25-2023 04:58 AM
@maheshkhatal my bad, I made a typo in my client script. Use the below client script and check once.
We should use getXMLWait in onSubmit client script, getXML and getXMLAnswer will not work as they are asynchronous.
function onSubmit(){
var cmdb_ci = g_form.getValue('cmdb_ci'); //change variable as per your configuration
var ga = new GlideAjax('global.checkCIExistIncident');
ga.addParam('sysparm_name', 'isCIAvailable');
ga.addParam('sysparm_ci',cmdb_ci);
ga.getXMLWait();
answer = ga.getAnswer(); //getAnswer() retrieves the result for you
if(answer == 'true') {
return true;
}else{
return false;
}
}
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2023 06:07 AM
Hello Anvesh, answer variable is not defined- getting that error. I tried Anand Kumar's suggestion and it worked for me. If you still have suggestions to make it work please do suggest I shall explore them. Thank you again.