Autopopulate value for list type filed if there is only one choice available

Rutuja K
Giga Guru

Hi all,

 

How to auto populate or auto select value in list type field when there is only one choice/ record available for selection?

 

Thank You!

RK.

1 ACCEPTED SOLUTION

Tai Vu
Kilo Patron

Hi @Rutuja K 

Timi_0-1704255567678.png

From your screenshot above, you can try the below approach.

1. Create an OnChange Client Script in the Product ID [u_product_id] field.

2. Make a GlideAjax call with the same query to the gethostingtype function. Then return an array contains the sys_id of Hosting Type records.

3. Now in Client Script, check if the array length in response and set the value to the Hosting Type.

Sample below.

#Script Include

getServices: function() {
	var owner = this.getParameter('sysparm_owner_id');
	var arrService = [];
	var grService = new GlideRecord('cmdb_ci_service');
	grService.addQuery('owned_by', owner);
	grService.query();
	while (grService.next()) {
		arrService.push(grService.getUniqueValue());
	}
	return JSON.stringify(arrService);
},

 

#OnChange Client Script

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

    if (newValue === '') {
        g_form.clearValue('business_services');
    }

    var ga = new GlideAjax('CLCatalogItemUtilAJAX');
    ga.addParam('sysparm_name', 'getServices');
    ga.addParam('sysparm_owner_id', newValue);
    ga.getXMLAnswer(function(answer) {
        var service = JSON.parse(answer);
        if (service.length === 1) {
            g_form.setValue('business_services', service[0]);
			return;
        }
		g_form.clearValue('business_services');
    });
}

 

Enjoy

Timi_1-1704257381374.png

 

 

Cheers,

Tai Vu

View solution in original post

10 REPLIES 10

Addy22
Tera Guru

Hi,

 

You can provide a default value directly for any choice you want to show as auto select

Thanks for responding Adarsh, 

I didn't understand what you are suggesting

can you please explain

Ankur Bawiskar
Tera Patron

@Rutuja K 

auto-populate but based on what criteria?

please explain your business requirement in detail along with screenshots.

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar ,

based on value selected in one field, I want to populate value in list type field.
List collector records are fetched from another table. If there is only one record available for selection then I want that to-be populated by default.