Populate all RITM numbers of selected user in catalog item.

Sai175
Kilo Contributor

Hi,

I have two catalog items,

New Hire - with two variables (requested for (reference) , Model (reference)

find_real_file.png

Another catalog item,

Leaver - with variables(requested for, Request's related to user)

when requested for is selected, all RITM's of the requested user should be populated in another field.

find_real_file.png

Question:

1. what variable type is suggested to populate all RITM's of user.

2. How to achieve this.

Please help me in this.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you need to check the variable value for request on behalf of and not requested_for field

If you just want to display the RITMs then you can use single line text variable and populate the values as comma separated numbers

Use onChange client script + GlideAjax to set the variable present on Leaver catalog item

UI Type - ALL

Applies to Catalog Item - True

Variable - Requested on behalf of

Script:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading) {
		return;
	}

	if(newValue == '')
		g_form.clearValue('variableName');

	if(oldValue != newValue){
		var ga = new GlideAjax('getMyRequests');
		ga.addParam('sysparm_name', "checkRecordPresent");
		ga.addParam('sysparm_user', newValue);
		ga.getXMLAnswer(function(answer){
			if(answer != ''){
				g_form.setValue('variableName',answer);
			}
		});
		//Type appropriate comment here, and begin script below
	}
}

Script Include:

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

	getRequestItems: function(){

		var user = this.getParameter('sysparm_user');
		var arr = [];
		var gr = new GlideRecord("sc_req_item");
		gr.addQuery("cat_item.name", "New Hire"); // name of your catalog item
		gr.query();
		while (gr.next()) {
			if(gr.variables.requested_on_behalf_of == user)
				arr.push(gr.getUniqueValue());
		}

		return arr.toString();
	},

	type: 'getMyRequests'
});

Regards
Ankur

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

View solution in original post

13 REPLIES 13

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Sai 

1 ) You have to use the reference field , Reference to the sc_request table.

2) To achieve this you can write down below reference qualifier in your 'Request's related user' field:  

 

javascript: 'requested_for='  +current.variables.request_on_behalf_of; 

 

Please make corrections to the variable name. instead of 'request_on_behalf_of' write down your own variable name for 1st variable.

 

Please mark my answer as correct/helpful based on impact.

 

Regards,

Gunjan Kiratkar


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Wayne Richmond
Tera Guru

A list collector would be a good as @Aman Kumar suggested - that way you are actually referencing the records. 

You could also just use a text box and separate the reference numbers with commas: RITM0012345, RITM0012346, RITM0012347 etc.

With the latter solution, you'd still need to do a Glide Record lookup to get your list and then push it to the text box.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you need to check the variable value for request on behalf of and not requested_for field

If you just want to display the RITMs then you can use single line text variable and populate the values as comma separated numbers

Use onChange client script + GlideAjax to set the variable present on Leaver catalog item

UI Type - ALL

Applies to Catalog Item - True

Variable - Requested on behalf of

Script:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading) {
		return;
	}

	if(newValue == '')
		g_form.clearValue('variableName');

	if(oldValue != newValue){
		var ga = new GlideAjax('getMyRequests');
		ga.addParam('sysparm_name', "checkRecordPresent");
		ga.addParam('sysparm_user', newValue);
		ga.getXMLAnswer(function(answer){
			if(answer != ''){
				g_form.setValue('variableName',answer);
			}
		});
		//Type appropriate comment here, and begin script below
	}
}

Script Include:

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

	getRequestItems: function(){

		var user = this.getParameter('sysparm_user');
		var arr = [];
		var gr = new GlideRecord("sc_req_item");
		gr.addQuery("cat_item.name", "New Hire"); // name of your catalog item
		gr.query();
		while (gr.next()) {
			if(gr.variables.requested_on_behalf_of == user)
				arr.push(gr.getUniqueValue());
		}

		return arr.toString();
	},

	type: 'getMyRequests'
});

Regards
Ankur

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

Hi @Ankur Bawiskar,

tried the scripts, but RITM's are not populating as expected.

find_real_file.png

find_real_file.png

find_real_file.png

is there anything wrong in script,

thanks.

Hi,

you are calling wrong function name in GlideAjax

it should be getRequestItems and not checkRecordPresent at line 11 in client script

are you using correct variable name to compare at line 13?

that variable should be from New Hire catalog item

Regards
Ankur

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