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.

Need to set Label of Field based on Task Type

Shane J
Tera Guru

I need to set that label for 'Task' (for Time Cards in this case) to the Task Type of the record within the field.

function onCondition() {

    var task1 = g_form.getReference('task');

        g_form.setLabelOf('task', task1.getTableName());

}

This nets me 'task' for the label, I suppose it is the task table, but not what I want.   So I tried this:

function onCondition() {

    var task1 = g_form.getReference('task');

g_form.setLabelOf('task', task1.sys_class_name);

}

This nets me (for example) 'pm_project' for a Project.   I'd like to build this so I don't have to setup every possible option for types of records we could have in there in a way that doesn't look goofy to the User, thus why I want the table name.

1 ACCEPTED SOLUTION

In that case, I would recommend using both methods and getting creative with when each is called. If starting from a Task's related list, use the onDisplay Business Rule and a client script that sets the label. If starting from Time Card directly, use an onChange client script that calls a script include to return the value of the table's label and set the field label.


View solution in original post

18 REPLIES 18

I think we're crossing wires.   I should be able to pass the task.sys_class_name as a parameter to the Script Includes via the Client Script, then use that parameter to query the sys_db_object table in the Script Includes to get the Label from there and return it as an 'answer' - right?


I realize now that the entire reason you have me doing two queries is to avoid getReference.


I don't know of any way to get the table name of the reference field directly on the form.


CS:



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


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


  return;


  }


  var ga = new GlideAjax('TaskLabelUpdate');


  ga.addParam('sysparm_name','getTaskLabel');


  ga.addParam('task_id', newValue);


  ga.getXML(doAlert);


}




// Callback function to process the response returned from the server


function doAlert(response) {


  var answer = response.responseXML.documentElement.getAttribute("answer");


  g_form.setLabelOf('task', answer);



}



Script Includes, set to 'Client Callable':



var TaskLabelUpdate = Class.create();


TaskLabelUpdate.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  getTaskLabel: function() {


  var taskID = this.getParameter('task_id');


  var tn = new GlideRecord ('task');


  tn.addQuery('sys_id', taskID);


  tn.query();


  while (tn.next()){


  var thename = tn.sys_class_name;


  alert(thename);


  var tab = new GlideRecord('sys_db_object');


  tab.addQuery('name', thename);


  tab.query();


  while (tab.next()){


  var retVal = tab.label;


  return retVal;


  }


  }


  },



      type: 'TaskLabelUpdate'});




****



Still getting 'null' and losing my mind.   I know my query is sound because I setup a BR to verify that's working.  


Try removing this line from your script include: alert(thename);



It might be erroring out since that's a client-side function and you're trying to run it server-side.