The CreatorCon Call for Content is officially open! Get started here.

Catalog Client Script to set reference field to current user

matthew_strecke
Kilo Contributor

I am working on a SC item and I need to populate the name field (reference field) with the current user when a certain item is picked on the SC item. This is my code so far but I can't seem to get it to work. Any suggestions?

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

  if (isLoading || newValue == '') {

  return;

  }

  var myself = g_form.getValue('voicemail_for_who');

  var id = g_form.getValue('name');

  if(myself == 'Myself') {

  var user = new GlideRecord('sys_user');

  user.addQuery('sys_id',id);

  user.query();

  if (user.next()) {

  g_form.setValue('name', user.name);

  }

  }

}

1 ACCEPTED SOLUTION

You need not do GlideRecord in that case. You can fetch the logged in user info at client side with help of g_user.



Please try with below script.



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


  if (isLoading || newValue == '') {


  return;


  }


  var myself = g_form.getValue('voicemail_for_who');


  var id = g_form.getValue('name');


alert(myself); //This is for testing purpose to check what value you get in myself variable.


  if(myself == 'Myself') {


g_form.setValue('name', g_user.userID); //use this if name is a reference field


g_form.setValue('name', g_user.userName;); //use this line if name is a string field.


}


}



Reference:


GlideUser (g user) - ServiceNow Wiki


View solution in original post

5 REPLIES 5

Try using the client-side GlideUser, e.g.


g_form.setValue('name', g_user.userName);



https://developer.servicenow.com/app.do#!/api_doc?v=helsinki&id=c_GlideUserAPI