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);
1 REPLY 1

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.