OnLoad Catalog Client Script not working on Catalog Item UI builder Component

Daniel Plunket1
Tera Contributor

Hello, 

I'm working on configuring a catalog item component on a UI builder page to display a record producer. This record producer has an onLoad client script to populate a reference field with the current logged in user along with additional fields for phone number and email address. The second script is an onChange script where when the selected user changes, it populates the telephone and email address fields.

 

The onLoad script doesn't seem to work when I view it through UI builder, but the onChange one does. I have the UI type for both set to All.

 

Has anyone run into this before?

3 REPLIES 3

Riya Verma
Kilo Sage
Kilo Sage

Hi @Daniel Plunket1 ,

 

Hope you are doing great.

 

UI Type of script should be set to "All" for both scripts. This situation can occur due to a few possible reasons. Let's explore some potential solutions to address this problem:

 

  1. Check Script Execution Order: Ensure that the onLoad script is executed before the onChange script. In ServiceNow, the order of execution can impact the behavior of client scripts. 

  2. Verify Field Mapping: Double-check the field mapping configuration for the reference field, telephone, and email address. Ensure that the field names specified in the script match the actual field names on the record producer form. 

Below is an example code snippet showcasing the onLoad and onChange client scripts:

onload:

function onLoad() {
   // Populating reference field with the current logged-in user
   var userId = gs.getUserID();
   var currentUser = new GlideRecord('sys_user');
   if (currentUser.get(userId)) {
       current.u_reference_field = currentUser.getUniqueValue();
       current.u_phone_number = currentUser.phone;
       current.u_email_address = currentUser.email;
   }
}

 

on change

function onChange(control, oldValue, newValue, isLoading) {
   // Populating telephone and email address fields when the selected user changes
   if (!isLoading && newValue) {
       var selectedUser = new GlideRecord('sys_user');
       if (selectedUser.get(newValue)) {
           current.u_phone_number = selectedUser.phone;
           current.u_email_address = selectedUser.email;
       }
   }
}

 

By ensuring the proper execution order, field mapping, and debugging, you should be able to identify and resolve the issue with the onLoad script not working within the UI builder page.

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

SnowShilpi
Tera Contributor

Hi Riya, Still Its not working i am setting placeholder text on load for 2 fields

function onLoad() {
   var shortDescription = g_form.getControl('short_description');
   shortDescription.placeholder = "e.g. Reporting system for XYZ transactions";
   
   var businessImpactJustification = g_form.getControl('u_business_impact_justification');
   businessImpactJustification.placeholder = "e.g. In XYZ Reporting App, non offer centric data.";
}
 
order is 90 still its not working

shubhamgoya
ServiceNow Employee
ServiceNow Employee

I am also facing the same issue. Bumping this up if anyone found a solution.