Want to autopopulate worknote on sc_request, sc_req_item, sc_task record after adding attachment on
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I have requirement that, if on Service portal, catalog item at the time of form submission i add attachment the i want to auto populate work note on sc_request, sc_req_tem, sc_task record that attachment is added. Now current BR is Before and Insert type and working when attachment is added 2nd time after submitting the catalog item on SP and i want to auto populate work note when i add attachment while submission. Give me a solution without disturbing existing script. I want to keep existing script functional.
Current/existing BR -
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
When you initially add an attachment to a Catalog Item on the request form, the table_name is 'sc_cart_item' then it is changed to 'sc_req_item' when the request is submitted. You can either add 'sc_cart_item' as another OR condition in your if(targetTable... condition, or run the existing script also on Update and add the condition when table_name changes to sc_req_item.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Steps
-> Leave your existing Before Insert BR unchanged (handles second and subsequent attachments)
-> create separate Async insert BR on Attachment table
-> In this new BR, detect if the attachment is linked to sc_request, sc_req_item, or sc_task.
-> Check if the attached record was just created or is newly submitted (you can check if created within last X seconds or if work_notes is empty).
-> Add the work note message for the attachment upload on the linked record
Something like this
(function executeRule(current, previous /*null when async*/ ) {
// Only proceed if attachment linked to these tables
var targetTable = current.table_name + '';
if (targetTable !== 'sc_request' && targetTable !== 'sc_req_item' && targetTable !== 'sc_task')
return;
var targetRecord = new GlideRecord(targetTable);
if (!targetRecord.get(current.table_sys_id))
return;
// Optional: check if attachment is within 1-2 minutes of creation (new submission)
var createdDiff = gs.nowDateTime().getTime() - targetRecord.sys_created_on.getGlideObject().getTime();
if (createdDiff > 2 * 60 * 1000) // more than 2 minutes old, skip (optional)
return;
var uploader = gs.getUserDisplayName();
var workNote = 'Attachment Uploaded: ' + current.file_name + ' by ' + uploader + '.\n' +
'Please check the \'Related Attachments\' list to view it.';
targetRecord.work_notes = workNote;
targetRecord.update();
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hope you are doing good.
Did my reply answer your question?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader