How to prevent a duplicate request be uploaded for user?

Magali Legaspi
Tera Contributor

Hi all!

 

I'm working on this catalog item. My requirement is to check that no requests have been uploaded for the same user before. I have the "requested_for" field which references to the users. I tried this script I found on the community but it's not working, I get an error message on my javascript console because of the getElementByID.

 

 

Script Include:

 

(It is Client Callable and accessible from all application scopes)

 

 

var AjaxFunctions = Class.create();
AjaxFunctions.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkUserRequest: function() {
var user = this.getParameter('sysparm_user');
var item = this.getParameter('sysparm_item');

var requests = [];
var ritm = new GlideRecord('sc_req_item');
ritm.addActiveQuery();
ritm.addQuery('request.requested_for', user);
ritm.addQuery('cat_item', item);
ritm.query();
while (ritm.next()) {
requests.push(String(ritm.number));
}
if (requests.length > 0)
return requests.join(', ');
return 'none';
},
type: 'AjaxFunctions'
});

 

 

Catalog Client Script:

 

function onChange(control, oldValue, newValue, isLoading) {

if (isLoading)
return;

g_form.hideFieldMsg('requested_for', true);

if (newValue == '')
return;
var ga = new GlideAjax('AjaxFunctions');
ga.addParam('sysparm_name', 'checkUserRequest');
ga.addParam('sysparm_user', newValue); // User
ga.addParam('sysparm_item', document.getElementById('sysparm_id').value); // Item
ga.getXMLAnswer(function(answer) {

if (answer && answer != 'none') {
var msg = 'This person already has an open Request(s) for this item: ' + answer;
g_form.showFieldMsg('requested_for', msg, 'error', false);
}
});
}

 

I'm using an OnChange script right now but it could be an 'OnSubmit' too. 

 

Any help will be very much appreciated!

1 ACCEPTED SOLUTION

jaheerhattiwale
Mega Sage
Mega Sage

@Magali Legaspi Please try replacing the client script code with below code:

 

Client script:

function onChange(control, oldValue, newValue, isLoading) {

if (isLoading)
return;

g_form.hideFieldMsg('requested_for', true);

if (newValue == '')
return;
var ga = new GlideAjax('AjaxFunctions');
ga.addParam('sysparm_name', 'checkUserRequest');
ga.addParam('sysparm_user', newValue); // User
ga.addParam('sysparm_item', g_form.getUniqueValue()); // Item
ga.getXMLAnswer(function(answer) {

if (answer && answer != 'none') {
var msg = 'This person already has an open Request(s) for this item: ' + answer;
g_form.showFieldMsg('requested_for', msg, 'error', false);
}
});
}

 

 

Please mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

2 REPLIES 2

jaheerhattiwale
Mega Sage
Mega Sage

@Magali Legaspi Please try replacing the client script code with below code:

 

Client script:

function onChange(control, oldValue, newValue, isLoading) {

if (isLoading)
return;

g_form.hideFieldMsg('requested_for', true);

if (newValue == '')
return;
var ga = new GlideAjax('AjaxFunctions');
ga.addParam('sysparm_name', 'checkUserRequest');
ga.addParam('sysparm_user', newValue); // User
ga.addParam('sysparm_item', g_form.getUniqueValue()); // Item
ga.getXMLAnswer(function(answer) {

if (answer && answer != 'none') {
var msg = 'This person already has an open Request(s) for this item: ' + answer;
g_form.showFieldMsg('requested_for', msg, 'error', false);
}
});
}

 

 

Please mark as correct answer if this solves your issue.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Thank you so much!! Now it's working perfect. Thanks for your help!