Auto Populate Reference field based on another Reference field

Austine
Tera Contributor

How can I auto populate a field value based on the value selected on another field? Basically, these are two different fields that are referencing the same table, so we want the value selected for field 1 to be auto populated in field 2.

 

Thanks.

 

8 REPLIES 8

@Austine Please check if there is any reference qualifier applied on the site field. Please refer to https://docs.servicenow.com/bundle/washingtondc-platform-administration/page/script/server-scripting... to know more about reference qualifiers.

@Sandeep Rajput , Yes, there's a reference qualifier condition in one of those fields. And I am still looking for solution to this task

 

Thanks.

Sumanth16
Mega Patron

Hi @Austine ,

 

Client script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

 

    if (isLoading || newValue == '') {

 

          return;

 

    }

 

   

 

    var ga = new GlideAjax('GetCustomerData');

 

  ga.addParam('sysparm_name','getFieldValue');

 

  ga.addParam('sysparm_field','u_reference_1');

 

  ga.addParam('sysparm_user_id', g_form.getValue('caller'));

 

      ga.getXML(DoSomething);

 

       

 

      function DoSomething(response) {

 

            var company = response.responseXML.documentElement.getAttribute("answer");

 

            g_form.setValue('company',company);

 

      }

 

}

 

 

Script include:

 

var GetCustomerData = Class.create();

 

GetCustomerData.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

 

    getFieldValue: function() {

 

              var user = new GlideRecord('x_medt2_ihs_customers');

 

              user.get(this.getParameter('sysparm_user_id'));

 

              if(! user.sys_id){

 

                                return false;

 

              }

 

        return user[this.getParameter('sysparm_field')];

 

    }

 

});

 

Please mark as helpful/correct if it helps.

 
Thanks & Regards,
Sumanth Meda

vermaamit16
Kilo Patron

Hi @Austine 

 

Did you got a chance to read the below article :

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

 

You can give it a try using the Auto Populate Feature as you are dealing with reference fields only.

 

Thanks and Regards

Amit Verma

Thanks and Regards
Amit Verma