Catalog client script to clear location

Mary S
Mega Sage

For catalog items, we have a variable set that includes location with a default of javascript:gs.getUser().getLocation().

 

We have traveling staff with multiple locations and would like this cleared on load for them so they are forced to select a location.

 

Trying a client script on the variable set is working for OOB title, but now for a field we created: "Buildings" u_buildings.

 

Getting the message when saving: This catalog client script is not VA supported due to the presence of the following variables in the script: g_user.u_buildings.

 

What should be in the var userBuildings= ?

 

function onLoad() {
   //Set title and buildings
    var userTitle = g_user.title;
	var userBuildings = g_user.u_buildings;
	
	//If user is a sub, clear location
    if (userTitle && userTitle.toLowerCase().includes('sub')) {
        g_form.clearValue('location');
    }
	//If user has multiple buildings, clear location
	if (userBuildings.includes(',')) {
		        g_form.clearValue('location');
    }
	//If user is a building sub, clear location
	if (userBuildings.toLowerCase().includes('sub')) {
		        g_form.clearValue('location');
    }
}

 

1 ACCEPTED SOLUTION

Mary S
Mega Sage

It's been a while, but thought I'd update this with how it worked for us. Two client scripts added to the variable set did the trick.

 

Variable set:

  • Opened by: reference sys_user
  • Requested for: reference sys_user
  • Location: reference cmn_location; auto-populate dot walk from requested_for
  • Title:  string; auto-populate dot walk from requested_for (hidden--used for script only)
  • Buildings: string; custom field; auto-populate from requested_for (hidden--used for script only)

 

1st Client script: If requested_for title contains sub, clear location

onChange

applied to variable "title"

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == '') {
    return;
  }

  var titleValue = newValue.toLowerCase();

  if (titleValue.includes('sub')) {
    g_form.setValue('location', '');
  }
}

 

2nd Client script: If requested_for buildings contains a comma (more than one building)

onChange

applied to variable "buildings"

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == '') {
    return;
  }

  var buildingsValue = newValue.toLowerCase();

  if (buildingsValue.includes(',')) {
    g_form.setValue('location', '');
  }
}

 

View solution in original post

3 REPLIES 3

Paul Curwen
Giga Sage

You cannot use g_user.u_buildings.

 

g_user is the GlideUser API method (a handful of cached user properties that are easily and quickly accessible to client-side JavaScript) and has no awareness of what u_buildings is, it is not a recognised method. See here for list of g_user methods that are available:

 

https://servicenowguru.com/scripting/user-object-cheat-sheet/

 

I presume u_buildings is a custom field you have defined on the user (sys_user) table? If so, then you have two choices:

 

1. Use GlideAjax to get the user u_building value from the sys_user table in your Client Script

2. Use GlideSession (getClientData() and putClientData() methods)

to store the users common data, so you can access it directly and immediately without a database lookup anywhere in the platform. (Recommended

 

If you have not used GlideSession before learn all about it directly from Chuck himself here (start at 10:20 mins) in this video (you will thank me later 😉https://www.servicenow.com/community/developer-blog/video-community-live-stream-api-adventures-glide...

 

 

***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul

Mary S
Mega Sage

Thanks for the information, @Paul Curwen .  Yes, u_buildings is a custom field on the user table.  I will look into GlideSession.

Mary S
Mega Sage

It's been a while, but thought I'd update this with how it worked for us. Two client scripts added to the variable set did the trick.

 

Variable set:

  • Opened by: reference sys_user
  • Requested for: reference sys_user
  • Location: reference cmn_location; auto-populate dot walk from requested_for
  • Title:  string; auto-populate dot walk from requested_for (hidden--used for script only)
  • Buildings: string; custom field; auto-populate from requested_for (hidden--used for script only)

 

1st Client script: If requested_for title contains sub, clear location

onChange

applied to variable "title"

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == '') {
    return;
  }

  var titleValue = newValue.toLowerCase();

  if (titleValue.includes('sub')) {
    g_form.setValue('location', '');
  }
}

 

2nd Client script: If requested_for buildings contains a comma (more than one building)

onChange

applied to variable "buildings"

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == '') {
    return;
  }

  var buildingsValue = newValue.toLowerCase();

  if (buildingsValue.includes(',')) {
    g_form.setValue('location', '');
  }
}