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

Aman Kumar S
Kilo Patron

Hi @Sai

You will need a list collector field for this purpose and your reference table will be Requested Item table, and for auto-populating associated RITMs with a user, you will need to write an onChange client script, using AJAX to fetch the RITMs and pass that on to be shown in the newly created list collector field.

Your encoded query should be:

"request.requested_for="+ sys_id of the user// sys_id you will pass from the client script

For reference to AJAX:

getXMLAnswer vs getXML

Best Regards
Aman Kumar

hi @Aman Kumar ,

tried script include but the RITM's are not filtered.

below is the script include,

find_real_file.png

find_real_file.png

what is wrong with this script, can you please help.

You will have to explicitly pass parameter from your client script to script include.

You can't pass functional parameters in AJAX, even in the reference link, the script example passes caller_id as additional data from client script in order fetch details for that specific user.

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions 🙂

 

Best Regards
Aman Kumar

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi @Sai 

1 ) You have to use the reference field , Reference to the sc_request table. (Note : If you want to select only one request then reference field. otherwise list collector for multi select)

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