catalog client script email

RC12
Tera Contributor

i have written 1 client script where as soon as the person loggs in, his name and mail would be populated. when i "try it" in the catalog, it is getting populated when i use the service portal, the name is getting populated but in the email field, at the top an error message is coming in portal saying "there is a javascript error in your browser console". 

function onLoad() {
//Type appropriate comment here, and begin script below
var a=g_user.userID;
g_form.setValue('name',a);
var b=g_form.getReference('name');//user table
g_form.setValue('email',b.email);

}

can any1 tell me what i am doing wrong

1 ACCEPTED SOLUTION

Robbie
Kilo Patron
Kilo Patron

Hi RC,

Whilst Murthy is correct to point you to best practice and use GlideAjax to fetch server data from the client, there is a way to do this very easily.

Rather than call an onLoad script, set the default values for each field by configuring the variable.

For the email field, set the default value to: javascript: gs.getUser().getEmail();

For the name field, set the default value to: javascript: gs.getUserID()

These will work on both the catalog native view and the Service Portal.

Please mark correct and/or helpful.

Thanks,

Robbie

View solution in original post

5 REPLIES 5

Hi RC,

You're welcome. Happy to help.

What's important to point out here for your understanding is the difference between 'Server side' and 'Client side' scripting and the API's available for each.

Here's an extremely useful link to the API's which is a goto for every SN developer:

(Notice you can switch between Client and Server (Global Server) under the API type)

https://developer.servicenow.com/dev.do#!/reference/api/quebec/server_legacy/c_GlideSystemAPI#r_GS-getUser?navFilter=glidesys

For the solution I provided, we leveraged gs.getuser(). 'gs' is calling on GlideSystem which is a Server side API whereby we have various methods available to us including getEmail() 

We called this on the default value(s) at dictionary level.

Please note however, you cannot use gs or GlideSystem on Client side scripts. In your original script you were correctly calling g_user or GlideUser where you also have various methods such as .userID

SN provide a number of API's and methods for most common use cases, however when you need something for a more specific use case and you need to query server side data from the client, you'll need to use GlideAjax. There's a ton of material and examples on the community for this, however a good starting point is: 

https://docs.servicenow.com/bundle/rome-application-development/page/script/ajax/topic/p_AJAX.html

To help others, please mark correct and/or helpful.

Thanks,

Robbie