function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ralf WittenbergerRalf Wittenberger 

Move Attachement from Task to Lead/Account Parent Id

Hi,
i am using email to salesforce, here, whenever an customer is sending email to a specific email it will be automatically forwarded to email to salesforce email. Then the email will be added as a Task to either Lead or Contact, whereever the emailaddress from the customer is linked to.

Now, all attachements are only saved to the Task but I would like to attach them to the Parent Object, such as Lead or Account, is that possible by writing a trigger on Attachement?

Thanks in advance
NagaNaga (Salesforce Developers) 
Hi Ralf,

You can not create trigger on attachment from salesforce standard setup

But you can create trigger on attachment from developer console or eclipse. Go to developer console -> File -> New -> Apex trigger. Then select Attachment sObject

For more details please refer following link:

http://www.sfdcpoint.com/salesforce/trigger-on-attachment-in-salesforce/

Please let me know if this helps

Best Regards
Naga kiran
Ralf WittenbergerRalf Wittenberger
Hi Naga,

thanks for your early reply, I already assumed to have a apex trigger, maybe, this wasn´t clear from my question.

The example in your link is interesting but not totally helpful.

Do you know, how to move the attachement from task to lead or account, depending on the parent object with apex trigger?
 
Sagar PareekSagar Pareek
Hi Ralf ,

Try creating a trigger on Attachment Object. Setup>Apex Triggers>Developer Console>File>New>Apex Trigger
Sagar PareekSagar Pareek
Hi Ralf,

Do you want the attacment to be attached to both task and other object like account/lead too?
Ralf WittenbergerRalf Wittenberger
Hi Sagar,

yes, if it would be easy to do, attached to both would be great, if not that easy, it is more important to have it linked to Lead/Account.
Ralf WittenbergerRalf Wittenberger
i found a trigger, which works for Accounts as follwing:

trigger CheckforAttachement on Attachment (before insert) {
    //Check to see if the Attachment has a Task as the Parent Record
   for (Attachment a : trigger.new){
       String parentIdString = String.valueof(a.parentId);
    if (parentIdString.substring(0,3) == '00T'){
        //Select the AccountId from the Task
        task parent = [SELECT AccountId FROM Task WHERE WhatId = :a.parentId];
        //Check to see if the Account exists
        if (parent.AccountID != null){
            //Select the Attachment body (it isn't in memory for an update)
            Attachment body = [SELECT Body FROM Attachment WHERE Id = :a.Id];
            //Create a new Attachment, preserving as much as is possible
            Attachment newA = New Attachment(
                Name = a.Name,
                Body = body.Body,
                OwnerId = a.OwnerId,
                ParentId = parent.AccountId
            );
            //Insert the new Attachment
            insert newA;
        }}
    }
}


when i tried to adopt this to LeadID as following, it doesn´t work, any ideas why?

trigger MoveAttToLead on Attachment (after insert) {
 //Check to see if the Attachment has a Task as the Parent Record
   for (Attachment a : trigger.new){
       String parentIdString = String.valueof(a.parentId);
    if (parentIdString.substring(0,3) == '00T'){
        //Select the LeadId from the Task
        task parent = [SELECT WhoID FROM Task WHERE WhoId = :a.parentId];
        //Check to see if the Account exists
        if (parent.WhoID != null){
            //Select the Attachment body (it isn't in memory for an update)
            Attachment body = [SELECT Body FROM Attachment WHERE Id = :a.Id];
            //Create a new Attachment, preserving as much as is possible
            Attachment newA = New Attachment(
                Name = a.Name,
                Body = body.Body,
                OwnerId = a.OwnerId,
                ParentId = parent.WhoID
            );
            //Insert the new Attachment
            insert newA;
        }}
    }
}
Ralf WittenbergerRalf Wittenberger

Hi @all, now, after lots of different testings etc... i finally made the following trigger, which works totally great. feel free to share.

let me quickly summarize the background of this, I am activating email to salesforce, so if customers sending emails in, they will automatically attached to either relevant Lead or Account. Unfortunately, the attachment is usually saved to the Task but this doesn´t help our complaint and customer service center, as they want the attachments saved to the parent object, such as Lead or Account, so with the following trigger, it´s doing totally, what I expected, as well as deleting the attachment from task to avoid double attachments.

trigger MoveAtt on Attachment (after insert) {
    List<Id> forDeletionIds = new List<Id>();
    for (Attachment a : trigger.new){
    String parentIdString = String.valueof(a.parentId);
    if (parentIdString.substring(0,3) == '00T'){
        System.debug(a.parentId);
        if(Task.WhatId != null){
            task parent1 = [SELECT Id,WhatId  FROM Task WHERE Id = :a.parentId];
        if (parent1.WhatId  != null){
            Attachment body = [SELECT Body FROM Attachment WHERE Id = :a.Id];
            Attachment newA = New Attachment(
                Name = a.Name,
                Body = body.Body,
                Description = 'Email Attachment from ' + date.today(),
                OwnerId = a.OwnerId,
                ParentId = parent1.WhatId
            );
        }
        task parent = [SELECT Id,WhoId  FROM Task WHERE Id = :a.parentId];
        if (parent.WhoId  != null){
            Attachment body = [SELECT Body FROM Attachment WHERE Id = :a.Id];
            Attachment newA = New Attachment(
                Name = a.Name,
                Body = body.Body,
                Description = 'Email Attachment from ' + date.today(),
                OwnerId = a.OwnerId,
                ParentId = parent.WhoId
            );
            insert newA;
            forDeletionIds.add(a.Id);
        }
      }
    }
List<Attachment> forDeletion = [SELECT Id FROM Attachment WHERE Id IN :forDeletionIds];
delete forDeletion;
    }}