Dot walking in a Client Script

Wayne Richmond
Tera Guru

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

3 REPLIES 3

eican
Kilo Guru

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.


justin_drysdale
Mega Guru

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;


          }


}


kalindaskalov
Giga Contributor

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.