Copy Journal Entries & Keep in Date Order

Sue Frost
Giga Guru

I have an existing UI Action which copies a case (part of a custom app) to a new record. A number of fields from the original case are written to the new one.

There is a request to also copy over the work notes (work_notes) journal field. I've found / tweaked code to do that but the entries are not being copied in date order. The code I've found to sort an array is not doing anything.

If someone could point out what's wrong with this code or how it should look to sort the journal entires - newest at the top - I'd very much appreciate the help.

var notes = current.work_notes.getJournalEntry(-1); //gets all journal entries as a string where each entry is delimited by '\n\n'

notes.sort(function(a, b){

return a.sys_created_on-b.sys_created_on

})

var na = notes.split("\n\n");                                             //stores each entry into an array of strings

for (var i = 0; i < na.length; i++){

  gs.print(na[i]);

  gr.work_notes = na[i];

};

var msg = (gs.getMessage('copy.case'));

gr.work_notes = msg + current.number;

1 ACCEPTED SOLUTION

Sue Frost
Giga Guru

Popping in to post the resolution to this issue.



The issue turned out to be with the date-time stamps on the journal entries on the newly copied entries - the speed of SN is such that multiple entries are created with the same date/time stamp and so appear in a random order.



I wound up resetting the date/time stamp to that of the original journal entry, preserving the original date/time stamp and the original order.



------


function runMsgValidation(){


  var msgCon = getMessage('copy.case.receiveddate.message');


  var con = alert(getMessage('copy.case.receiveddate.message'));


  if(con){


  return true;   //Abort submission


  }


  //Call the UI Action and skip the 'onclick' function


  gsftSubmit(null, g_form.getFormElement(), 'Copy_Case_Action'); //MUST call the 'Action name' set in this UI //Action



}


current.update();


doInsertAndStay();


function doInsertAndStay() {


  var gr = new GlideRecord('u_case');


  gr.initialize();


  gr.u_request_from = current.u_request_from;


  //gr.u_received = current.u_received;


  gr.u_received = nowDateTime();


  gr.short_description = current.short_description;


  gr.description = current.description;


  gr.u_risk_type = current.u_risk_type;


  gr.u_product_type = current.u_product_type;


  gr.u_product_sub_type = current.u_product_sub_type;


  gr.u_region = current.u_region;


  gr.u_brokerage=current.u_brokerage.sys_id;


  gr.u_source = current.u_source;


  gr.u_sub_source = current.u_sub_source;


  gr.u_language = current.u_language;


  gr.u_line_of_business = current.u_line_of_business;


  gr.u_policy_prem = current.u_policy_prem;


  gr.u_copied_case = current.sys_id;


  gr.u_name_insured = current.u_name_insured;


  gr.assigned_to = gs.getUserID();



//new


  gr.insert();



  var grJ = new GlideRecord('sys_journal_field');


  grJ.addQuery('element_id', current.sys_id);


  grJ.orderByDes('sys_created_on');


  grJ.query();



  while(grJ.next()){


  var grJN = new GlideRecord('sys_journal_field');


  grJN.initialize();


  grJN.name = grJ.name;


  grJN.element_id = gr.sys_id;


  grJN.value = grJ.value;


  grJN.element = grJ.element;


  grJN.insert();


  grJN.sys_created_on = grJ.sys_created_on;


  grJN.sys_created_by = grJ.sys_created_by;


  grJN.update();


  }



  //end new



  var msg = (gs.getMessage('copy.case'));


  gr.work_notes = msg + current.number;


  gr.u_entity = 'u_case';


  gr.setWorkflow(false);


  // gr.insert();


  gr.update();


  action.setRedirectURL(gr);


}


View solution in original post

10 REPLIES 10

Community Alums
Not applicable

Hi Kevan,

 

I'm also looking for a function to copy existing activity entries over to a new task, whilst preserving the timestamps of the originals. By the looks of this thread, it isn't possible to do that immediately (which is important, here).  Did you (or anyone else on this thread) manage to achieve that at all? I'd guess the use of sys_history_line really dictates anything that can be done, although I might take a look at the history line API to see if that could help.

 

Thanks,

 

Jason