populate reference field with display value from another reference field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2019 08:06 AM
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.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2019 03:32 AM
Hi Abhishek
Same issue as my original post, I'm getting "Match not found, reset to original" when clicking save or update. Its also not updating the reference fields that depend upon it when it is populated by your script.
Cheers
James Roberts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2019 01:11 AM
So a note to all
I've worked this from a different angle using the code from Abhishek Gardade. I created an additional field (called office in this example) on the user which also references the same "practice" table. It grabs the sys_id from the users office field and it populates the practice reference on the incident correctly.
I think the issue with the original method is, it's using the value (department.name) which doesn't populate the reference field correctly but I couldn't use the sys_ID either as that doesn't match on the practice table. I'm guessing (with my limited knowledge) you would have to do some kind of look up (in the script include) on the practice table using the department.name, grab the sys_id that matches the look up and send that back to the client script?!