Restrict in RITM level

Chaithu3
Tera Contributor

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 @Ankur Bawiskar @Allen Andreas @Dan H @Mark Roethof 

 

Thanks in advance

 

 

1 ACCEPTED SOLUTION

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

16 REPLIES 16

the solution is already provided in the links I shared.

You need to enhance it as per your script

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi @Ankur Bawiskar ,

 

Ill paste the code for you, I have tried still not working. Can you please dont mind modifying it and help me with the 2nd point as well.

 

Script include :

 

var check_for_users = Class.create();
check_for_users.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDetails: function() {
var oldUser = new GlideRecord('u_contractors_onboarding');
oldUser.addQuery('u_first_name', this.getParameter('sysparm_old_first_name'));
oldUser.addQuery('u_last_name', this.getParameter('sysparm_old_last_name'));
oldUser.query();
if (oldUser.next())
var che = "exist";
return che;
},
type: 'check_for_users'


});

 

Client script :

 

function onSubmit() {
//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.getXML(ajaxResponse);

function ajaxResponse(answers) {
var answer = answers.responseXML.documentElement.getAttribute("answer");
if (answer == 'exist' || answer == exist) {
g_form.addErrorMessage('The user is already available in Contractor onboarding table');
return false;
} else {
return true;
}

}

 

Please help me with the 2nd point as well, It should restrict after submit the request.

2. we should not submit 2 requests on the same user.

Ex : when i submit a request for a user to add in a custom table then he will get added into the table after approval. if i submit a request for a user to that table and without knowledge if any other of my colleauge/manager were trying to add the same user to the table through catalog item, then it throw an error saying "There is alrerady a request raised for this user".

 

Thanks in advance.

Hi,

You can try from your side; the logic is already shared in the links.

you just need to enhance it

OR

create 2 onChange client scripts 1 each on first name and last name

validate the data

if validation fails; then clear the variable and make it mandatory

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thanks @Ankur Bawiskar ,

 

The first point got completed, I was talking about the second point which is the major part of the question.

 

Please help me with the 2nd point as well, It should restrict after submit the request.

2. we should not submit 2 requests on the same user.

Ex : when i submit a request for a user to add in a custom table then he will get added into the table after approval. if i submit a request for a user to that table and without knowledge if any other of my colleauge/manager were trying to add the same user to the table through catalog item, then it throw an error saying "There is alrerady a request raised for this user".

 

Hi,

combine validation for both the points in same script include

Script Include for that

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.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() {
	//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 {
			return true;
		}
	}
}

I  hope this will help you to mark my response as correct and helpful to close the thread.

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader