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

@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