Need to update attachment field after raising the request
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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,
Below I write the business rule:
IN Attchment table:
After submitting:
After submiting RITM sys_id noth same.
can you help me for this issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
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
- In the Advanced section, use the following script.
(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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.