How to Attach a Knowledge-Base Article to a Task, by Using a Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2014 11:55 AM
Hello All,
I need to be able to attach a knowledge-base article to a catalog task in a workflow by using a script within this catalog task. Nothing that I have tried has worked yet. The biggest problem with this, as far as I can tell, is that I cannot access the task's sys_id until after it is created. Is there a way around this, or any way of doing this at all?
Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2014 12:39 PM
We had found this script below and tried adding it to the workflow step that creates the task. It did exactly what we wanted.... but it also did a lot more. We found that it would attach the KB to the task that was created, but it also looped through all of the other sc_tasks and attached it to every single task ever created. If we ran the workflow again, it would attach a second copy, and so on.
Would anyone have a suggestion on how to modify the script so that it would only run on the record that is being created on the workflow?
function attach_kb() {
var item = new GlideRecord("sc_task");
item.addQuery("task",current.sysapproval );
item.query();
while (item.next()) {
var kb_item = new GlideRecord('m2m_kb_task');
kb_item.initialize();
kb_item.task = item.sys_id;
kb_item.kb_knowledge.setDisplayValue('KB0010425');
kb_item.insert();
}
}