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.

copy comments from one task to another task

SnowDEV2
Kilo Expert

I have a requirement where I need to copy the comments entered in the first task to next task..

I have created a BR but no luck.. could anybody please help on this.

 

BR: push comments

when: before insert/update

 

script:

 

var gr = new GlideRecord('sc_task');

gr.addQuery('parent',current.sys_id);

gr.addQuery('short_description', "xyz");

gr.query();

while (gr.next()) {

            var num = current.number;

          gr.comments = current.comments.getJournalEntry(-1);

          gr.update();

 

    }

8 REPLIES 8

Subhajit1
Giga Guru

Could you try running this BR as After Insert/Update


randrews
Tera Guru

for maintenance purposes i would probably put the function to copy the comments in   the create task item in the workflow.. a year from now when you are trying to figure out where that came from it will be easier to find there than in a BR.



you should be able to use almost the same function but set task.comments = xxxx



just put it in the advanced script section and look up the previous task like you did in the br.


tltoulson
Kilo Sage

If I understand the requirement correctly, when a new sc_task is created it should copy the comments from the previous sc_task.   If this is the case, here is what the script should look like:



BR: push comments


when: before insert/update



script:



(function() {


        var gr = new GlideRecord('sc_task');


        gr.addQuery('parent', current.parent.sys_id); // Get an sc_task with the same parent as the current one


        gr.addQuery('short_description', 'xyz');


        gr.query();


        if (gr.next()) {


                  current.comments = gr. comments.getJournalEntry(-1); // Need to set the current comments not the GlideRecords


        }


})();




I wrapped the whole thing in a self-executing anonymous function because this is a good practice to keep the global BR context clear.


I used to do it that way a lot Travis and started changing.. because if you change the short description of a task you can mess it up...



instead what i do now is put this line at the bottom of any task i need to find...



workflow.scratchpad.rma_task = task.sys_id;



now when you go to find the task you add


task1.addQuery('sys_id',workflow.scratchpad.rma_task );



just seems a lil cleaner to me.