Please make me understand this code.

gourav2
Kilo Expert

There is a UI action on my Knowledge Management form which is named as Create Article and it creates an article from the form upon clicking. Below is the snippet of the code which I am finding difficult to understand, would someone like to explain it for me.

--------------------------------------------------------------------------------------------------

if (current.hasAttachments()) {

              var ids = GlideSysAttachment.copy('kb_submission', current.sys_id, 'kb_knowledge', gr.sys_id);

              ids = ids.toArray();

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

                      var oldandnew = ids[i].split(',');

                      gr.text = gr.text.toString().replaceAll(oldandnew[0],oldandnew[1]);

              }

      }

---------------------------------------------------------------------------------------------------

The code written in the FOR loop is ambiguous for me, thanks a lot for any help!

1 ACCEPTED SOLUTION

Yes Chris, Your guess is absolutely right



In the script above there is a statement [gr.text = current.text;] which is copying the contents of Text field from KB Submission record and pasting as it is in new KB article (going to be created) Text field. Now that, if KB Submission record had attachments then Text field contents include the URLs pointing to sys_attachment record but for KB Submissiion.



Therefore, with statement [gr.text = gr.text.toString().replaceAll(oldandnew[0],oldandnew[1]);], script is replacing those sys_attachment sys_id's with the new sys_ids (from sys_attachment record) which are created for KB article.



@Gourav : I tried explaining it as simple as possible. Please let me know if you are clear with the logic?



Brgds, AM


View solution in original post

5 REPLIES 5

antonymirza
Tera Expert

Hi Gourav,



Would you mind sharing the complete code please? I must say this code is incomplete as I cannot see where have you initialized "gr"



As far as I understand with this part of code, this UI Action is defined on "KB Submission" table. As per the code, system is creating new sys_attachment records for copying attachments from KB Submission to KB Article record (new record). And, after creating new records in sys_attachment record, it's updating the sys_id's of new sys_attachment records in KB Article text field.



Adding the log statements will give you more clarity on this



Brgd, AM


Here is the whole code:



current.approval = 'approved';


current.u_reviewer=gs.getUserID();


current.status="Knowledge Created";


current.update();


generateKB();




function generateKB() {


      if (current.changes())


          current.update();


      var gr = new GlideRecord('kb_knowledge');


      gr.initialize();


      gr.newRecord();


      gr.text = current.text;


      gr.source = current.sys_id;


      gr.sys_domain = current.sys_domain;


      gr.short_description = current.short_description;


      gr.category = current.category;


  gr.u_status =   current.status;


      gr.valid_to = current.valid_to;


  gr.u_category = current.u_category;


  gr.u_type = current.u_type;


      gr.direct = current.direct;


      gr.display_attachments = current.display_attachments;


      gr.topic = current.topic;


  gr.author=current.submitted_by;


  gr.u_article_origin = 'XYZ';


      if (current.hasAttachments()) {


              var ids = GlideSysAttachment.copy('kb_submission', current.sys_id, 'kb_knowledge', gr.sys_id);


              ids = ids.toArray();


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


                      var oldandnew = ids[i].split(',');


                      gr.text = gr.text.toString().replaceAll(oldandnew[0],oldandnew[1]);


              }


      }


  var newID = gr.insert();


      gs.addInfoMessage("Article " + gr.number + " created");


      action.setRedirectURL(gr);


      action.setReturnURL(current);


}


------------------------------------------------------------------------------------------------------


What exactly this statement does:


var oldandnew = ids[i].split(',');


                      gr.text = gr.text.toString().replaceAll(oldandnew[0],oldandnew[1]);


This I am not able to understand. Thank you very much Antony!


This is just a guess, but I believe it's looking within the text of the knowledge article, and replacing the sys_id's of attachments from the original article with the sys_id of the attachments of the copied article.



So, if there are embedded images in the knowledge article, those images get copied, and the links to them replaced so that changes to one don't impact the other.



To be sure, if you do a JSUtil.logObject(ids); in there, I imagine you'll get a better clue.


Yes Chris, Your guess is absolutely right



In the script above there is a statement [gr.text = current.text;] which is copying the contents of Text field from KB Submission record and pasting as it is in new KB article (going to be created) Text field. Now that, if KB Submission record had attachments then Text field contents include the URLs pointing to sys_attachment record but for KB Submissiion.



Therefore, with statement [gr.text = gr.text.toString().replaceAll(oldandnew[0],oldandnew[1]);], script is replacing those sys_attachment sys_id's with the new sys_ids (from sys_attachment record) which are created for KB article.



@Gourav : I tried explaining it as simple as possible. Please let me know if you are clear with the logic?



Brgds, AM