Dot walking in a Client Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2014 08:25 AM
I'm trying to populate a reference variable on a catalog item using an onLoad catalog client script but the data I want is in parent field of the field I am referencing and the standard dot walking isn't bringing back a result.
On the User table is a reference field to the Department table. On the Department table is the Parent field. On that is another Parent field and so on until you get to the top level of a department hierarchy. I am trying to dot walk as follows: cust.department.parent.parent.parent.parent.name but this brings back a blank value. Here is the code in full:
<code>
function onLoad() {
var cust = g_form.getReference('newbb_name');
g_form.setValue('newbb_jobtitle', cust.title);
g_form.setValue('newbb_dept', cust.department);
g_form.setValue('newbb_div', cust.department.parent.parent.parent.parent.name);
g_form.setValue('newbb_office', cust.location);
g_form.setValue('bbnew_tel', cust.u_business_phone);
g_form.setValue('bbnew_email', cust.email);
g_form.setValue('bbnew_lineman', cust.u_line_manager);
}
</code>
The rest of the fields work fine apart from the div one. Any ideas why?
Ta
- Labels:
-
Service Mapping
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2014 08:29 AM
You have to do getReference for each level you want to dot-walk.
But this also invokes multiple server query calls.
Performance wise you would be better using a server side script. Either using a "after" business rule once the record was updated or a GlideAjax script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2014 04:06 PM
I may be wrong, but I believe you cannot dot walk like that in a client script. Here is a quick (untested) workaround. It should give you an idea of how to proceed:
function onLoad() {
var cust = g_form.getReference('newbb_name');
g_form.setValue('newbb_jobtitle', cust.title);
g_form.setValue('newbb_dept', cust.department);
//g_form.setValue('newbb_div', cust.department.parent.parent.parent.parent.name);
g_form.setValue('newbb_div',getTopParentName(cust.department));
g_form.setValue('newbb_office', cust.location);
g_form.setValue('bbnew_tel', cust.u_business_phone);
g_form.setValue('bbnew_email', cust.email);
g_form.setValue('bbnew_lineman', cust.u_line_manager);
}
function getTopParentName(data) {
var top = new GlideRecord('sys_user');
top.addQuery('department', data);
top.query();
if(top.next()) {
return top.parent.parent.parent.parent.name;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-17-2021 06:28 AM
I would suggest that if you are looking at a refernce field you do the following.
var something = g_form.getReference('refernce field or variable');
g_form.setValue('somefield',something.the field you are triyng to get);
That should work without a query.