g_service_catalog.isOrderGuide(); not working on native ui

Exero
Kilo Contributor

Hi,

I have the following code working for a user submitting a catalogue item:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '' || window) {
		return;
	}

	// check if we are in an order guide using the g_service_catalog api for Service Portal and Mobile
	var isOrderGuide = g_service_catalog.isOrderGuide();

	if(!isOrderGuide) {
		SetRequester();
	}
}

function SetRequester()
{
	var requester = g_form.getValue('requested_for');
	g_form.setValue('req_requested_for', requester);
}

 

Basically it is checking if it is not an order guide and if that is the case then it will copy data between one field and another.

However, when I use the "Try it" button on the backend (native UI) or have an automation that runs on the backend it throws an error: "ReferenceError: g_service_catalog is not defined"

I have written a workaround for this as below:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '' || window) {
		return;
	}
	
	try {
		// check if we are in an order guide using the g_service_catalog api for Service Portal and Mobile
		var isOrderGuide = g_service_catalog.isOrderGuide();

		if(!isOrderGuide) {
			SetRequester();
		}
	} catch(e) {
		if(e.toString() == "ReferenceError: g_service_catalog is not defined") {
			SetRequester();
		}
	}
}

function SetRequester()
{
	var requester = g_form.getValue('requested_for');
	g_form.setValue('req_requested_for', requester);
}

 

However, this is not an adequate solution. Would anyone have any clue why I am getting the above error in the backend but not the frontend? I need to be able to discern an order guide from a normal catalogue item.

Thanks in advance

1 ACCEPTED SOLUTION

@Exero 

something like this

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '' || window) {
		return;
	}

	var url = top.location.href;
	if(url.indexOf('sc_cat_item_guide') == -1){
		var requester = g_form.getValue('requested_for');
		g_form.setValue('req_requested_for', requester);
	}
}

Regards
Ankur

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

View solution in original post

5 REPLIES 5

Harish KM
Kilo Patron
Kilo Patron

Check this

https://community.servicenow.com/community?id=community_question&sys_id=34c29333db1e10107d3e02d5ca961905

Regards
Harish

Exero
Kilo Contributor

I wish I could mark comments as unhelpful, because this would surely get that mark... I have checked that community post previously and is wholly unhelpful, as you could see if you actually read my post, I am already successfully using the code they discuss... I am having an issue with an error appearing when the client script is run by the backend, and not the frontend, of servicenow. Please read a post and make sure you understand it before commenting unhelpfully and clogging the feed.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can check the URL and see if it's an order guide and based on that take the decision

Regards
Ankur

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

How would I go about doing this?