Sync Attachment between sc_request to sc_task and visa versa

JP6
Tera Contributor

I have a requirement to sync attachment between sc_request and sc_task 

I tried the below script using business rule while  it is working for sc_task to sc_request but not for sc_request to sc_task. Seeking your assistance. 

 

Table: sys_attachment

when to run: After insert

 

(function executeRule(current, previous) {
    // Validate necessary fields
    if (!current.table_name || !current.table_sys_id) {
        gs.error("Missing table_name or table_sys_id.");
        return;
    }

    // Retrieve the task record
    var taskRec = new GlideRecord(current.table_name);
    taskRec.addQuery("sys_id", current.table_sys_id);
    taskRec.query();

    if (taskRec.next()) {
        if (taskRec.request) {
            // Copy attachment from sc_task to sc_request
            GlideSysAttachment.copy(current.table_name, current.table_sys_id, "sc_request", taskRec.request.toString());
            gs.info("Attachment copied from " + current.table_name + " to sc_request.");
        } else {
            gs.error("Request field is empty in sc_task.");
        }
    } else {
        gs.error("No matching record found for sys_id: " + current.table_sys_id);
    }

    // Retrieve the request record correctly
    var requestRec = new GlideRecord("sc_request"); // Correcting the table reference
    requestRec.addQuery("sys_id", taskRec.request); // Query using the request ID
    requestRec.query();

    if (requestRec.next()) {
        // Copy attachment from sc_request back to sc_task
        GlideSysAttachment.copy("sc_request", taskRec.request.toString(), "sc_task", requestRec.sys_id.toString());
        gs.info("Attachment copied from sc_request to sc_task.");
    } else {
        gs.error("No matching sc_request found for request ID: " + taskRec.request);
    }
})(current, previous);

 

1 REPLY 1

Juhi Poddar
Kilo Patron

Hello @JP6 

Maybe this could help: Sync attachment between RITM(sc_req_item) and CatalogTask(sc_task)

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar