Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Need to update attachment field after raising the request

bharathvall
Mega Contributor

As a requester,

 

I want the attachment added field in the RITM (sc_req_item) record to automatically display the number of attachments uploaded when submitting a catalog item request with a mandatory attachment,

So that the request record clearly reflects how many documents were attached at the time of submission.

please find the below screenshots,

bharathvall_0-1758005617388.png

 

 

bharathvall_1-1758005641866.png

Below I write the business rule:

var WhatOperation = current.operation();
    var ForWhom = current.table_sys_id;
    var ParentREQ = '';
    var ChildWho = '';

    // For insert action
    if (WhatOperation == 'insert') {

        var GRITMAdd = new GlideRecord('sc_req_item');
        GRITMAdd.addQuery('sys_id', '=', ForWhom);
        GRITMAdd.query();
        if (GRITMAdd.next()) {
            var UpdateAdder = GRITMAdd.u_attachment_added;
            UpdateAdder++;
            GRITMAdd.u_attachment_added = UpdateAdder;
            ParentREQ = GRITMAdd.request;
            ChildWho = GRITMAdd.number;

            GRITMAdd.update();

        }
        // Updating REQ also - Why? FlowDesigner challenge for sc_req_item table
        var GREQAdd = new GlideRecord('sc_request');
        GREQAdd.addQuery('sys_id', '=', ParentREQ);
        GREQAdd.query();
        if (GREQAdd.next()) {
            var UpdateAdderREQ = GREQAdd.u_attachment_added;
            UpdateAdderREQ++;
            GREQAdd.u_attachment_added = UpdateAdderREQ;
            GREQAdd.u_updated_ritm = ChildWho;

            GREQAdd.update();

        }

    }
    // For delete action
    else if (WhatOperation == 'delete') {

        var GRITMDel = new GlideRecord('sc_req_item');
        GRITMDel.addQuery('sys_id', '=', ForWhom);
        GRITMDel.query();
        if (GRITMDel.next()) {
            var DeleteAdder = GRITMDel.u_attachment_deleted;
            DeleteAdder++;
            GRITMDel.u_attachment_deleted = DeleteAdder;
            ParentREQ = GRITMDel.request;
            ChildWho = GRITMDel.number;

            GRITMDel.update();
        }


        // Updating REQ also - Why? FlowDesigner challenge for sc_req_item table
        var GREQDel = new GlideRecord('sc_request');
        GREQDel.addQuery('sys_id', '=', ParentREQ);
        GREQDel.query();
        if (GREQDel.next()) {

            var UpdateDelREQ = GREQDel.u_attachment_deleted;
            UpdateDelREQ++;
            GREQDel.u_attachment_deleted = UpdateDelREQ;
            GREQDel.u_updated_ritm = ChildWho;
            GREQDel.update();

'        }
 
it is not working when user requested raise RITM, it is not updating in "Attchment_added" field,
the root cause is:
when an attachment is added to the catalog item, the attachment table sys_id is different from the one creating after submitting the request, That's why it is not being captured the first time
 
Before submiting:
 
bharathvall_2-1758006107012.png

IN Attchment table:

bharathvall_3-1758006182991.png

 

After submitting:

bharathvall_4-1758006282951.png

After submiting RITM sys_id noth same.

 

can you help me for this issue.

2 REPLIES 2

G Ponsekar
Tera Guru

Hi @bharathvall ,

 

can you try like below:

Table: Attachment (sys_attachment)

    • Active: true
    • When to run:
      • When: after
      • Insert: true
      • Delete: true
    • Filter Condition: Table name is sc_req_item
  1. In the Advanced section, use the following script. 
 
javascript
(function executeRule(current, previous /*null when async*/ ) {
    // Check if the current attachment belongs to a Requested Item (RITM)
    if (current.table_name == 'sc_req_item') {
        var ritmGr = new GlideRecord('sc_req_item');
        if (ritmGr.get(current.table_sys_id)) {
            var agg = new GlideAggregate('sys_attachment');
            agg.addAggregate('COUNT');
            agg.addQuery('table_sys_id', current.table_sys_id);
            agg.query();

            var count = 0;
            if (agg.next()) {
                count = agg.getAggregate('COUNT');
            }

            // Update the custom field on the RITM            
ritmGr.u_attachment_count = count; ritmGr.setWorkflow(
false); // Prevents cascading business rules
ritmGr.update(); } } })(current, previous);

 

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks, GP

svirkar420
Tera Guru

Hi @bharathvall , Here you can try this business rule this should do the trick. Try this and let me know if it works.

(function executeRule(current, previous /*null when async*/) {
if (current.table_name == 'sc_req_item') {
var ritmGR = new GlideRecord('sc_req_item');
if (ritmGR.get(current.table_sys_id)) {
var attGR = new GlideAggregate('sys_attachment');
attGR.addAggregate('COUNT');
attGR.addQuery('table_name', 'sc_req_item');
attGR.addQuery('table_sys_id', current.table_sys_id);
attGR.query();
if (attGR.next()) {
ritmGR.u_attachment_count = attGR.getAggregate('COUNT');
ritmGR.update();
}
}
}
})(current, previous);

If this solution helped you Please Mark this solution as accepted and helpful as it will be helpful for other users as well.
Best Regards.
Saurabh V.