Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

Update ER Case when attachment added to related corrective action

Not applicable

A pain point for our ER Agents is that there is not an easy way to know if corrective action records associated to an ER case has an attachment added. 

 

An idea I had was to write a business rule on the corrective action table that posts a work note to the associated ER Case when an attachment is added. Has anyone written a BR for this purpose that you'd be willing to share as a template? Or are there better ways to accomplish this? 

 

Thanks! 

2 REPLIES 2

Vivekanandan1
Mega Guru

@Community Alums 

Attachments are stored in sys_attachment, not on the business table itself. Hooking on sys_attachment lets you catch all adds, including those from UI, imports, and APIs.

Design

  • Table: sys_attachment
  • When: after
  • Insert: (enabled)
  • Filter condition: table_name is your corrective action table name (example below uses sn_hr_er_corrective_action)
  • Advanced: (scripted)
  • Run as: A user/role with permission to update ER Cases’ work notes

 

Script (template):

Replace table names and field names to match your environment:

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

    // Only react to new attachments on the Corrective Action table

    if (current.operation() !== 'insert') return;

    if (current.table_name != 'sn_hr_er_corrective_action') return; // <-- change if different

 

    // Look up the Corrective Action record this attachment belongs to

    var caGR = new GlideRecord('sn_hr_er_corrective_action'); // <-- change table if needed

    if (!caGR.get(current.table_sys_id)) return;

 

    // Find the parent ER Case reference on the Corrective Action

    // Adjust the field name if your data model differs

    if (!caGR.er_case) return; // e.g., 'er_case' is a reference to sn_hr_case

 

    var erGR = new GlideRecord('sn_hr_case'); // <-- parent ER Case table

    if (!erGR.get(caGR.er_case)) return;

 

    // Compose a concise work note

    var fileName = current.getValue('file_name') || 'an attachment';

    var caNumber = caGR.getValue('number') || caGR.getUniqueValue();

    var caLink = gs.getProperty('glide.servlet.uri') +

                 'sn_hr_er_corrective_action.do?sys_id=' + caGR.getUniqueValue(); // optional deep link

 

    var note = 'Attachment "' + fileName + '" was added to Corrective Action ' +

               caNumber + '.\n\nLink: ' + caLink;

 

    // Post as a work note on the ER Case

    erGR.setWorkflow(false); // don't re-trigger heavy flows/BRs on purpose

    erGR.work_notes = note;  // ensure journal field is correct in your instance

    erGR.update();

})();

 

Press Helpful, if you found this helpful!

Not applicable

I have tried the following but can't seem to get it working. Does anything jump out at you? 

 

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

    // Only react to new attachments on the Corrective Action table
    if (current.operation() !== 'insert') return;
    if (current.table_name != 'sn_hr_er_corrective_action') return; 

    // Look up the Corrective Action record this attachment belongs to
    var caGR = new GlideRecord('sn_hr_er_corrective_action'); 
    if (!caGR.get(current.table_sys_id)) return;

    // Find the parent ER Case reference on the Corrective Action
    // Adjust the field name if your data model differs

    if (!caGR.er_case) return; 
    var erGR = new GlideRecord('sn_hr_core_case'); // <-- parent ER Case table
    if (!erGR.get(caGR.er_case)) return;

    // Compose a concise work note

    var fileName = current.getValue('file_name') || 'an attachment';

    var caNumber = caGR.getValue('number') || caGR.getUniqueValue();

   // var caLink = gs.getProperty('glide.servlet.uri') +

   //              'sn_hr_er_corrective_action.do?sys_id=' + caGR.getUniqueValue(); // optional deep link

    var note = 'Attachment "' + fileName + '" was added to Corrective Action ' 

//           + caNumber + '.\n\nLink: ' + caLink;
 

    // Post as a work note on the ER Case
    erGR.setWorkflow(false); // don't re-trigger heavy flows/BRs on purpose
    erGR.work_notes = note;
    erGR.update();

})(current, previous);

 

Snag_1467492.png