How to get order guide sysid in on load script

Debasis Pati1
Tera Contributor

Hi Everyone,

How i should get order guides sysid in on load catalog client script.

I tried the below code it is working for me in portal but not on native view.

in native view i tried g_form.getParameter("sysparm_id") as well even this is not working i am getting a null value.

 

var url = top.location.href;
    var orderGuideSysId;
    if(window == null){
        // for portal
        orderGuideSysId = new URLSearchParams(url).get("sys_id");
    }
    else{
        // for native
        orderGuideSysId = new URLSearchParams(url).get("sysparm_guide");

}

alert(orderGuideSysId);

}

I am getting the alert of null in native view load and in portal its working fine.

@Ankur Bawiskar Can you help me here.

 

Regards,

Debasis

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi @Debasis Pati 

update as this

Ensure Isolate Script field is False for your client script for allowing DOM i.e. window

function onLoad(){

	var url = top.location.href;
	var orderGuideSysId;
	if(window == null){
		// for portal
		orderGuideSysId = new URLSearchParams(url).get("sys_id");
	}
	else{
		// for native
		var gUrl = new GlideURL();
		gUrl.setFromCurrent();
		orderGuideSysId = gUrl.getParam("sysparm_guide");
	}

	alert(orderGuideSysId);

}

OR use this and Keep Isolate Script = True

function onLoad(){

	try{
		var url = top.location.href;
		var orderGuideSysId;
		if(window == null){
			// for portal
			orderGuideSysId = new URLSearchParams(url).get("sys_id");
		}
	}
	catch(ex){
		// for native
		var gUrl = new GlideURL();
		gUrl.setFromCurrent();
		orderGuideSysId = gUrl.getParam("sysparm_guide");
	}

	alert(orderGuideSysId);

}

Regards
Ankur

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

View solution in original post

3 REPLIES 3

Muhammad Khan
Mega Sage

@Debasis Pati 

 

Uncheck the isolate script, then it will work in Native UI as well. See the below image for reference.

find_real_file.png

If that field is not available on the form, then you have to bring it on the form via form layout.

Check out this article, it will help you understand the behavior of top, window and doument objects.

Top Window Document Objects in Native UI and Service Portal

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi @Debasis Pati 

update as this

Ensure Isolate Script field is False for your client script for allowing DOM i.e. window

function onLoad(){

	var url = top.location.href;
	var orderGuideSysId;
	if(window == null){
		// for portal
		orderGuideSysId = new URLSearchParams(url).get("sys_id");
	}
	else{
		// for native
		var gUrl = new GlideURL();
		gUrl.setFromCurrent();
		orderGuideSysId = gUrl.getParam("sysparm_guide");
	}

	alert(orderGuideSysId);

}

OR use this and Keep Isolate Script = True

function onLoad(){

	try{
		var url = top.location.href;
		var orderGuideSysId;
		if(window == null){
			// for portal
			orderGuideSysId = new URLSearchParams(url).get("sys_id");
		}
	}
	catch(ex){
		// for native
		var gUrl = new GlideURL();
		gUrl.setFromCurrent();
		orderGuideSysId = gUrl.getParam("sysparm_guide");
	}

	alert(orderGuideSysId);

}

Regards
Ankur

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

in native view the above script gives undefined as alert