Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

How to use getJournalEntry(-1) to produce an array of strings?

CharlesR1
Kilo Guru

Hello,

The business rule below runs when we close an Incident task. It creates a KB article and everything is populated correctly with the exception of the history , which shows everything in a huge text block, rather than showing as an array of strings. I used ' var na = cmt.split("\n\n");' but this is not working.

Any ideas?

Thanks in advance,

Charles

function onAfter(current, previous) {

      var candidate = new GlideRecord("kb_knowledge");

      var pNumber = current.number;

      var pTitle = current.u_task_title;

      var cmt = current.work_notes.getJournalEntry(-1);

      var na = cmt.split("\n\n");

      candidate.short_description = (pNumber + ' ' + pTitle);

      candidate.topic = 'Incident Tasks Reports';

      candidate.workflow_state = 'published';

      candidate.text = "<h3>Description:</h3>" + current.description + "<br/><h3>Task Status is:</h3>" +       current.u_task_status + "<br/><h3>History:</h3>" + (na);

      candidate.cmdb_ci = current.cmdb_ci;

      candidate.sys_domain = current.sys_domain;

      candidate.insert();

      gs.addInfoMessage('Knowledge article ' + candidate.number + '   has been published in the Knowledge Base');

current.update()

}

1 ACCEPTED SOLUTION

Brad, Goran,



Thank you both for your help. I have not managed to get this to work by replacing the values - instead I changed the script as follows, and this now works fine:



From:


var cmt = current.work_notes.getJournalEntry(-1);


var na = cmt.split("\n\n");



To:


var cmt = current.work_notes.getHTMLValue();



Thanks again,



Charles


View solution in original post

14 REPLIES 14

Ok thanks Brad - I don't know how to use the replace() method - I've looked for examples on the wiki and community but I can't find anything that helps - would you be able to point me in the right direction?



Thanks,



Charles


replace('WHAT_YOU_LOOKING_FOR','PUT_THIS_INSTEAD');



in your case probably something like .replace('<br>','\n');


Here is also a good example:


JavaScript String replace() Method


Thanks Goran - this is a new one for me!



I have changed the code as below, but now the result is undefined:



function onAfter(current, previous) {


      var candidate = new GlideRecord("kb_knowledge");


      var pNumber = current.number;


      var pTitle = current.u_task_title;


      var cmt = current.work_notes.getJournalEntry(-1);


      var na = cmt.split("\n\n");


      var rep = na.replace("\n","<br>");


      candidate.short_description = (pNumber + ' ' + pTitle);


      candidate.topic = 'Incident Tasks Reports';


      candidate.workflow_state = 'published';


      candidate.text = "<h3>Description:</h3>" + current.description + "<br/><h3>Task Status is:</h3>" +       current.u_task_status + "<br/><h3>History:</h3>" + (rep);


      candidate.cmdb_ci = current.cmdb_ci;


      candidate.sys_domain = current.sys_domain;


      candidate.insert();


      gs.addInfoMessage('Knowledge article ' + candidate.number + '   has been published in the Knowledge Base');



current.update()


}



Where am I going wrong?



Thanks again


Brad, Goran,



Thank you both for your help. I have not managed to get this to work by replacing the values - instead I changed the script as follows, and this now works fine:



From:


var cmt = current.work_notes.getJournalEntry(-1);


var na = cmt.split("\n\n");



To:


var cmt = current.work_notes.getHTMLValue();



Thanks again,



Charles