

| Applies to: Lightning and Classic |
Salesforce for Outlook and now Lightning for Outlook are great tools for adding emails into Salesforce. However, many of our emails have signature lines that include logos or other images that clog up storage and add no value. Below is how I eliminate these unnecessary images.
There’s a lot of code snippets to making this happen but you should be able to cut and paste it. I’ll walk you through the process.
First, create a new class and name it “GlobalHelper” (or choose your own name and use it throughout the code). This is the main piece of code that finds and deletes the images. Paste in the following:
<br />global class GlobalHelper {<br />global static void deleteImageAttachments(){<br />// removed the "image" files from Attachments. These are almost always images from email signature lines<br />// no magic to the LIMITs on the SQOL, just preventing the possibility of governor limits</p>
<p>list&lt;Attachment&gt; aDel = new list&lt;Attachment&gt;();<br />for (Attachment a : [SELECT Id, ParentId FROM Attachment WHERE Name LIKE 'image%' LIMIT 10000]){<br />if ( ((string)a.ParentId).substring(0,3) &lt;&gt; '02s') aDel.add(a);<br />}<br />if (!aDel.isEmpty()) delete aDel;<br />}<br />}<br />Next, we want to schedule this code to run nightly so we need to create a schedulable class. So lets create another new class and call it “schedulableDeleteImageAttachments”. Its code will look like this:
<br />public class schedulableDeleteImageAttachments implements Schedulable{<br />public void execute(SchedulableContext Sche){<br />GlobalHelper.deleteImageAttachments();<br />}<br />}<br />You’ll need test classes for these so lets create those now. Create a class called “GlobalHelperTest”. Its code will look like:
<br />@isTest<br />private class GlobalHelperTest {<br />public static testMethod void testDeleteImageAttachments(){<br />Account a = new Account();<br />insert a;</p>
<p>Attachment at = new Attachment(name='image01', ParentId=a.Id, body=Blob.valueof('myBlob'));<br />insert at;</p>
<p>GlobalHelper.deleteImageAttachments();<br />}<br />}<br />Create another test class for the schedulable class called “schedulableDeleteImageAttachmentsTest” with the following code:
@isTest<br />public class schedulableDeleteImageAttachmentsTest {<br />static testMethod void myUnitTest() {<br />for (CronTrigger c : [SELECT Id, CronJobDetailId, CronJobDetail.JobType, CronJobDetail.Name, State<br />FROM CronTrigger WHERE CronJobDetail.JobType = '7' AND CronJobDetail.Name = 'testDeleteImage']){<br />system.abortJob(c.Id);<br />}</p>
<p>test.starttest();<br />schedulableDeleteImageAttachments dia = new schedulableDeleteImageAttachments();<br />String sch8 = '0 00 3 * * ?';<br />system.schedule('testDeleteImage', sch8, dia);<br />test.stopTest();</p>
<p>for (CronTrigger c : [SELECT Id, CronJobDetailId, CronJobDetail.JobType, CronJobDetail.Name, State<br />FROM CronTrigger WHERE CronJobDetail.JobType = '7' AND CronJobDetail.Name = 'testDeleteImage']){<br />system.abortJob(c.Id);<br />}<br />}<br />}That should take care of the code requirements. So now we need to setup the schedule within Salesforce.
That should do it for you. The email images will now be automatically deleted when your scheduled job runs.
Hope this is helpful for you! If so, leave me a comment and let me know.
5 Comments
Hi Terry, thank you for this great post. It helps me a lot. For one thing, if the ParentId starts with ‘0s2’, then it is for an email message. So, shall it be ‘if ( ((string)a.ParentId).substring(0,3) == ’02s’) aDel.add(a);’?
Thank Rafy. Are you asking why am I looking only at ParentId = “0s2”? I do that to prevent images that might be added to non-email objects from being deleted. For example, if an image is updated to an account object, I don’t want to inadvertently delete that.
Yes. You want to delete the images only coming from emails. So, I think
if ( ((string)a.ParentId).substring(0,3) ’02s’) aDel.add(a);
should be
if ( ((string)a.ParentId).substring(0,3) == ’02s’) aDel.add(a);
am I right?
The ParentId for the Attachment is a Task so that would be “00T”. If you want to link the deletes specifically, then I’d use “== ’00T'”. “02s” is an HTML Component which you do not want to delete. See: Record ID Prefixes
Hi, here from bing, i enjoyng this, i will come back soon.