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

Harsh Vardhan
Giga Patron

are you getting value in DeptVar variable ?

 

if not then put the alert(DeptVar); and see what exactly it's giving you.  

Well the DeptVar value is populating the reference field but the dependent reference fields do not update, but I think they are not updating as its not matching it for some reason. I can confirm there is no trailing space or anything like that either.

 

Passing the DeptVar to the alert also does show the department name as expected.

find_real_file.png

you can glide record on department table to get the sys id and then set it. 

 

i would suggest create one script include here (client callable should be checked) then use glide ajax to set the result in your field. 

The practice table and the department tables are not related and don't reference each other, so do I still need the sys id?

They might match on name but not always as the department field does also have departments of users in the head office. Those wont match the practices in the practice table.

Its an annoyance but its the way our AD is currently being used (which then syncs to department in servicenow).

If I do still required the sys id, could you explain what needs to happen with the script include? I have not been able to get my head around that yet at all.