How to assign table_sys_id on the attachment record on script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-22-2018 12:50 PM
Hi, all,
On the workflow for the service catalog item, I would like to attach the attachment to the service Catalog task.
So far, I am on success to create an attachment record on the Catalog Task activity on the workflow with the following snippet of the code below.
However, when I check the attachment record on the 'sys_attachment.list', 'table_sys_id' is empty which is the reason (I think) a sc_task record generated has no Attachment.
Could anyone give me the guidance to resolve this issue?
Appreciate your help and comment in advance,
Moon 1
---
// dataJosn is the JSON string
task.u_data = dataJson;
try {
// Attach the RITM number to the file name
var fileNameAttach = 'Data_'+current.number+'.json';
var contentTypeAttach = 'application/json';
var gsAttach = new GlideSysAttachment();
var idAttached = gsAttach.write(task, fileNameAttach, contentTypeAttach, galDataString);
gs.info('>>>DEBUG: Task');
gs.info(task);
gs.info('>>>DEBUG: ID of Service Catalog Task Attachement: (' + idAttached +')');
//ToDo: Need to assign table_sys_id to the attachment when table_name is 'sc_task'. sys_attachment table has the attachment record generated.
var att = new GlideRecord('sys_attachment');
att.addQuery('sys_id', idAttached);
att.query();
if (att.next()) {
gs.info('>>>DEBUG: attachment record generated!');
att.table_sys_id = current.parent.sys_id;
att.update();
}
} catch (ex) {
gs.error('Error to create a catalog task. A reason is ' + ex);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-22-2018 12:56 PM
There is an OOB API for this but instead of cluttering up your system with duplicate data I would recommend following this.
https://community.servicenow.com/community?id=community_blog&sys_id=1e5eaaaddbd0dbc01dcaf3231f961939
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2018 02:30 PM
Thank you on your posting.
1. I am using OOB API (https://developer.servicenow.com/app.do#!/api_doc?v=kingston&id=r_SGSA-write_GR_S_S_S) as you saw above my code. But I can't figure out how to set table_sys_id. Could you guide me which OOB API you refer, and it will be the better to give an example code.
2. The link you posted is good to know to render the related attachements with human readble ID (record) as the calculated field value. Thank you, But I need to link between service catalog task record with the attachment which added to the sys_attachment table using my code, but there is no linkage between attachment and service catalog task table since there is no table_sys_id which I don't know how to assign.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-25-2018 05:02 AM
This is the correct one.
https://developer.servicenow.com/app.do#!/api_doc?v=kingston&id=r_SGSA-copy_S_S_S_S
You would have to do it from a business rule on the sc_task table to be able to have the target sys_id.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-25-2018 10:52 AM
Thank you for your guidance!
Just in case to make it clear, I would like to rephrase what I want to achieve.
Goal: create an attachment to the sc_task record. The attachment content is needy to contain variables value from the custom Service Catalog Item.
My approach (be bear with my shortage of experience on SNOW, I am only working with SNOW 3 months).
1. Created a custom workflow (WF) for Catalog Item
2. Built JSON string using the Catalog Item variables on Run Script activity on the WF, and store to the scratchpad
3. On Catalog Task activity (Script section) on the WF, retrieve scratchpad object (json string), and created GlideSysAttachemnt object to the task table using .write() method with Json string content, which returns the id of the attachment, and I verified an attachment record is generated with the correct JSON content and its table is sc_task.
Issue:
1. there is no table_sys_id assigned on the attachment record on sys_attachment table from above code, which I tried to resolve with above original posting and few other ways inside on the WF.
My question:
1. you pointed me to use Attachment COPY method API and guide me to code 'Business Rule' on sc_task. Could you elaborate little more. Are you suggesting 'onload' BIZ rule, take sc_trask's sys_id and update sys_attachment record for which task_sys_id using sc_task sys_id, something like that?
2. On the WF executed on Service Catalog Item, is there anyway to get task sys_id from Catalog Task activity or Create Task activity so that I can assign Attachment object created dynamically with full attribubutes including content and task_sys_id?
All the best,
Moon 1