Moving attachments form RITMS

Mprov113
Tera Contributor

I would like to make an automated task in flow designer that takes a build document attached to a RITM, and upon manager approval of that RITM moves said attachment to a designated Knowledge Base table without the RITM creator needing the ability to modify/write to that table.  Is there a way to automate this process? 

I have found some scripts for copying attachments in ServiceNow but not specific for this use case.

Thank you!  

4 REPLIES 4

Yashsvi
Kilo Sage

Hi @Mprov113,

 

please check below link:

https://www.servicenow.com/community/now-platform-forum/i-need-to-copy-an-attachment-from-ritm-to-a-...

 

Thank you, please make helpful if you accept the solution.

Mprov113
Tera Contributor

Thank you very much for replying to my query, but the solution does not directly cover what I hope to accomplish. 
I have the attachment in a RITM. When the RITM closes "complete", I would like the attachment to be moved to a read-only Knowledge Base "kb_name" that I set up. Is there a way to accomplish this without coding?  If not, what would be the script that produces this condition? 

Thanks again! 

Hi @Mprov113,

The process of moving an attachment from a RITM to a Knowledge Base table upon manager approval using Flow Designer in ServiceNow:

Steps:

1. Create a Script Include:
- Define a function to copy attachments.

var AttachmentUtils = Class.create();
AttachmentUtils.prototype = {
initialize: function() {},

copyAttachment: function(sourceTable, sourceSysId, targetTable, targetSysId) {
var grAttachment = new GlideRecord('sys_attachment');
grAttachment.addQuery('table_name', sourceTable);
grAttachment.addQuery('table_sys_id', sourceSysId);
grAttachment.query();

while (grAttachment.next()) {
var sa = new GlideSysAttachment();
var attachmentData = sa.getBytes(sourceTable, sourceSysId, grAttachment.sys_id);
sa.write(targetTable, targetSysId, grAttachment.file_name, attachmentData);
}
},

type: 'AttachmentUtils'
};

 

2. Create a Flow in Flow Designer:
- Trigger: Set the flow to start when the RITM reaches the manager approval stage.
- Approval Action: Add an approval action for the manager.
- Script Action: Add a script action to create a KB article and copy the attachment.

 

(function execute(inputs, outputs) {
var sourceTable = 'sc_req_item';
var sourceSysId = current.sys_id;
var targetTable = 'kb_knowledge';

// Create new KB article
var kbArticle = new GlideRecord(targetTable);
kbArticle.initialize();
kbArticle.short_description = 'Build Document for ' + current.number;
kbArticle.workflow_state = 'draft';
kbArticle.kb_knowledge_base = 'YOUR_KNOWLEDGE_BASE_ID'; // Set your Knowledge Base ID here
kbArticle.kb_category = 'YOUR_CATEGORY_ID'; // Optionally set a category
kbArticle.u_ritm = sourceSysId; // Link back to the RITM
kbArticle.insert();

var targetSysId = kbArticle.sys_id;

// Copy attachment
var attachmentUtils = new AttachmentUtils();
attachmentUtils.copyAttachment(sourceTable, sourceSysId, targetTable, targetSysId);
})(inputs, outputs);

 

Conclusion:
1. Script Include: Copies attachments from one record to another.
2. Flow:
- Triggered by manager approval.
- Creates a KB article.
- Copies the attachment from the RITM to the new KB article.

Ensure you replace placeholders with actual values for Knowledge Base and category IDs. This setup automates the attachment transfer upon approval.

 

Thank you, please make helpful if you accept the solution.

Mprov113
Tera Contributor

Thank you so much!  I will test this solution in my Dev instance and update this thread with my results. 🙂