Catalog data lookup definition

I_Das
Tera Contributor

I am trying to write a catalog data lookup definition. If a user is logged in then it will auto populate their business unit and location. But it is only working in on change. It is not working for on load. Does catalog data lookup definition not work for on load?

5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

Catalog Data Lookup Definitions do not work onLoad.  If your instance is on the Utah or later version, try the Variable Auto-populate feature

https://www.servicenow.com/community/developer-articles/auto-populate-a-variable-based-on-a-referenc... 

 

Pre-Utah you can use an onLoad Catalog Client Script which uses supported methods of getReference

https://docs.servicenow.com/bundle/tokyo-api-reference/page/app-store/dev_portal/API_reference/Glide... 

or GlideAjax

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

to return the related field values based on the variable value.

Thank you very much.

Community Alums
Not applicable

In ServiceNow, the Catalog Data Lookup Definitions are designed to work with onChange events, not onLoad events. This is because data lookup definitions are primarily intended to populate fields based on a change in another field.

 

To resolve this issue, you can use a Client Script. This script will execute when the form loads (onLoad) and will populate the fields with the logged-in user's business unit and location. Here's a simple example of how you can do this:

function onLoad() {
// This script runs when the form loads
if (g_form.isNewRecord()) { // Checks if the form is a new record
var userID = g_user.userID; // Gets the logged-in user's ID
var gr = new GlideRecord('sys_user'); // Creates a new GlideRecord on the 'sys_user' table
if (gr.get('user_name', userID)) { // If the user exists in the 'sys_user' table
g_form.setValue('business_unit', gr.getValue('business_unit')); // Sets the 'business_unit' field to the user's business unit
g_form.setValue('location', gr.getValue('location')); // Sets the 'location' field to the user's location
}
}
}

 

 

 

This script uses the GlideRecord API to retrieve the logged-in user's business unit and location from the 'sys_user' table. It then populates the 'business_unit' and 'location' fields on the form with these values.

Thank you.