- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2023 12:09 AM - edited 04-12-2023 12:10 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2023 12:28 AM
Hi @Gokul Nath ,
To populate the record in reference field based on lookup select table like this
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2023 12:40 AM - edited 04-12-2023 12:47 AM
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'
});
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2023 12:28 AM
Hi @Gokul Nath ,
To populate the record in reference field based on lookup select table like this
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2023 12:36 AM - edited 04-12-2023 12:36 AM
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);
}
Alert is bringing up the case number
But the field is not setting the value as you get it
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2023 12:44 AM
Hello @Mohan raj
Your script worked on making few configuration change to look up field, Thanks for the time
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2023 12:40 AM - edited 04-12-2023 12:47 AM
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'
});
ServiceNow Community MVP 2024.
Thanks,
Pavankumar