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:

global class GlobalHelper {
 global static void deleteImageAttachments(){
 // removed the "image" files from Attachments. These are almost always images from email signature lines
 // no magic to the LIMITs on the SQOL, just preventing the possibility of governor limits
 
 list<Attachment> aDel = new list<Attachment>();
 for (Attachment a : [SELECT Id, ParentId FROM Attachment WHERE Name LIKE 'image%' LIMIT 10000]){
 if ( ((string)a.ParentId).substring(0,3) <> '02s') aDel.add(a);
 }
 if (!aDel.isEmpty()) delete aDel;
 }
}

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:

public class schedulableDeleteImageAttachments implements Schedulable{
 public void execute(SchedulableContext Sche){
 GlobalHelper.deleteImageAttachments();
 }
}

You’ll need test classes for these so lets create those now. Create a class called “GlobalHelperTest”. Its code will look like:

@isTest
private class GlobalHelperTest {
 public static testMethod void testDeleteImageAttachments(){
 Account a = new Account();
 insert a;
 
 Attachment at = new Attachment(name='image01', ParentId=a.Id, body=Blob.valueof('myBlob'));
 insert at;
 
 GlobalHelper.deleteImageAttachments();
 }
}

Create another test class for the schedulable class called “schedulableDeleteImageAttachmentsTest” with the following code:

@isTest
public class schedulableDeleteImageAttachmentsTest {
static testMethod void myUnitTest() {
for (CronTrigger c : [SELECT Id, CronJobDetailId, CronJobDetail.JobType, CronJobDetail.Name, State
FROM CronTrigger WHERE CronJobDetail.JobType = '7' AND CronJobDetail.Name = 'testDeleteImage']){
system.abortJob(c.Id);
}

test.starttest();
schedulableDeleteImageAttachments dia = new schedulableDeleteImageAttachments();
String sch8 = '0 00 3 * * ?';
system.schedule('testDeleteImage', sch8, dia);
test.stopTest();

for (CronTrigger c : [SELECT Id, CronJobDetailId, CronJobDetail.JobType, CronJobDetail.Name, State
FROM CronTrigger WHERE CronJobDetail.JobType = '7' AND CronJobDetail.Name = 'testDeleteImage']){
system.abortJob(c.Id);
}
}
}

That should take care of the code requirements.  So now we need to setup the schedule within Salesforce.

  • Using Classic, navigate to Setup and search setup for “Apex Classes”.
  • In the body of the page, click the button called “Schedule Apex”
  • Give the job a meaningful name like “Delete Email Images”
  • Select the Apex class called “schedulableDeleteImageAttachments”
  • Choose your preferred schedule
    • Note: you’ll want to manually type in the End time so you can enter a year that is out into the future.
  • Save the scheduled job.

 

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.

Terry Miller
Terry has spent over 20 years focused on business leadership and information technology. As an independent consultant, he enjoys working with a variety of customers to help them solve business problems using the Salesforce® platform. His ability to quickly identify bottlenecks and provide understandable solutions has gained him the trust of his customers. If you're looking for expert guidance on your next Salesforce® project, click here to contact Terry today.

5 Comments

  1. 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);’?

    1. 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.

  2. 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?

Leave a Reply

Your email address will not be published.

*