Copy fields from HR Case to HR Task

davidjo
Kilo Explorer

Hi,

We have a related links "Add Task" on or HR Case form that allows us to create a task from within a case.   There is a UI Action that controls this and I would like to copy some field values (short_description and description) from the case to the newly created task and have been unsuccessful trying to add script that would copy the information.   Does anyone have any ideas on how to accomplish this.   Copy of script below for reference.

function addTask(btn) {

var associatedTables = g_scratchpad.associated_task_tables.evalJSON();

if (associatedTables.length == 1)

gsftSubmit(null, g_form.getFormElement(), btn.id);

else

openAddTaskPopup();

}

function openAddTaskPopup() {

var dialog = new GlideDialogWindow("sm_add_task_popup");

dialog.setWidth("400");

dialog.setTitle(getMessage("What type of task would you like to create?"));

dialog.setPreference("associated_task_tables", g_scratchpad.associated_task_tables);

dialog.setPreference("parent_id", g_form.getUniqueValue());

dialog.render(); //Open the dialog

}

if (typeof window == 'undefined')

serverAddTask();

function serverAddTask() {

var taskTable = new sn_sm.SMConfiguration().getTaskTable(current);

var uri = action.getGlideURI();

var path = taskTable + '.do';

var query = 'parent='+current.sys_id.toString();

uri.set('sys_id', '-1');

uri.set('sysparm_query', query);

action.setRedirectURL(uri.toString(path));

action.setNoPop(true);

action.setReturnURL(current);

}

Thanks,

David

3 REPLIES 3

Michael Fry1
Kilo Patron

I'd used a business rule to query for the fields you want to set from the Case. Something like:



(function executeRule(current, previous /*null when async*/) {


   


      var hr = new GlideRecord('sn_hr_core_case');


      hr.get(current.parent);


      current.short_description = hr.short_description;


      current.description = hr.description;    


      hr.update();


   


})(current, previous);


Hi Michael,



I was unable to get the business rule to work and I decided to create a new


UI Action using the script below and was able to get this to work. Thanks


for your assistance.



var newhr_task = new GlideRecord('hr_task');



newhr_task.initialize();


newhr_task.short_description = current.short_description;


newhr_task.description = current.description;


newhr_task.parent = current.sys_id;



newhr_task.insert();



gs.addInfoMessage('hr_task ' + newhr_task.number + ' created.\nIt has been


related to the Parent hr_task:' + newhr_task.parent.number );


action.setRedirectURL(newhr_task);


action.setReturnURL(current);



On Mon, Aug 28, 2017 at 8:43 PM, Michael.Fry <


randrews
Tera Guru

why are we copying fields?



this duplicates the data in the database, is redundant, and creates TWO sources for the fields.



the only reason you should be copying the field is if they are ever going to diverge   so on the task after they get it they will change it but want to change it ONLY on the task not on the hr case...



Normally if a field is shared... what you want to do is SHOW it on the child from the parent... so instead of creating a br/script to copy the short description .. just put in the case.field name on the form... if for some reason you DO need to copy the field   <for example assignment group should START the same but the task might get reassigned>   then you do not need a script... create a before br that runs on insert only and in the actions select the form field <ie assignment group> and then same as and dot walk up to hr_case.assignment group in the field picker.


should be a much cleaner and easier to maintain solution for you!