Catolog Item Variable's Default Value - can I dot.walk to Manager displaying on User record?

richelle_pivec
Mega Guru

On a catalog item, I have the User who opens the request auto-populating the "Ask on Behalf of the this User" field.

I then have the Phone field populating with that user's phone number. This is the code I have in the "Default Value" field for Phone number:

javascript: var userPhone; var user = new GlideRecord('sys_user'); if (user.get(gs.getUserID())) {userPhone = user.phone}; userPhone;

I'd like to do something similar for the Manager from the user's record in a field on the catalog item:

javascript: var userManager; var user = new GlideRecord('sys_user'); if (user.get(gs.getUserID())) {userManager = user.cost_center.manager}; userManager;

The difference between the "phone" and "manager" field on the user's record is that the manager field is actually a reference field on the "cost center" table…

Is it possible to dot.walk to that manager field in the Default Value of the Dictionary of the Manager field on the catalog item?

Thanks,

Richelle

1 ACCEPTED SOLUTION

richelle_pivec
Mega Guru

Here's how I got this to work.


1. On the catalog item I changed the Manager field type to Lookup Select Box.


Lookup from table: Cost center [cmn_cost_center]


Lookup value field: Sys_id


Lookup lable field(s): manager


Default value: javascript:getCostCenterSysId(getReqfor(gs.getUserID()));


Reference Qualifier: Active = True



2. I also added the Cost Center (cost_center) Reference field to the catalog item:


Reference: Cost center [cmn_cost_center]


Default Value: javascript:getCostCenterSysId(getReqfor(gs.getUserID()));


Reference Qualifier: Active = True



3. For the Catalog Client Script:


Type: onChange


Variable name: cost_center


Script:


function onChange(control, oldValue, newValue, isLoading) {



if(newValue == oldValue){


return;


}



var gp = new GlideRecord('cmn_cost_center');


var cc = g_form.getValue('cost_center');


gp.addQuery('sys_id',cc);


gp.query();


if(gp.next()){


  g_form.setValue('cc_manager', gp.sys_id);


  }


}



Now when I open a new item for this, the "Requested By" user from the catalog populates the "Request For" field and the phone number, cost center, and manager from that user's record populate their corresponding fields as well. This is for our Data Security Exception catalog item, so I need them all to tie together so I can send a notification to the manager of the cost center whenever a request is made by someone in the cost center.



thanks again for your help,



Richelle


View solution in original post

6 REPLIES 6

Chuck Tomasi
Tera Patron

Hi Richelle,



If your manager variable is a reference field, then you are all set. You are copying a sys_id from the user's manager field in to a reference variable. Reference variables (and fields) hold sys_ids. No additional dot-walking needed.



You might also want to take a look at using GlideAjax from an onChange form since this appear to be changing values.



GlideForm (g form) - ServiceNow Wiki


Client Scripts - ServiceNow Wiki


GlideAjax - ServiceNow Wiki


Client Script Best Practices - ServiceNow Wiki      


Abhinay Erra
Giga Sage

Yes you can.


Ajai S Nair
Giga Guru

You can set manager by using:


javascript:gs.getUser().getManagerName() if the variable is a reference field



These links may help you:


Getting a User Object - ServiceNow Wiki


Default Value for Service Catalog Variable


I tried the "javascript:gs.getUser().getManagerName()" code in the default value field (and various other combinations as well), and it does not seem to be working. I can pull values that are directly on the user record (even referenced ones), but this one is a reference field that is on another table and not a reference field directly on the user table...The manager is tied to the cost center the user is in and not the user him/herself.



I'll try the catalog client script route next...