- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2022 11:33 PM
Hello Experts,
I have a catalog item, When i submit a request ten it should create a user record in a custom table.
the requirement is not to create a duplicate user in that custom table, So i have written a script that should throw an error message comparing the first name and the last name from table and catalog item.
Now i need help about after approval of RITM then the record insert in the custom table, So different users should not able to create RITM's for same user.
EX : i have submitted a request for a user through the catalog item and it is ready for approval, Then again my colleauge/manager trying to submit a request for the same user. Then it should get restrict saying that an request has been already raised for this user.
Please help ASAP.
Looping
Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-22-2022 09:08 AM
Hi,
yes in script include function add this query
So here is your final code
Script Include:
var check_for_users = Class.create();
check_for_users.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDetails: function() {
var firstName = this.getParameter('sysparm_old_first_name');
var lastName = this.getParameter('sysparm_old_last_name');
var catItemSysId = this.getParameter('sysparm_catItemSysId');
var che = "";
var oldUser = new GlideRecord('u_contractors_onboarding');
oldUser.addQuery('u_first_name', firstName);
oldUser.addQuery('u_last_name', lastName);
oldUser.query();
if (oldUser.hasNext())
che = "exist";
var found = this.checkExistingRITM(firstName,lastName,catItemSysId);
if(che == 'exist' || found == 'exist'){
return 'exist';
}
return '';
},
checkExistingRITM: function(firstName,lastName,catItemSysId) {
var found = 'not exist';
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('cat_item', catItemSysId);
ritm.addEncodedQuery('stateIN-5,1,2'); // work in progress or open or Pending
ritm.query();
while(ritm.next()){
if(ritm.variables.u_first_name == firstName && ritm.variables.u_last_name == lastName){
found = 'exist';
break;
}
}
return found;
},
type: 'check_for_users'
});
onSubmit:
function onSubmit() {
if (!g_form.ajaxComplete) {
ajaxCall();
return false;
}
function ajaxCall(){
//Type appropriate comment here, and begin script below
var gr = new GlideAjax("check_for_users");
gr.addParam("sysparm_name", "getUserDetails");
gr.addParam("sysparm_old_first_name", g_form.getValue('u_first_name'));
gr.addParam("sysparm_old_last_name", g_form.getValue('u_last_name'));
gr.addParam("sysparm_catItemSysId", g_form.getUniqueValue());
gr.getXML(ajaxResponse);
function ajaxResponse(answers) {
var answer = answers.responseXML.documentElement.getAttribute("answer");
if (answer == 'exist') {
g_form.addErrorMessage('The user is already available in Contractor onboarding table');
return false;
} else {
g_form.ajaxComplete = true;
g_form.submit();
}
}
}
}
Please mark my response as correct and helpful to close the thread
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-21-2022 11:51 PM
Hi
Thank you so much for your help and time on this.
Sorry to say, its not working as expected.
The onsubmit client script throwing an error and again it is getting submitted in the first case.
the second case if already a request raised for a user then when i try to submit another request it should say the user request is under progress, but after the same error msg again it get submitted.
if you want the sys_id of the item : 6ce667971b1d00144da721ff6e4bcba0
Please help me to resolve this.
Once again thank you so much
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-22-2022 12:37 AM
Hi,
I just merged both the logic in single onSubmit
1st function checks 1st part and calls 2nd function to check 2nd part
based on that it would return 'exist' or empty string
did you check what came in alert?
Also check this link for solution to Aysnchronous
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-22-2022 12:39 AM
update onSubmit as this
function onSubmit() {
if (!g_form.ajaxComplete) {
ajaxCall();
return false;
}
function ajaxCall(){
//Type appropriate comment here, and begin script below
var gr = new GlideAjax("check_for_users");
gr.addParam("sysparm_name", "getUserDetails");
gr.addParam("sysparm_old_first_name", g_form.getValue('u_first_name'));
gr.addParam("sysparm_old_last_name", g_form.getValue('u_last_name'));
gr.addParam("sysparm_catItemSysId", g_form.getUniqueValue());
gr.getXML(ajaxResponse);
function ajaxResponse(answers) {
var answer = answers.responseXML.documentElement.getAttribute("answer");
if (answer == 'exist') {
g_form.addErrorMessage('The user is already available in Contractor onboarding table');
return false;
} else {
g_form.ajaxComplete = true;
g_form.submit();
}
}
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-22-2022 09:01 AM
Hi
Its working super fine. But a small validation can we keep in the code like :
When already request submitted on that and that request should be in state of work in progress, if it is already completed then it is ok. but can we check in the code like is the ritm in work in progress or waiting for approval, That only can pick and check that already an ritm created and waiting for approval.
Thanks in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-22-2022 09:08 AM
Hi,
yes in script include function add this query
So here is your final code
Script Include:
var check_for_users = Class.create();
check_for_users.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDetails: function() {
var firstName = this.getParameter('sysparm_old_first_name');
var lastName = this.getParameter('sysparm_old_last_name');
var catItemSysId = this.getParameter('sysparm_catItemSysId');
var che = "";
var oldUser = new GlideRecord('u_contractors_onboarding');
oldUser.addQuery('u_first_name', firstName);
oldUser.addQuery('u_last_name', lastName);
oldUser.query();
if (oldUser.hasNext())
che = "exist";
var found = this.checkExistingRITM(firstName,lastName,catItemSysId);
if(che == 'exist' || found == 'exist'){
return 'exist';
}
return '';
},
checkExistingRITM: function(firstName,lastName,catItemSysId) {
var found = 'not exist';
var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('cat_item', catItemSysId);
ritm.addEncodedQuery('stateIN-5,1,2'); // work in progress or open or Pending
ritm.query();
while(ritm.next()){
if(ritm.variables.u_first_name == firstName && ritm.variables.u_last_name == lastName){
found = 'exist';
break;
}
}
return found;
},
type: 'check_for_users'
});
onSubmit:
function onSubmit() {
if (!g_form.ajaxComplete) {
ajaxCall();
return false;
}
function ajaxCall(){
//Type appropriate comment here, and begin script below
var gr = new GlideAjax("check_for_users");
gr.addParam("sysparm_name", "getUserDetails");
gr.addParam("sysparm_old_first_name", g_form.getValue('u_first_name'));
gr.addParam("sysparm_old_last_name", g_form.getValue('u_last_name'));
gr.addParam("sysparm_catItemSysId", g_form.getUniqueValue());
gr.getXML(ajaxResponse);
function ajaxResponse(answers) {
var answer = answers.responseXML.documentElement.getAttribute("answer");
if (answer == 'exist') {
g_form.addErrorMessage('The user is already available in Contractor onboarding table');
return false;
} else {
g_form.ajaxComplete = true;
g_form.submit();
}
}
}
}
Please mark my response as correct and helpful to close the thread
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader