Jaspal Singh
Mega Patron
Mega Patron

For those who are frequent users of Email Client Template find_real_file.png have you come across a scenario where you need to send the same attachments from incident/change or any table where you have Email Client template enabled on?

If so, I guess the approach being used would be download the attachments first & then get it attached to the Email Client. It may time consuming as it requires unncessary downlad first & then attach again. To remove the redundant step of download & then attach we can have an option where we can copy the attachments directly to Email Client template from the record.

Consider an example of Incident where you have 5 documents attached & all of these 5 needs to be sent you can have an option of 'Include Attachments' from Notification but you may not want the attachments to sent always & thus is not the option you may opt for.

In that case you just get a Before Insert/Update Business rule on Email (sys_email) table with below snippet & its done.

 

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

	// Add your code here
	var lookforatt=new GlideRecord('sys_attachment');
	lookforatt.addQuery('table_sys_id',current.instance);
	lookforatt.query();
	while(lookforatt.next())
		{
GlideSysAttachment.copy('incident', current.instance, 'sys_email', current.sys_id);
}
})(current, previous);

 

What the above code does is just copies all the attachment from source (incident) in above case to Email Client. Something like below.

For instance a record has 3 attachments.

find_real_file.png

When Email Client Template is opened all attachments are copied.

find_real_file.png

 

GlideSysAttachment.copy('incident', current.instance, 'sys_email', current.sys_id);

where 'incident' is source table & can be replaced with required table for copying the attachment from record to Email Client template.

 

Thanks,

Jaspal Singh

Comments
Milind Apte
Mega Explorer
This is definitely going to help especially support team and this will help to reduce TAT.
Roshani
Tera Expert

Hi Jaspal,

How you get view option for attachment on email client. Could you plaese let me know.

Andrew Parkes
Tera Expert

Hi Jaspal, i've followed your steps (very helpful article!) but the attachments are duplicating, sometimes tripling  in the client, have you experienced this?

Jaspal Singh
Mega Patron
Mega Patron

If you closely see it simply copies attachment from record where the mail client is used. Can you check if there are some additional business rule running on the Email (sys_email) table.

Harsh Vardhan
Giga Patron

try with below script. 

 

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

	// Add your code here

	GlideSysAttachment.copy(current.target_table,  current.instance ,'sys_email', current.sys_id);


})(current, previous);
Rashid Yakhyaev
Mega Expert

@Andrew Parkes Did you figure it out? I'm having the same problem and nothing helps. We're on Paris patch 3 version

Harsh Vardhan
Giga Patron

Can you paste the script which you have used ? if you see my above response and the script which I have mentioned, that will absolutely not going to create any duplicate attachment. 

Note: Working perfectly on Paris patch 3 as well .

Rashid Yakhyaev
Mega Expert

Hi @Harshvardhan , our script is slighly different and I found the issue. The issue was not in the script but because of another BR on the sys_email table.

Thank you for taking the time to answer though, it's appreciated!

//Rashid

Trisha Agarwal
Kilo Contributor

Hi all,

We had this solution also running in our instance but it gave us problems with the OOTB functionality on notifications "Include attachments". It overrides the functionality.

I have changed the condition to query and it works great now.

 

Santosh70
Tera Expert

@Jaspal Singh  , Thanks for the Article.

 

could you please tell what does current.instance will do? to get the sys id of the incident record whether current.getTabelName or current.sys_id will not work?

 

Thanks again.

Snehaja_Bhore1
Kilo Contributor

@Trisha Agarwal  Hi Trisha, we are trying to include attachments while we click on forward from workspace emails under specific case. As you have mentioned this is working at your instance with Include Attachment, would you be able to provide us how you have done this?

Thanks,

Snehaja 

Kartik5
Kilo Contributor

@Jaspal Singh I am using BR - After - Insert with below code:

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

// Add your code here
var lookforatt=new GlideRecord('sys_attachment');
lookforatt.addQuery('table_sys_id',current.instance);
lookforatt.query();
while(lookforatt.next())
{
GlideSysAttachment.copy('sn_customerservice_case', current.instance, 'sys_email', current.sys_id);
}
})(current, previous);

 

Issue1: Every attachment added are duplicating on Email Client

Issue2: Emails sent are also having duplicate emails. 

 

Igor __tek
Tera Contributor

Hi people,

to resolve issue with duplicite entries, you have to set limit 1 to the query, it is because function:

GlideSysAttachment.copy('sn_customerservice_case', current.instance, 'sys_email', current.sys_id);
}

do always copy all attachments from a record so if you run it as many times as many attachments you have in the record you are creating duplicates.

Just change a function something like this:

find_real_file.png

I hope this will be helpful.

Igor __tek
Tera Contributor

Hi people,

to resolve issue with duplicite entries, you have to set limit 1 to the query, it is because function:

GlideSysAttachment.copy('sn_customerservice_case', current.instance, 'sys_email', current.sys_id);
}

do always copy all attachments from a record so if you run it as many times as many attachments you have in the record you are creating duplicates.

Just change a function something like this:

find_real_file.png

I hope this will be helpful.

Robert Wijnbelt
ServiceNow Employee
ServiceNow Employee

This was helpful, and I think you also need to make the Business Rule "After Insert" only, not after Update.

To address Issue #2, I did the following:

The SetLimit(1) function prevents duplicates to show up in the Email Client popup, and the Business Rule "After Insert" only prevents it from firing again after you hit "Send", which updates sys_email and changes the email Type field to "send-ready". This would trigger the BR again.
I guess you can also prevent this from happening by setting a condition on the BR to check for this value.

Alex Vastartis
Tera Contributor

All,

I've submitted an Idea on the Idea Portal to have this added as a feature, please review and consider lending your support.

Idea - Include Attachments on Email Forward

Ash41
Kilo Sage

@Jaspal Singh Great Article!

I have similar requirement, I want to copy CC  users and email history to the email client. Is that possible? Please suggest

Jaspal Singh
Mega Patron
Mega Patron

By CC users you mean watchlist users? Did you try configuring something if so share for a check.

SWills
Tera Expert

Fair warning, don't make this a "While" loop, replace with IF.
Otherwise it will exponentially increate the number of emails, as the "GlideAttachment.copy" function already handles ALL record attachments. no need to iterate through all related attachments.

I also strongly recommend having strict conditions on this BR to only run for outbound emails composed from the email client.

@Jaspal Singh please edit the post if possible. So people don't use the original posted script

Joe Jeffcoat
Tera Explorer

I know it's been a while since this had any replies, but I'm curious as to how you can modify this to pull from the Email Template itself.  So if we put an attachment on the Quick Message template, how do you pull that attachment into the Email client automatically?

 

It's really silly that this hasn't been addressed OOTB already.  If your template includes attachments, you would think it would automatically pull them in.

B Rollogas
Tera Explorer

@Jaspal Singh I realize this post is from awhile ago, but my client has a need for this to be implemented, but our hang up is that one of our assignment groups does not want this functionality for their team when they are using the email client.

 

Is there a way to make this feature not 'global' or to make it so this specific assignment group is excluded by using category? We've tried altering the script, but it doesn't seem to be working. Any help would be amazing! I have provided our script below:

 

(function executeRule(current, previous /*null when async*/ ) {
 
        // Check if the current record is an Email record
        if (current instanceof GlideRecord && current.getTableName() === 'sys_email_draft') {
            // Get the related Incident record
            var incident = new GlideRecord('incident');
            incident.addQuery('sys_id', current.instance);
            incident.query(); {
                // Get the category field from the Incident record
                var category = incident.getValue('category');
 
                // Define the value that indicates the incident is servicing.
                var servicingValue = 'Servicing';
 
                // Check if the category of the Incident is not servicing
                if (category != servicingValue) {
 
                    // Add your code here
                    var lookforatt = new GlideRecord('sys_attachment');
                    lookforatt.addQuery('table_sys_id', current.instance);
                    lookforatt.query();
                    if (lookforatt.next()) {
                        GlideSysAttachment.copy('incident', current.instance, 'sys_email', current.sys_id);
                    }
                }
            }
}
}
        )(current, previous);
Kiruthika2
Tera Explorer

Hi,

 

The above BR attaches helps. But the problem is when attachments are added not only to Forward function, but also while using Reply and Reply All. In Reply and Reply All the attachment should not be added. Please help

Greg D
Tera Guru

If you'd like to only copy attachments when using the Email Client and not the automatic email notifications then you can add a condition to filter where 'header' contains 'X-ServiceNow-Source: EmailClient'. Otherwise attachments will be copied to all outgoing email from the ticket. Not sure about previous releases but we're on Washington. 

pgandikota
Tera Explorer

@Harsha Vardhan your script is working as suggested in the classic interface, but in the CSM/FSM configurable workspace we have the option to select from the record or computer and attach the required attachments. So when this business rule stays active. In the workspace, while sending emails to the end user, it sends all the attachments irrespective of whether any attachments are selected or not. Please help me to resolve this issue.

Version history
Last update:
‎05-04-2020 08:35 AM
Updated by: