Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Dynamically pass values through to template fields

Jeff Krueger1
Tera Expert

Is there a way to dynamically pass values through the template fields?   For instance, having a short description of "Create mainframe account for <requested_by>" which would pass the matching value through once the template is applied:

find_real_file.png

1 ACCEPTED SOLUTION

Thanks John, I haven't tried your code yet, but I did add a GlideRecord query which did the trick:



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


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


  return;


  }



  var gr = new GlideRecord('sys_user');


  gr.get(g_form.getValue('requested_by'));


  var rb = gr.name;



  if (newValue == 'New Mainframe Account') {


  g_form.setValue('short_description', newValue + ' for ' + rb);


  }


}


View solution in original post

6 REPLIES 6

Hey Jeff,



To answer your question about g_form.getValue giving you a sys_id - that's because you're getting a value of a reference field which always has the sys_id as the field value. It would be nice if something like g_form.getDisplayValue() would work, but that would only run server side and not in a client script such as yours.



I would recommend using the g_form.getReference() function with a callback that outputs the referenced user's title which is what you want.



Something like this in your code:



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


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


  return;


  }



  var rb = g_form.getReference('requested_by', function(ref) {


        return ref.getValue('title');   //title is the field in the user table that shows the full name, but you can choose any field


  });



  if (newValue == 'New Mainframe Account') {


            g_form.setValue('short_description', newValue + ' for ' + rb);


  }


}



I did not test that code, but let me know if that helps you out.



Thanks


Thanks John, I haven't tried your code yet, but I did add a GlideRecord query which did the trick:



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


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


  return;


  }



  var gr = new GlideRecord('sys_user');


  gr.get(g_form.getValue('requested_by'));


  var rb = gr.name;



  if (newValue == 'New Mainframe Account') {


  g_form.setValue('short_description', newValue + ' for ' + rb);


  }


}