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

Thank you both to Chris Martin and Antony for your detailed explanation. Thank you once again.