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

The catalog item fields are going to be in a permanent record for auditing purposes.   That is why they want this data stored in the record as opposed to just a reference field to go look up the data.  


No, I wouldn't need the manager full name - it could just be the user.name field value that contains their ID.


How would I modify the setValue statement to pull back the manager name or ID?


I've edited my script a bit to attempt to pull value from location table.   Lines in bold is what I added.   In the location table, "u_code" is the value I'm trying to pull back into my catalog field called v_location.



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


  if(newValue != '')   {


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


  var loc = new GlideRecord('cmn_location');


  loc.get(user_ref.location);


  }


}


function loadInfo(user_ref) {


      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_location', loc.u_code);


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);


}



error I'm getting when launching catalog item:



location error.JPG


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);


}