Regarding autopopulation of data upon select one data lookup field

Gudu
Tera Contributor

Hi Team,

I would like to auto populate some fields based on selection of one data lookup field.

 

Gudu_0-1751019549383.png

 

Here select the cmdb application is a datalookup field and it is referencing to cmdb_ci_appl table. 

I want to auto populate below fields upon selection of any application from this "select the cmdb application" field. 

 

1. application status

2. application short name 

3. application long name

4. application owner 

5. applcation prime 

6. vp

7. deprovisioning servicenow queue name (support group)

8. sox 

 

Could anyone please help me with code , as i am new to this and a beginner. 

 

please leave more information field as this is the user field has to  be filled.

4 REPLIES 4

Najmuddin Mohd
Mega Sage

Hello @Gudu ,

The easy way without scripting would be to use Auto Populate Feature in the Catalog item. But, for this you need to change the Lookup field type to Reference field type.

The below article will provide you step by step approach.

https://www.servicenow.com/community/developer-articles/auto-populate-reference-data-in-service-cata...


If you want to go with Lookup field, then you need to create onChange Client script on the Catalog item and call GlidAjax ScriptInclude to fill the values.

The below link provides you to know how to write GlideAjax.

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


If the above information helps you, Kindly mark it as Helpful and Accept the solution.
Regards,
Najmuddin.


Gudu
Tera Contributor

It will be helpful to me if anyone will writhe down the code for me as this is confusing me. 

Ankur Bawiskar
Tera Patron
Tera Patron

@Gudu 

you can use auto populate feature which came from Utah onwards and no scripting required

Auto-populate a variable based on a reference type variable (Utah) 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Harmeet2Singh
Tera Expert

Hi,

First create a client callable Script include as below, replace the actual table and fields below:

 

var get_application_data = Class.create();
get_application_data.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    fetchdata: function() {
        var ap = this.getParameter('sysparm_application');
        var appl = new GlideRecord('cmdb_ci_appl'); // your cmdb table
        appl.get(ap);
        return JSON.stringify({ // Please change the field names as in table
            application_status: appl.getValue('operational_status'),
            short_name: appl.getValue('short_name'),
            long_name: appl.getValue('name'),
            owner: appl.getValue('owned_by'),
            prime: appl.getValue('prime'),
            vp: appl.getValue('vp'),
            deprovisioning_servicenow_queue_name: appl.getValue('queue_name'),
            sox: appl.getValue('sox'),
        });


    },

    type: 'get_application_data'
});
 
 
Now create an on change catalog client script, on variable 'Select the CMDB Application'
 
use below code, with actual variable names to be replaced in quoted names
 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    // Call the script include to get user information
    var ga = new GlideAjax('get_application_data');
    ga.addParam('sysparm_name', 'fetchdata');
    ga.addParam('sysparm_application', newValue);
    ga.getXML(populateApplicationDate);
}

function populateApplicationDate(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if (answer) {
        var appInfo = JSON.parse(answer);
        // Populate fields with application information, replace variable names in quotes as in form.
        g_form.setValue('application_status', appInfo.application_status);
        g_form.setValue('short_name', appInfo.short_name);
        g_form.setValue('name', appInfo.name);
        g_form.setValue('owned_by', appInfo.owned_by);
        g_form.setValue('short_name', appInfo.short_name);
        g_form.setValue('prime', appInfo.prime);
        g_form.setValue('vp', appInfo.vp);
        g_form.setValue('queue_name', appInfo.queue_name);
        g_form.setValue('sox', appInfo.sox);

    }

}
 
 
Please mark this response as correct or helpful if it assisted you with your question.