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

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.


OK, so I have some of this working:



I have a Display BR with the following script setup:



g_scratchpad.tn = current.task.sys_class_name;



and a UI Policy with this:



function onCondition() {


g_form.setLabelOf('task', g_scratchpad.tn);


}



That at least sets the field label correctly at time of load.   I can't get the OnChange to work though - I keep getting 'null'.



find_real_file.png


find_real_file.png


You need to call the specific function by adding this between lines 5 and 6 of your client script:



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


ga.addParam('task_id', newValue);



Then, in your script include, add this between lines 4 and 5:



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



You will have the task's sys_id at that point. Query the task table for that sys_id, get the table name, query the sys_db_object table for that table name, and get the label.



There may be an easier, more straight-forward way, but that's the best I can come up with at the moment.


Can't I skip the Task table query and just feed back the task.sys_class_name?


That would give you "change_request" or "incident" or "pm_project_task." If you want the label, you would need to do another query to get it.