Client script causing javascript error

Russell Abbott
Kilo Sage

I have a client script that updates a RITM when an Interaction type is 'Request'. It copies a 'Yes' or 'No' value from the Interaction over to the RITM

However it seems like it's throwing an error on the Portal view "there is a Javascript error in your browser console" the error is 'typeerror: window is null'

How can i set this script to not load on the Portal view? We don't create Interactions from there.

Or, is there another way to get this to work?

Thanks!

 

function onLoad() {
   //Type appropriate comment here, and begin script below
   var urlParams = new URLSearchParams(window.location.href);
   var data=urlParams.get("u_is_this_being_submitted_for_a_provider_physician_or_app");
	g_form.setValue("is_this_being_submitted_for_a_provider_physician_or_app",data);
}

find_real_file.png

1 ACCEPTED SOLUTION

I have just tested this in PDI and I observed the following behavior.

function onLoad() {

    if (window === null) { // For portal
        
		//top.location.href; will work in portal
		//top.window.location.href; will work in portal
		//top.document.location.href; will work in portal
		//window.location.href; will not work in portal because window is null in portal
		//document.location.href; will not work in portal because document is null in portal
		
		alert('Portal  Top: ' + top.location.href);
		alert('Portal Top.Window: ' + top.window.location.href);
		alert('Portal Top.Document: ' + top.document.location.href);
		alert('Document Object: ' + document);
		alert('Window Object: ' + window);
		alert('Top Object: ' + top);
		
    } else { // For Native UI
		
		//top.location.href; will work in Native UI
		//top.window.location.href; will work in Native UI
		//top.document.location.href; will work in Native UI
		//window.location.href; will work in Native UI
		//document.location.href; will work in Native UI
		
		
		alert('Native UI Top: ' + top.location.href);
		alert('Native UI Top.Window: ' + top.window.location.href);
		alert('Native UI Window: ' + window.location.href);
		alert('Native UI Top.Document: ' + top.document.location.href);
		alert('Native UI Document: ' + document.location.href);
		alert('Document Object: ' + document);
		alert('Window Object: ' + window);
		alert('Top Object: ' + top);
    }	
}
  1. Scenario 1:
    • Catalog Client Script UI Type = All
    • Isolate = False
    • If block will be executed in portal, else block will be executed in Native UI.
  2. Scenario 2:
    • Catalog Client Script UI Type = All
    • Isolate = True
    • If block will be executed in portal and Native UI both, else block will not be executed in portal and Native UI.
    • window and document will be null, to access them top will be used.
  3. Scenario 3:
    • Catalog Client Script UI Type = Desktop
    • Isolate = False
    • No block will be executed in Portal, else block will be executed in Native UI.
  4. Scenario 4:
    • Catalog Client Script UI Type = Desktop
    • Isolate = True
    • No block will be executed in Portal, if block will be executed in Native UI.
    • window and document will be null, to access them top will be used.
  5. Scenario 5:
    • Catalog Client Script UI Type = Mobile / Service Portal
    • Isolate = False
    • No block will be executed in Native UI, if block will be executed in Portal.
    • window and document will be null, to access them top will be used.
  6. Scenario 6:
    • Catalog Client Script UI Type = Mobile / Service Portal
    • Isolate = True
    • No block will be executed in Native UI, if block will be executed in Portal.
    • window and document will be null, to access them top will be used.

 

Now coming to your question,

Was it working fine in Native UI before?

If it was working fine in Native UI, then try the below script. Make sure to uncheck Isolate checkbox and UI type should be All.

function onLoad() {

    var params;
    var data;

    if (window === null) { // For portal

        urlParams = new URLSearchParams(top.window.location.href);
        data = urlParams.get("u_is_this_being_submitted_for_a_provider_physician_or_app");
        g_form.setValue("is_this_being_submitted_for_a_provider_physician_or_app", data);

    } else { // For Native UI

        urlParams = new URLSearchParams(window.location.href);
        data = urlParams.get("u_is_this_being_submitted_for_a_provider_physician_or_app");
        g_form.setValue("is_this_being_submitted_for_a_provider_physician_or_app", data);
    }
}

 

If you want this to be executed in Native UI then change UI type to Desktop and remove the if block along with the script inside and remove the else without removing the script inside.

 By the way when I used urlParams.get(), I was only able to get those parameters which start with sysparm_ e.g., urlParams.get('sysparm_view') was working fine but urlParams.get('sys_id') was returning null.

View solution in original post

9 REPLIES 9

Muhammad Khan
Mega Sage
Mega Sage

Hi

 

You can access window in portal via top. Try below script.

function onLoad() {
   //Type appropriate comment here, and begin script below
    if (window === null) {
        var urlParams = new URLSearchParams(top.window.location.href);
        var data=urlParams.get("u_is_this_being_submitted_for_a_provider_physician_or_app");
        g_form.setValue("is_this_being_submitted_for_a_provider_physician_or_app",data);
    }
}

For better understanding refer to docs.

Service Portal and client scripts

find_real_file.png

While this did stop the error, it would no longer pass the value from an Interaction to a Request upon transfer. Any ideas?

I have just tested this in PDI and I observed the following behavior.

function onLoad() {

    if (window === null) { // For portal
        
		//top.location.href; will work in portal
		//top.window.location.href; will work in portal
		//top.document.location.href; will work in portal
		//window.location.href; will not work in portal because window is null in portal
		//document.location.href; will not work in portal because document is null in portal
		
		alert('Portal  Top: ' + top.location.href);
		alert('Portal Top.Window: ' + top.window.location.href);
		alert('Portal Top.Document: ' + top.document.location.href);
		alert('Document Object: ' + document);
		alert('Window Object: ' + window);
		alert('Top Object: ' + top);
		
    } else { // For Native UI
		
		//top.location.href; will work in Native UI
		//top.window.location.href; will work in Native UI
		//top.document.location.href; will work in Native UI
		//window.location.href; will work in Native UI
		//document.location.href; will work in Native UI
		
		
		alert('Native UI Top: ' + top.location.href);
		alert('Native UI Top.Window: ' + top.window.location.href);
		alert('Native UI Window: ' + window.location.href);
		alert('Native UI Top.Document: ' + top.document.location.href);
		alert('Native UI Document: ' + document.location.href);
		alert('Document Object: ' + document);
		alert('Window Object: ' + window);
		alert('Top Object: ' + top);
    }	
}
  1. Scenario 1:
    • Catalog Client Script UI Type = All
    • Isolate = False
    • If block will be executed in portal, else block will be executed in Native UI.
  2. Scenario 2:
    • Catalog Client Script UI Type = All
    • Isolate = True
    • If block will be executed in portal and Native UI both, else block will not be executed in portal and Native UI.
    • window and document will be null, to access them top will be used.
  3. Scenario 3:
    • Catalog Client Script UI Type = Desktop
    • Isolate = False
    • No block will be executed in Portal, else block will be executed in Native UI.
  4. Scenario 4:
    • Catalog Client Script UI Type = Desktop
    • Isolate = True
    • No block will be executed in Portal, if block will be executed in Native UI.
    • window and document will be null, to access them top will be used.
  5. Scenario 5:
    • Catalog Client Script UI Type = Mobile / Service Portal
    • Isolate = False
    • No block will be executed in Native UI, if block will be executed in Portal.
    • window and document will be null, to access them top will be used.
  6. Scenario 6:
    • Catalog Client Script UI Type = Mobile / Service Portal
    • Isolate = True
    • No block will be executed in Native UI, if block will be executed in Portal.
    • window and document will be null, to access them top will be used.

 

Now coming to your question,

Was it working fine in Native UI before?

If it was working fine in Native UI, then try the below script. Make sure to uncheck Isolate checkbox and UI type should be All.

function onLoad() {

    var params;
    var data;

    if (window === null) { // For portal

        urlParams = new URLSearchParams(top.window.location.href);
        data = urlParams.get("u_is_this_being_submitted_for_a_provider_physician_or_app");
        g_form.setValue("is_this_being_submitted_for_a_provider_physician_or_app", data);

    } else { // For Native UI

        urlParams = new URLSearchParams(window.location.href);
        data = urlParams.get("u_is_this_being_submitted_for_a_provider_physician_or_app");
        g_form.setValue("is_this_being_submitted_for_a_provider_physician_or_app", data);
    }
}

 

If you want this to be executed in Native UI then change UI type to Desktop and remove the if block along with the script inside and remove the else without removing the script inside.

 By the way when I used urlParams.get(), I was only able to get those parameters which start with sysparm_ e.g., urlParams.get('sysparm_view') was working fine but urlParams.get('sys_id') was returning null.

This is incredibly insightful and in depth! I was able to get this working and now have a greater understanding of what I'm working with. Thank you!

Russell Abbott
Kilo Sage

I didn't need to pull this from a URL on the Portal view as it's only for Interactions to Requests and we don't use Portal view for Interactions. I made it work much simpler below, but this doesn't work on Workspaces agent view. Any ideas?

function onLoad() {
   //Type appropriate comment here, and begin script below
   var data=g_form.getValue("u_is_this_being_submitted_for_a_provider_physician_or_app");
	g_form.setValue("is_this_being_submitted_for_a_provider_physician_or_app",data);
}