AutoPopulate a reference field based on another reference both fields referring same table

Community Alums
Not applicable

Need to auto populate application business unit which is a reference field referring (cmdb_ci_business_app) depending on the business application field which is also a reference field also referring to (cmdb_ci_business_app) table on a catalog form.

Variable 1: Business Application (Reference - cmdb_ci_business_app)

variable 2: Application business unit(reference-cmdb_ci_business_app)

tried the autopopulate option on the variable it is populating on form level but on ritm its clearing the value.

written a script include and onchange client script also

able to get the value from script include but not able to set on the field . Its setting the value and clearing immediately 

please help

1 ACCEPTED SOLUTION

@Community Alums 

For this case you can use 2nd variable as string type and populate the Business unit via onChange client script

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

View solution in original post

15 REPLIES 15

Brad Bowman
Kilo Patron
Kilo Patron

It sounds like both the auto-populate feature and client script/script include are probably working, but being overridden by a Catalog UI Policy or another Catalog Client Script when variable 2 changes.

Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

why both variabes are referring to same table?

your 2nd variable should be string and set the value using onChange client script + GlideAjax

Also you mentioned it's clearing on RITM level

Please share your script here along with screenshot. your script should be set with Applies on Catalog form checkbox as true

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

Community Alums
Not applicable
Catalog Client Script:
function
onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var ga = new GlideAjax('CBHFlexCare360readacess');
    ga.addParam('sysparm_name', 'getBusinessUnit');
    ga.addParam('recName', g_form.getValue('business_application'));
    ga.getXMLAnswer(resp);

    function resp(response) {
        // var answer = response.responseXML.documentElement.getAttribute("answer");
        // alert(answer);
            g_form.setValue('application_business_unit', response);
            // g_form.setReadOnly('application_business_unit', true);
        }
    }

Script Include:
getBusinessUnit: function() {
        var appname = this.getParameter('recName');
        var gr = new GlideRecord('cmdb_ci_business_app');
        gr.addQuery('sys_id', appname);
        gr.query();
        if (gr.next()) {
            return gr.business_unit.getDisplayValue;
        }
    },   
With the above script its setting the value on catalog form view i.e., ess view and clearing immediately.
Could you Please guide on this.


If 'application_business_unit' is a reference type variable, just return the business_unit (sys_id), not the display value as a sys_id is what is needed to populate a reference variable or field.

            return gr.business_unit;