- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2025 04:06 AM
Hi Team,
I have a catalog item with an onSubmit client script that enforces some conditions. Currently:
The script enters the if block and shows the error message as expected.
However, when the conditions are not matched, the form does not submit as intended.
I have written the following script, but it’s not working properly. Could you please review and help fix it?
Thank you in advance for your support!
Thank you
Siva
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2025 02:36 PM - edited 12-22-2025 02:51 PM
Hi @Siva82 ,
I tried to replicate the your use case in PDI (async + onsubmit catalog script)
1) Created a simple client Ajax script as shown below:
2) Used your similar script but unfortunately it never submitted as you pointed out in your script too
var allowSubmit = false;
function onSubmit() {
// SECOND submit → allow
if (allowSubmit) {
return true;
}
var ga = new GlideAjax('IncidentAssignmentCheck');
ga.addParam('sysparm_name', 'getIncidentCount');
ga.addParam('sysparm_user', g_user.userID);
ga.getXML(function(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
var count = parseInt(answer, 10) || 0;
if (count > 5) {
alert(
'You have ' + count +
' active incident(s) already created. Please wait before creating another one.'
);
return; // ❌ stop, do NOT submit
}
// ✅ allow submit
allowSubmit = true;
g_form.submit(g_form.getActionName());
});
// FIRST submit → always stop
return false;
}
3) After reading articles, I modified the my above script to use g_scratchpad and call g_form.submit(g_form.getActionName())
function onSubmit() {
if (g_scratchpad.allowSubmit) {
return true;
}
var ga = new GlideAjax('IncidentAssignmentCheck');
ga.addParam('sysparm_name', 'getIncidentCount');
ga.addParam('sysparm_user', g_user.userID);
ga.getXML(function(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
var count = parseInt(answer, 10) || 0;
if (count > 5) {
alert(
'You have ' + count +
' active incident(s) already created. Please wait before creating another one.'
);
return; // ✅ STOP here
}
alert('You have ' + count + 'only active accident , your request will be submitted');
g_scratchpad.allowSubmit = true;
g_form.submit(g_form.getActionName());
});
return false;
}
4) Result verification: for if block condition
5) Result verification: outside if condition and it submits
Here is the Thread I referred :
I modified your code you posted, you can try this or make changes as per your use-case
function onSubmit() {
// allow second submit
if (g_scratchpad.allowSubmit) {
return true;
}
var locationSysId = g_form.getValue('current_work_location');
var accessory = (g_form.getDisplayValue('choose_accessory') || '').toLowerCase();
var hasYubikey = accessory.includes('yubikey');
alert(hasYubikey);
var ga = new GlideAjax('LocationSubmitValidator');
ga.addParam('sysparm_name', 'getLocationData');
ga.addParam('sysparm_location_id', locationSysId);
ga.getXMLAnswer(function(answer) {
var data;
try {
data = answer ? JSON.parse(answer) : null;
} catch (e) {
data = null;
}
// If no data, allow
if (!data) {
g_scratchpad.allowSubmit = true;
g_form.submit(g_form.getActionName());
return;
}
// Block condition
if (
data.name && data.name.toLowerCase().includes('remote') &&
(data.onsiteSupport == 0 || data.onsiteSupport === '0') &&
(data.hardwareSelfService == 0 || data.hardwareSelfService === '0') &&
!hasYubikey
) {
g_form.addErrorMessage(
'For Remote locations without onsite or hardware self-service support, a Yubikey request is mandatory.'
);
return;
}
// Allow submit
g_scratchpad.allowSubmit = true;
g_form.submit(g_form.getActionName());
});
return false; // always block original submit until callback finishes
}
If you find this answer useful, please mark it as helpful and solution accepted.
Thanks and Regards,
Mohammed Zakir
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2025 05:47 AM
Hi @Siva82
I'm assuming since the error message is displayed, it confirms that the script is entering the if condition and the GlideAjax call is working as expected. Why not put the highlighted code in screenshot above use else condition after if and check. not sure it will work but give it a try
Regrads,
Mohammed Zakir
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2025 02:36 PM - edited 12-22-2025 02:51 PM
Hi @Siva82 ,
I tried to replicate the your use case in PDI (async + onsubmit catalog script)
1) Created a simple client Ajax script as shown below:
2) Used your similar script but unfortunately it never submitted as you pointed out in your script too
var allowSubmit = false;
function onSubmit() {
// SECOND submit → allow
if (allowSubmit) {
return true;
}
var ga = new GlideAjax('IncidentAssignmentCheck');
ga.addParam('sysparm_name', 'getIncidentCount');
ga.addParam('sysparm_user', g_user.userID);
ga.getXML(function(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
var count = parseInt(answer, 10) || 0;
if (count > 5) {
alert(
'You have ' + count +
' active incident(s) already created. Please wait before creating another one.'
);
return; // ❌ stop, do NOT submit
}
// ✅ allow submit
allowSubmit = true;
g_form.submit(g_form.getActionName());
});
// FIRST submit → always stop
return false;
}
3) After reading articles, I modified the my above script to use g_scratchpad and call g_form.submit(g_form.getActionName())
function onSubmit() {
if (g_scratchpad.allowSubmit) {
return true;
}
var ga = new GlideAjax('IncidentAssignmentCheck');
ga.addParam('sysparm_name', 'getIncidentCount');
ga.addParam('sysparm_user', g_user.userID);
ga.getXML(function(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
var count = parseInt(answer, 10) || 0;
if (count > 5) {
alert(
'You have ' + count +
' active incident(s) already created. Please wait before creating another one.'
);
return; // ✅ STOP here
}
alert('You have ' + count + 'only active accident , your request will be submitted');
g_scratchpad.allowSubmit = true;
g_form.submit(g_form.getActionName());
});
return false;
}
4) Result verification: for if block condition
5) Result verification: outside if condition and it submits
Here is the Thread I referred :
I modified your code you posted, you can try this or make changes as per your use-case
function onSubmit() {
// allow second submit
if (g_scratchpad.allowSubmit) {
return true;
}
var locationSysId = g_form.getValue('current_work_location');
var accessory = (g_form.getDisplayValue('choose_accessory') || '').toLowerCase();
var hasYubikey = accessory.includes('yubikey');
alert(hasYubikey);
var ga = new GlideAjax('LocationSubmitValidator');
ga.addParam('sysparm_name', 'getLocationData');
ga.addParam('sysparm_location_id', locationSysId);
ga.getXMLAnswer(function(answer) {
var data;
try {
data = answer ? JSON.parse(answer) : null;
} catch (e) {
data = null;
}
// If no data, allow
if (!data) {
g_scratchpad.allowSubmit = true;
g_form.submit(g_form.getActionName());
return;
}
// Block condition
if (
data.name && data.name.toLowerCase().includes('remote') &&
(data.onsiteSupport == 0 || data.onsiteSupport === '0') &&
(data.hardwareSelfService == 0 || data.hardwareSelfService === '0') &&
!hasYubikey
) {
g_form.addErrorMessage(
'For Remote locations without onsite or hardware self-service support, a Yubikey request is mandatory.'
);
return;
}
// Allow submit
g_scratchpad.allowSubmit = true;
g_form.submit(g_form.getActionName());
});
return false; // always block original submit until callback finishes
}
If you find this answer useful, please mark it as helpful and solution accepted.
Thanks and Regards,
Mohammed Zakir
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2025 10:43 PM
Hi @Mohammed8
Great, It's work. Thank you much for help
Thank you
Siva
