How to Determine Context of Catalog Client Script?

G24
Kilo Sage

As we all know, a Service Catalog "Item" can have a Catalog Client Script and that script may be run in up to THREE different "contexts" (situations), as indicated by the "Applies on" checkboxes:

xxxx.png

 

Question 1

I would think that for EACH OF THOSE THREE, we could also be on EITHER the Portal or the Platform UI.  Is that right?  So up to 6 total situations??

   Catalog Item Portal

   Catalog Item Platform UI

   Requested Item Portal

   Requested Item Platform UI

   Catalog Task Portal

   Catalog Task Platform UI

 

Question 2 (My Main Question)

What is the best / most reliable / most elegant / most reusable way to determine which of those 3 to 6 contexts I am running in?  Please provide example code if possible.

 

Why do I want to know this?  Because I might have a 1,000 line script, and only a small part of it may need to BRANCH (do something differently) if I am in a particular context.  So I don't want to duplicate my entire script.  I just want to KNOW what context I am running in, so I can branch, if necessary.  Hopefully that makes sense.  Thank you!

3 REPLIES 3

Hemanth M1
Giga Sage
Giga Sage

Hi @G24 ,

 

The UI type determines where you would like to run this script

1)All : On All ui types

2)Desktop : Only Platform UI

3)Mobile/Service Portal  : Only Mobile and Portal UI

 

HemanthM1_0-1698292141506.png

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

Jim Coyne
Kilo Patron

 

 

Try this Catalog Client Script out in your environment:

 

 

 

function onLoad() {
/*
    var ui = "fulfiller"; //default assumption
    if (window === null) {
        ui = "portal";
    }
*/
    if (typeof window != 'undefined') {
        var ui = "portal";
    } else {
        var ui = "fulfiller";
    }

    //or if not afraid of the ternary operator
    var ui2 = (window === null) ? "portal" : "fulfiller";

    try {
        var ignore = g_service_catalog.isOrderGuide();
        var ui3 = "portal";
    } catch (err) {
        var ui3 = "fulfiller";
    }

    //this seems to work properly
    var ui4 = "fulfiller";  //default assumption
    if (typeof spModal != 'undefined') {
       ui4 = "portal";
    }

    var where = g_form.getTableName();

    console.warn("Client Script is running in the " + ui + " - " + ui2 + " - " + ui3 + " - " + ui4 + " view on the " + where + " form/table");

}

 

 

 

You should be able to use the "window" object to differentiate between the fulfiller and Service Portal views, but I've tried in both Utah and Vancouver and it's not working for some reason.  It is actually mentioned in this SN Docs article - Service Portal and client scripts.

 

The article also mentions that "g_service_catalog" is only available in the Portal, but trying to use that in the try/catch statement doesn't seem to work properly either.  It raises an error when outside the try/catch in the fulfiller view, that's why I tried the try/catch statement to avoid the error but detect where it was running.

 

What seems to work properly is to check if the "spPortal" object is defined, which it is only in the Portal view.

 

The "getTableName()" is helpful as well.  It will return:

  • "ni" when ordering the item in the fulfiller view
  • "sc_cat_item" when ordering the item in the Portal view
  • "sc_req_item" when looking at the Requested Item record in the fulfiller view
  • "sc_task" when looking at a Catalog Task record in the fulfiller view 

 

G24
Kilo Sage

Thanks @Jim Coyne Building upon your input, I came up with the following:

 

 

 

function onLoad() {
	var myContext = getContextOfClientScript();
	g_form.addInfoMessage("myContext: " + myContext);

	if (myContext == "sc_cat_item")
	{
		//Do Portal Catalog Item things...
		g_form.addInfoMessage("Looks like we are on the Portal, and ordering a Service Catalog Item.");
	}
	else if (myContext == "sc_req_item"){
		//Do Platform UI Requested Item things...
		g_form.addInfoMessage("Looks like we are on the Platform UI, and looking at a Requested Item.");
	}
	else if (myContext == "sc_task")
	{
		//Do Platform UI Service Catalog Task things.
		g_form.addInfoMessage("Looks like we are on the Platform UI, and working on a Service Catalog Task");
	}
}

//Reliably determine running on Portal ("sc_cat_item") or the Platform UI (either "sc_req_item" or "sc_task")
function getContextOfClientScript()
{
	var sTableName = g_form.getTableName();	
	if (sTableName){
		return sTableName;
	}
	else
	{
		return getPortalURLParameterValue("id");
	}

	function getPortalURLParameterValue(name) {
		name = name.replace(/[[]/, "\\[").replace(/[\]]/, "\\]");  
		var regexS = "[\\?&]" + name + "=([^&#]*)";  
		var regex = new RegExp(regexS);  
		var results = regex.exec(top.location);  
		if (results == null) {  
			return "";  
		} else {  
			return decodeURI(results[1]);
		}  
	}
}

 

 

 

 

Screenshots:

111.png222.png333.png

 

Very open to much more elegant solutions.

( And I noticed that for my own "Question 1", the script is NOT RUN in the case where you are viewing a Requested Item on the Service Portal.  Assume the same for viewing a Task on the Service Portal - Script not executed at all.)

Thanks.