Get Reference of a record from look up select box

Gokul Nath
Kilo Sage

Hi All,

I have a requirement, of finding the reference record selected in other field and populating into a reference field

 

Field 1 type: Lookup Select Box

Field 2 Type: Reference

 

Both have same tables configured,

when i select a record in field 1, the reference record should be populated in the second field which is not happening, please help with your suggestions

Gokul24_0-1681283407291.png

 

 

2 ACCEPTED SOLUTIONS

Mohan raj
Mega Sage

Hi @Gokul Nath ,

 

To populate the record in reference field based on lookup select table like this

Mohanraj_0-1681284407364.png

by using on change client script:

 

field name : field 1

script :

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

   //Type appropriate comment here, and begin script below
	var field1 = g_form.getValue('field_1');
	g_form.setValue('field_2',field1);
   
}

 

If my response helps you to resolve the issue close the question by Accepting solution and hit thumb icon. From Correct answers others will get benefited in future.

Regards,

Mohan.

View solution in original post

Pavankumar_1
Mega Patron

Hi @Gokul Nath  ,

You can achieve it through only onchange catalog client script or client script +script include.

1. Onchange Client Script

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    g_form.setValue('variable_2_for_case', g_form.getValue('variable_1_for_case')); //add your variables
}

or you can try below option aswell

2. You can achieve it through client script + script include.

client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('getCaseValue'); //script include name
    ga.addParam('sysparm_name', 'getDetails'); //function name
    ga.addParam('case_number', newValue); //passing value to server
    ga.getXMLAnswer(setdetails); //callback funtion
    function setdetails(response) {
        g_form.setValue('variable_2_for_case', response);//add your reference variable name
    }

}

Script Include: Make client cabble is true.

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

    getDetails: function() {
        var num = this.getParameter('case_number');
        var grCase = new GlideRecord("sn_customerservice_case");
        grCase.addQuery("number", num);
        grCase.query();
        if (grCase.next()) {
            return grCase.sys_id.toString();
        }
    },
    type: 'getCaseValue'
});

Screenshot (748).png

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

5 REPLIES 5

Mohan raj
Mega Sage

Hi @Gokul Nath ,

 

To populate the record in reference field based on lookup select table like this

Mohanraj_0-1681284407364.png

by using on change client script:

 

field name : field 1

script :

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

   //Type appropriate comment here, and begin script below
	var field1 = g_form.getValue('field_1');
	g_form.setValue('field_2',field1);
   
}

 

If my response helps you to resolve the issue close the question by Accepting solution and hit thumb icon. From Correct answers others will get benefited in future.

Regards,

Mohan.

Hello @Mohan raj 

Thanks for the response, but unfortunately, it doesn't seem to work for me.

 

 

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

   //Type appropriate comment here, and begin script below
	var field1 = g_form.getValue('please_indicate_the_ticket_number_related_to_this_request');
	g_form.setValue('please_indicate_the_ticket_number_related_to_this_request_v1',field1);
   
}

 

 

Gokul24_0-1681284882550.png

Alert is bringing up the case number

Gokul24_1-1681284954470.png

But the field is not setting the value as you get it

Hello @Mohan raj 
Your script worked on making few configuration change to look up field, Thanks for the time

Gokul24_2-1681285447754.png

 

Pavankumar_1
Mega Patron

Hi @Gokul Nath  ,

You can achieve it through only onchange catalog client script or client script +script include.

1. Onchange Client Script

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    g_form.setValue('variable_2_for_case', g_form.getValue('variable_1_for_case')); //add your variables
}

or you can try below option aswell

2. You can achieve it through client script + script include.

client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('getCaseValue'); //script include name
    ga.addParam('sysparm_name', 'getDetails'); //function name
    ga.addParam('case_number', newValue); //passing value to server
    ga.getXMLAnswer(setdetails); //callback funtion
    function setdetails(response) {
        g_form.setValue('variable_2_for_case', response);//add your reference variable name
    }

}

Script Include: Make client cabble is true.

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

    getDetails: function() {
        var num = this.getParameter('case_number');
        var grCase = new GlideRecord("sn_customerservice_case");
        grCase.addQuery("number", num);
        grCase.query();
        if (grCase.next()) {
            return grCase.sys_id.toString();
        }
    },
    type: 'getCaseValue'
});

Screenshot (748).png

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar