The CreatorCon Call for Content is officially open! Get started here.

Want to autopopulate worknote on sc_request, sc_req_item, sc_task record after adding attachment on

MayurChaudhari
Tera Contributor

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 -

(function executeRule(current, previous /*null when async*/ ) {

    // Get the name of the table where the attachment was added (e.g., sc_req_item or sc_request)
    var targetTable = current.table_name + '';

    // Only proceed if the attachment is added to an RITM or REQ
    if (targetTable == 'sc_req_item' || targetTable == 'sc_request') {

        // Retrieve the record the attachment is linked to
        var targetRecord = new GlideRecord(targetTable);
        if (targetRecord.get(current.table_sys_id)) {

            // Get the uploader's display name and the file name
            var uploader = gs.getUserDisplayName();
            var attachmentName = current.file_name;

            // Construct the work note message
            var workNote = 'Attachment Uploaded: ' + attachmentName + ' by ' + uploader + '.\n' +
                'Please check the \'Related Attachments\' list to view it.';

            // Set and save the work note to the target record
            targetRecord.work_notes = workNote;
            targetRecord.update();
        }

    }

})(current, previous);
3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

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.

Ankur Bawiskar
Tera Patron
Tera Patron

@MayurChaudhari 

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.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@MayurChaudhari 

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.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader