Issue with Duplicate Request Validation in ServiceNow Catalog

SUGAGURU
Tera Contributor

Hi 

I have a requirement in an existing catalog item that contains three dropdown variables:

  1. Level
  2. Grade
  3. Stage

When a user submits a request, the system should validate these three variables along with the Requested For user’s location. It should check whether there is already an existing request with the same combination that is still open (not Closed Complete or Closed Incomplete).

 

If such a request exists, the submission should be aborted with a message like:
"A request already exists."

 

I have implemented a Client Script and used a Script Include to check for existing open requests. From the Script Include logs, I can see that it correctly identifies an existing request number. However, even after showing the alert, the request is still getting submitted.

 

Could you please help me understand what might be missing or how to properly stop the submission?

3 REPLIES 3

Huynh Loc
Mega Sage

Hi @SUGAGURU ,

Please refer to the following article for the recommended solution to prevent form submission when using GlideAjax in a client script:
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0779964


If this response was helpful, please consider marking it as Correct and Helpful. You may mark more than one reply as an accepted solution.

NatrajS93604625
Tera Expert

@SUGAGURU 

You can try with below logic.

Client Callable Script Include:

 

var Requestcheckforduplication = Class.create();
Requestcheckforduplication.prototype = Object.extendsObject(AbstractAjaxProcessor, {

requestCheck: function() {
var user = this.getParameter('sysparm_user_id');
var varValue = this.getParameter('sysparm_email'); // example variable

var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('requested_for', user);
ritm.query();

while (ritm.next()) {

var varGr = new GlideRecord('sc_item_option_mtom');
varGr.addQuery('request_item', ritm.sys_id);
varGr.query();

while (varGr.next()) {
if (varGr.sc_item_option.item_option_new.name == 'email' &&
varGr.sc_item_option.value == varValue && user.location == 'location value') //Here you can validate the user location and variable value combinations

{

return 'true';
}
}
}

return 'false';
}

});

Client Script:

var ga = new GlideAjax('Requestcheckforduplication');
ga.addParam('sysparm_name', 'requestCheck');
ga.addParam('sysparm_user_id', g_form.getValue('requested_for'));
ga.addParam('sysparm_email', g_form.getValue('email'));//Pass the catalog variable values here

ga.getXMLAnswer(function(answer) {
if (answer == 'true') {
alert('Duplicate request exists');
}
else

{

g_form.submit();

}

return false;
});

If this response was helpful, please consider marking it as Correct and Helpful. You may mark more than one reply as an accepted solution.

Tanushree Maiti
Tera Sage

Hi @SUGAGURU 

 

Same thing we have implemented in our instance for the new hire kit catalog.

 

We created OnSubmit catalog client script -  with script include.

 

Sample Code :

 

1.  catalog client script

 

function onSubmit() {

var requestedFor = g_form.getValue('requested_for_input');

var validReq = new GlideAjax('ValidationUtils');
validReq.addParam('sysparm_name', 'checkifRequestExists');
validReq.addParam('sysparm_user', requestedFor);
validReq.getXMLWait();

var answer = validReq.getAnswer();

if (answer === 'true') {
g_form.addErrorMessage(
'Note:Request has already been created. Please ensure duplicate requests for Laptops are not submitted.'
);
return false;
}

return true;
}

 

2. Script include

 

var ValidationUtils = Class.create();
ValidationUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

checkifRequestExists: function() {
var retval = false;
var requestedFor = this.getParameter('sysparm_user');
var catItem = gs.getProperty('<sys_id of catItem>'); //we needed sys property for our requirement , you pass it as param

var grReqItem = new GlideRecord('sc_task');
grReqItem.addEncodedQuery('active=true^request_item.requested_for=' + requestedFor + '^cat_item=' + catItem+ '^state!=4');  //add your required  state value , 4 is Closed incomplete for sc_task for us
grReqItem.query();

if (grReqItem.hasNext()) {
retval = true;
}

return retval;

},

type: 'ValidationUtils'
});

 

Note: You can refer it as well - 

Refer: https://www.servicenow.com/community/developer-articles/async-validation-in-onsubmit-catalog-client-...

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: