I need to send 5 attachments to a user through servicenow so that users can be able to download

TOKALAJ
Tera Contributor

Hi,

 

I had a requirement to send 5 attachments to end user, but that must be attached automatically.

I tried the below ways:

1. By manually opening the HR task and added the attachments and assigned to the end user, end user is able to see them on the HR task and able to download. But my requirement is to add them automatically.

2. I tried by creating a HR template, added the documents to the template and associated the template to the HR task. But the end user is not able to view the attached documents on the HR template in portal.

 

So, I need help for this requirement. If anyone worked on the similar requirement Please let me know, that would be really helpful.

 

Thanks.

1 ACCEPTED SOLUTION

@TOKALAJ Here is the specification for the business rule on sn_hr_core_task table.

Name: Copy Attachments from HR Template to HR Task

Table: sn_hr_core_task

When: After

Insert: True

Update: Flase

Advance: True

 

Script:

(function executeRule(current, previous /*null when async*/) {
    var templateSysId = current.hr_template;
    if (!templateSysId) return;

    // GlideSysAttachment for copying
    var attachment = new GlideSysAttachment();
    
    // Get attachments from HR Template
    var grAttachment = new GlideRecord('sys_attachment');
    grAttachment.addQuery('table_name', 'sn_hr_core_template');
    grAttachment.addQuery('table_sys_id', templateSysId);
    grAttachment.query();

    while (grAttachment.next()) {
        // Copy each attachment from template to current HR task
        attachment.copy('sn_hr_core_template', templateSysId, 'sn_hr_core_task', current.sys_id);
    }
})(current, previous);

Condition: current.hr_template != ''

View solution in original post

3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@TOKALAJ There are two ways you can address this.

1. Create a KB article with those five attachments and put a link of this KB article in your task description and ask the users to download the attachments from the KB article.

2 Create a business rule on the sn_hr_core_task table which checks the short description of the task in the condition field and copies the attachment from the sys_attachment table to the task using GlideSysAttachment.copy() method.

 

Hope this helps.

TOKALAJ
Tera Contributor

Hi @Sandeep Rajput ,

 

Thank you so much for your response it was vey helpful, I am trying with the 2nd option to add a single attachment to the HR task, but I am not able to add, can you please provide me a sample code.

@TOKALAJ Here is the specification for the business rule on sn_hr_core_task table.

Name: Copy Attachments from HR Template to HR Task

Table: sn_hr_core_task

When: After

Insert: True

Update: Flase

Advance: True

 

Script:

(function executeRule(current, previous /*null when async*/) {
    var templateSysId = current.hr_template;
    if (!templateSysId) return;

    // GlideSysAttachment for copying
    var attachment = new GlideSysAttachment();
    
    // Get attachments from HR Template
    var grAttachment = new GlideRecord('sys_attachment');
    grAttachment.addQuery('table_name', 'sn_hr_core_template');
    grAttachment.addQuery('table_sys_id', templateSysId);
    grAttachment.query();

    while (grAttachment.next()) {
        // Copy each attachment from template to current HR task
        attachment.copy('sn_hr_core_template', templateSysId, 'sn_hr_core_task', current.sys_id);
    }
})(current, previous);

Condition: current.hr_template != ''