Hello Everyone

Anvesh Reddy123
Tera Contributor

I have two fields requested for(reference) and devices (list collector)
My requirement is to auto populate requested for assigned devices(from hardware table  ) in devices field. Please help me with the solution. 

5 REPLIES 5

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Anvesh Reddy123 

 

Is it in the catalog item? if yes, then in the 4th tab, under variable,  a new functionality has been added called - Auto populate. Try to explore that.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Yes It is in catalog item but we have two fields from different tables.
1.First field is requested for reference field on user table.
2. Second field is Device name list collector field on hardware asset table.

So , whenever requested for changes , I need to auto populate device field which are assigned to requested for in hardware table.I tried with client script and script include , it is working but creating duplicate records.

Hi @Anvesh Reddy123,

 

Create a Catalog Client Script on your catalog item:

 

 

// Catalog Client Script Configuration
Type: onChange
Field Name: requested_for
Script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    
    // Call the Script Include to get devices
    var ga = new GlideAjax('GetDevices');
    ga.addParam('sysparm_name', 'getDevicesForUser');
    ga.addParam('sysparm_user', newValue);
    ga.getXMLAnswer(function(response) {
        var devices = JSON.parse(response);
        g_form.getControl('device_name').options.length = 0; // Clear existing options
        devices.forEach(function(device) {
            g_form.addOption('device_name', device.sys_id, device.name);
        });
    });
}

 

 

Replace 'device_name' with the internal name of your list collector field.

 

Create a Script Include to fetch the devices:

 

 

var GetDevices = Class.create();
GetDevices.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    
    getDevicesForUser: function() {
        var userId = this.getParameter('sysparm_user');
        var deviceList = [];
        
        var gr = new GlideRecord('alm_hardware'); // Replace with your hardware asset table name
        gr.addQuery('assigned_to', userId);
        gr.query();
        
        while (gr.next()) {
            deviceList.push({
                sys_id: gr.getValue('sys_id'),
                name: gr.getValue('name') // Replace 'name' with the actual display field
            });
        }
        
        return JSON.stringify(deviceList);
    }
});

 

Thank you, please make helpful if you accept the solution.

Hi Yashsvi
Thanks for your response, I tried above code but still not working for me. when I change the requested for , device_name field is empty only.
pls find below screenshots:

AnveshReddy123_0-1718649347991.png

AnveshReddy123_1-1718649373204.png