how to pre populate catalog variables with user table data

karendk
Kilo Explorer


On our catalog forms we have a standard variable set that is pre populating the requested for value.   We use this standard variable set on almost all of our catalog items and it's working fine.   I need to modify one catalog item to pre populate more fields from the user table based on that requested for value.   I'm playing with this catalog client script shown below that runs based on the variable: v_requested_for field.

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

  if(newValue != '')   {  

    g_form.getReference(v_requested_for, loadInfo);

  }  

}  

function loadInfo(user_ref) {  

      g_form.setValue('v_email', user_ref.email);  

g_form.setValue('v_manager', user_ref.manager);

g_form.setValue('v_work_phone', user_ref.phone);

g_form.setValue('v_comp_code', user_ref.company);

g_form.setValue('v_pa', user_ref.u_personanel_area);

g_form.setValue('v_ou', user_ref.u_org_unit);

g_form.setValue('v_cc', user_ref.cost_center);

}

When I do "try it" on my catalog item, I get the following error:    

onChange script error: ReferenceError: 'v_requested_for' is undefined function h_312a23f70c2d4680071b73316cfbd4bc(control, oldValue, newValue, isLoading) {       if(newValue != '')   {       g_form.getReference(v_requested_for, loadInfo);   }     }

What am I doing wrong?

Thanks,

Karen

1 ACCEPTED SOLUTION

manikorada
ServiceNow Employee
ServiceNow Employee

Karen,



You can't do the dot walking in Client Script. You need to either use GlideAjax to pull the information from Server Side or use a GlideRecord in client script itself(which is not the best pratice)



So, it would be something like:


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


  if(newValue != '')   {


  var user_ref = g_form.getReference('v_requested_for', loadInfo);


  }


}


function loadInfo(user_ref) {


var loc = new GlideRecord('cmn_location');


  loc.addQuery('sys_id',user_ref.location);


loc.query();


if(loc.next())


{


g_form.setValue('v_location', loc.u_code);


}


      g_form.setValue('v_email', user_ref.email);


g_form.setValue('v_pernr', user_ref.employee_number);


g_form.setValue('v_position', user_ref.title);


// g_form.setValue('v_location', user_ref.location);


g_form.setValue('v_id', user_ref.user_name);


g_form.setValue('v_manager', user_ref.manager);


g_form.setValue('v_work_phone', user_ref.phone);


g_form.setValue('v_comp_code', user_ref.company);


g_form.setValue('v_pa', user_ref.u_personanel_area);


g_form.setValue('v_ou', user_ref.u_org_unit);


g_form.setValue('v_cc', user_ref.cost_center);


}


View solution in original post

7 REPLIES 7

manikorada
ServiceNow Employee
ServiceNow Employee

Karen,



Have your script as:



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


  if(newValue != '')   {


  var user_ref = g_form.getReference('v_requested_for', loadInfo);


  }


}


function loadInfo(user_ref) {


      g_form.setValue('v_email', user_ref.email);


g_form.setValue('v_manager', user_ref.manager);


g_form.setValue('v_work_phone', user_ref.phone);


g_form.setValue('v_comp_code', user_ref.company);


g_form.setValue('v_pa', user_ref.u_personanel_area);


g_form.setValue('v_ou', user_ref.u_org_unit);


g_form.setValue('v_cc', user_ref.cost_center);


}


Thank you.   There seems to be one more step for me to take if the field I need to pull in is a reference field.   Would I need to dot walk in my script?   For example, manager is a reference field back to user table.   Instead of pulling a value, it's pulling a sysid value.   Would I need to do something like :g_form.setValue('v_manager', user_ref.manager.name); to pull back the manager's name?


In short, yes


You can't actually dot-walk on the client side as it is a lightweight version of the full server-side GlideRecord.   You only have access to the values in the record itself.



Do you really need the manager's name?   Can you use a reference field instead for the manager variable so you can use the sys_id?