Copy attachment from old HR case and transfer it to new HR case and delete attachment from old case

Ashwini Sawant2
Tera Contributor

Hello Community,

have a requirement to copy an attachment from an old HR case and transfer it to a new HR case. Additionally, I would like to delete the attachment from the old case.

 

Could you please assist me with this process?

 

Thank you in advance !

 

@Ankur Bawiskar @Sandeep Rajput 

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Ashwini Sawant2 

when the new HR case is created, if it stores reference somewhere of old HR case then you can do this

1) use after insert BR on HR Case table

2) copy the attachment from old to new

3) then delete the attachments from old HR case

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@Ashwini Sawant2 

I believe transferred_from is a field on HR case and it will store the OLD HR Case reference when the new HR case is created.

You can use that to handle the logic.

Something like this but please enhance

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

    // Add your code here
    new GlideSysAttachment().copy('sn_hr_core_case', current.transferred_from, 'sn_hr_core_case', current.sys_id);

	// delete logic
    var attachment = new GlideSysAttachment();
	var oldHrCaseRef = current.transferred_from.getRefRecord();
	attachment.deleteAll(oldHrCaseRef);


})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello Ankur, 

 

Thank you for your prompt response. 

Below is the Async BR -  Transfer attachments after prediction

 

(function executeRule(current, previous /*null when async*/) {
   
    var sysClass = current.getValue('sys_class_name');
    var sysId = current.getUniqueValue();
    var gr = new GlideRecord('sys_email');
    gr.addQuery('instance', sysId);
    gr.addQuery('target_table', '!=', sysClass);
    gr.query();
    if (gr.next()) {
        new GlideSysAttachment().copy(gr.getTableName(), gr.getUniqueValue(), sysClass, sysId);
        gr.setValue('target_table', sysClass);
        gr.update();
    }
 
-------------------------------------------------------
Before BR - Copy Content from old Case post-Transfer
 
(function executeRule(current, previous /*null when async*/) {
    var grHRCase = new GlideRecord('sn_hr_core_case');
    if (!grHRCase.get(current.transferred_to))
        return;
   
    var openingComment = gs.getMessage('Case was cancelled and transferred to {0}', grHRCase.number);
    if (current.comments != openingComment) {
        if (current.comments)
            grHRCase.comments = gs.getMessage("{0}\n\n(Copied from original case: {1})", [current.comments, current.number]);
        if (current.work_notes)
            grHRCase.work_notes = gs.getMessage("{0}\n\n(Copied from original case: {1})", [current.work_notes, current.number]);
        grHRCase.update();
    }
})(current, previous);

@Ashwini Sawant2 

So what's not working?

what debugging did you do?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader