Auto populate

SwapnilN
Tera Contributor

I am writing below script for populating some fields from table but its populating after selecting Room Name.

 

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

    // Use g_form.getReference to get the selected room's details
    g_form.getReference('room_name', function(room) {
        if (room) {
            // Auto-populate the fields with the selected room details
            g_form.setValue('floor_number', room.floor_number);
            g_form.setValue('building', room.building);
            g_form.setValue('capacity', room.capacity);
        } else {
            console.error('Room details are not available for the selected room.');
        }
    });
}
2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

I would recommend using the variable auto-populate feature for a code-less approach

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

 

If you are not yet on Utah, or want to stick with the onChange script for whatever reason, confirm of course that room_name is a reference type variable, then add alerts before the getReference and before and after if(room) to confirm the script is running and see how far it is getting.  Also make sure there are no other Catalog Client Scripts or Catalog UI Policies that are affecting these fields when room_name changes.

Looks familiar 🤣

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn