populate reference field with display value from another reference field

James Roberts
Giga Contributor

So i am still very new to coding let alone coding servicenow scripts

Due to reasons we have have both departments and locations syncing to the caller_id.department from AD

I have gone and created a table with our sites and is populated via an import from a combinations of other data sources.

What i would like to do is to populate this practice field with the caller_id.department display value when it hasn't already been filled in/set. The script is doing but this is not actually "matching" or then populating dependent fields correctly.

To be clear the value is correct and would match if you typed it in to the field manually but when it auto populates using the script below the related reference fields do not update and when you click save it says "Match not found, reset to original"

 

(Alerts in place for testing)

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	var cpractice = g_form.getValue('u_practice_code');
	if(cpractice==""){
		alert("is empty");
		var caller = g_form.getReference('caller_id', showcallerDept);
	}
	else{
		alert("is not empty");		
	}
   //Type appropriate comment here, and begin script below

function showcallerDept(caller) {
	var DeptVar = g_form.getDisplayBox("caller_id.department").value;
	g_form.setValue('u_practice_code',DeptVar);

	}

}

 

Just a note to say I will be away for a few days after today so I may not mark an answer or reply until Tuesday.

11 REPLIES 11

If i see your client script you are getting caller department value (eg: Colwyn Bay), now write glide record on table which is reference on u_practice_code field. 

 

Sample Code:

 

Script include:

 

var getDep = Class.create();
getDep.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	
	det: function(){
		
		var id = this.getParameter('sysparm_user_name');
		var res;
		var gr = new GlideRecord('sys_user');
		gr.get(id);

		gs.log('name is'+gr.department.getDisplayValue());

		res=gr.department.getDisplayValue();
		
		var gr2 = new GlideRecord('u_practice_code table name');
		gr2.addQuery('name', res);
		gr2.query();
		if(gr2.next()){
			gs.log(' id is '+ gr2.sys_id);
			
			return gr2.sys_id;
		}
		
		
	},
	
	type: 'getDep'
});

 

Client script:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	var cpractice = g_form.getValue('u_practice_code');
	if(cpractice==""){
		alert("is empty");
		var caller = g_form.getValue('caller_id');



        var ga = new GlideAjax('getDep');
        ga.addParam('sysparm_name', 'det');
        ga.addParam('sysparm_user_name',caller);
        ga.getXML(HelloWorldParse);

	}
	else{
		alert("is not empty");		
	}
   //Type appropriate comment here, and begin script below


 
function HelloWorldParse(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
  alert(answer); 

}

}

 

 

Note: in script include second glide record , you have to add the table which you have used in u_practice_code field. 

 

Give a try and see the logs and alert . 

Hi Harshvardhan

 

Getting there i think, This alerted a sys ID but its still not filling in the practice reference field still.

 

i added g_form.setValue('u_practice_code',"answer")into your function and it kind of registers that somethings been filled in as the required indicator changes from red to black but it does not populate with anything visibly nor does it show after saving, the field required indicator returns to red after saving too.

Vishal Khandve
Kilo Sage

Hi james,

on which field you are writing the onchange scirpt?

remove the code inside of function and add this

function showcallerDept(caller)

{

 g_form.setValue('u_practice_code',caller.department);

 

Thanks,

Vishal

similar result to what I'm having with Harshvardhan code.

 

Field remains empty but the required indicator goes black indicating that its updated, nothing shows in the box and when saved it reverts to being red.

 

The on change is triggered when the caller is changed.

 

the u_practice_code is a reference to the practice table (custom table, not out of the box)

the department is a reference to the cmn_department table which I'm pretty sure is out of the box

AbhishekGardade
Giga Sage

Hello James,

Use Script Include instead of using getReferance();

# Client Script Code:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var cpractice = g_form.getValue('u_practice_code');
var caller = g_form.getValue('caller_id');

if(cpractice==""){
alert("is empty");
var siCall = new GlideAjax('callerInformation');
siCall.addParam('sysparm_name','getUserDetails'); // function name in script Include
siCall.addParam('sysparm_caller', caller);
siCall.getXML(getCallerInfo);
}
else{
alert("is not empty");
}
//Type appropriate comment here, and begin script below
function getCallerInfo(response) {

var DeptVar= response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_practice_code',DeptVar);
}

}

 

#SCRIPT INCLUDE:

find_real_file.png

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

getUserDetails: function() {
var callerID = this.getParameter('sysparm_caller');
var grUser = new GlideRecord('sys_user');
grUser.addQuery('sys_id',callerID);
grUser.query();
if(grUser.next()){

var dept = grUser.department.name ;
return dept;
}
},
type: 'callerInformation'
});

Please mark as Correct Answer/Helpful, if applicable.
Thanks!
Abhishek Gardade

Thank you,
Abhishek Gardade