- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2016 08:30 AM
I have a requirement to copy a Knowledge article into a Change Task, including any attachments that might be on the article.
Here is the business rule:
// function onDisplay(current, g_scratchpad) { // When to run: Display // Order: 100 // Filter Conditions: Special Instructions Flag is true //gs.log('InforDebug BSR: In Business Rule'); gs.log('InforDebug BSR: kb sys id is -- '+ current.u_infor_spec_instructions); gs.log('InforDebug BSR: html text is -- '+ getKBhtml(current.u_infor_spec_instructions)); var tblName = current.getTableName(); // get table name of record currently running against (allows reusability) var manageAttach = new InforManageAttachments(); // create new instance of scriptInclude current.u_infor_spec_instructions_display = manageAttach.copyHTMLFromKB(current.u_infor_spec_instructions, tblName); // call function to copy HTML and atatchments to record of tblName manageAttach.deleteDups(tblName); // call function to remove duplicate attachments from record of tblName }and the script include
var InforManageAttachments = Class.create(); InforManageAttachments.prototype = Object.extendsObject(AbstractAjaxProcessor, { copyHTMLFromKB : function(kbID, tblName) { var kbRec = new GlideRecord('kb_knowledge'); if (kbRec.get(kbID)) { GlideSysAttachment.copy('kb_knowledge', kbRec.sys_id, tblName, current.sys_id); // Added 4/21/2016 - Tamara - copy all attachments from KB record to CTASK return kbRec.text; } else { return ''; } }, deleteDups : function(tblName) { var grSysAttach = new GlideRecord('sys_attachment'); grSysAttach.addQuery('table_name', tblName); //substitute your table name here grSysAttach.addQuery('table_sys_id',current.sys_id); grSysAttach.orderByDesc('sys_created_on'); grSysAttach.query(); var lastID = 'not_a_match'; var lastFile = 'not_a_match'; while (grSysAttach.next()) { gs.log("scriptInclude - Processing sys_attachment record " + grSysAttach.sys_id + " and table_sys_id " + grSysAttach.table_sys_id); var isDuplicate = (lastID == grSysAttach.table_sys_id) && (lastFile == grSysAttach.file_name); lastID = grSysAttach.table_sys_id; lastFile = grSysAttach.file_name; if (isDuplicate) { grSysAttach.deleteRecord(); gs.log(grSysAttach.table_sys_id + ' ' + grSysAttach.table_name + ' ' + grSysAttach.file_name + ' ' + grSysAttach.sys_created_on + ' ' + isDuplicate); } } return; }, type: 'InforManageAttachments' });The consistent behavior is that only one attachment is copied from the Knowledge article to the Change Task, even though multiple attachments are added to the Knowledge article. I used the documentation from Useful Attachment Scripts - ServiceNow Wiki and Copy Attachments from Record to Record - ServiceNow Wiki.
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2016 09:21 AM
I worked with support and had to rewrite the script I was using that copied duplicates. I don't think the underlying copy was fixed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2017 01:58 PM
I have a similar issue with the GlideSysAttachment.copy() command. [like Where is it documented so that I might have read about .size() in the 1st place]
Specifically, I am now wondering how to attach an attachment. I had planned on using GlideSysAttachment.copy(). I understand about getting the array of attachments. But what call am I to use to attach the individual (in this case) KB Articles to an incident or other record?
Frankly, it looks easier to go and find my attachments in sys_attachment and just copy the data to a new Target record... Is that the best we have do you think?
[update]
The fact that we are only concerned with KB Articles provided a pretty simple solution:
(function executeRule(current, previous /*null when async*/) {
var increc = current.sys_id;
var genrec = current.correlation_id;
var grKB2T = new GlideRecord('m2m_kb_task');
grKB2T.addQuery('task',genrec);
grKB2T.query();
while(grKB2T.next()){
var kba = grKB2T.getValue('kb_knowledge');
doAttachKB(increc,kba);
}
gs.addInfoMessage("KB article(s) from the Genesys Record have been attached to this Incident");
})(current, previous);
function doAttachKB(task,knowledge) {
var grCopy = new GlideRecord('m2m_kb_task');
grCopy.initialize();
grCopy.task = task;
grCopy.kb_knowledge = knowledge;
return(grCopy.insert());
}
I hope this helps others.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2017 11:14 AM
This, was MOST helpful in what I needed to get done. Thank you!
