Modify Catalog Item RITM short description based on requested_for user location.

sarthak92
Giga Expert

Hi Guys, We have a requirement for one specific Catalog Item to modify the RITM short description and add [India] to short description if requested for user (u_requested_for) location is "India" in sys_user table record. I would like to do this via catalog client script only as we have a common workflow for most of Catalog Items so don't want to touch the workflow but somehow not able to achieve the same via onsubmit catalog client script. 

function onSubmit()
{
var user = new GlideRecord('sys_user');
user.addQuery('u_user_location',India);
user.addQuery('sys_id',g_form.getValue('u_requested_for'));
user.query();
while(user.next())
{
g_form.setValue('short_description','Catalog Item ABC [India]');
}
}

6 REPLIES 6

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Sarthak,

I would recommend switching to business rule and apply filter condition to trigger it only for a specific catalog item.

When : Before

Insert : True

Condition : Item | is | YOUR catalog item name

Script:

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var userGr = new GlideRecord('sys_user');
        user.addQuery('location.name','India'); //Make sure you are updating the correct record name. Alternatively you can remove this line from here and update this as part of BR filter conditions via dot-walking.
	userGr.addQuery('sys_id', current.request.requested_for);
	userGr.query();
	if(userGr.next())

		{
			current.short_description =current.short_description + 'India';
		}
})(current, previous);

 

- Pradeep Sharma 

Hi Pradeep, Thanks for your prompt answer. I am able to achieve the same without any script in BR with the help of Out of the Box conditions and Action in BR, But I would need to understand as how I can achieve the same via Catalog Client script ? can you help me with the script in case my script has some error? 

The OnSubmit Client script that you shared will be executed only when the form is submitted again. If you still want to do this via Client script I would recommend switching it to OnLoad CS.  I don't see any issue with the script. That said, please note it is best practice to avoid executing server-side code from the client-side. 

Please refer "Never use GlideRecord in Client Scripts" https://community.servicenow.com/community?id=community_blog&sys_id=f27ce2e1dbd0dbc01dcaf3231f961912

 

- Pradeep Sharma

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Sarthak,

As Pradeep already mentioned have before insert BR on sc_req_item table with condition as Catalog Item is your catalog item and use below

Note: I assume u_requested_for is a variable of type reference to sys_user table

if(current.variables.u_requested_for.u_user_location == 'India'){

current.short_description = current.short_description + 'India';

}

If u_requested_for is a field on RITM table then use below

if(current.u_requested_for.u_user_location == 'India'){

current.short_description = current.short_description + 'India';

}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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