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 

Trigger to move Attachment from Task to Lead

Hi, I would like to post this question again, maybe somebody has an idea, what am i missing or where i failed.

I have a trigger, which can duplicate a attachment, which is linked to an Task to an account with the following trigger


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 Leads, it doesn´t work and I have no clue, is there someone, that can maybe help me out here?

When I tried to replace AccountId with LeadId, console tells me "No such column 'LeadID' on entity 'Task'
Therefore, I have added a formular field "LeadID__c" to the Task, which pulls the LeadId from the field WhoId, which is the Name Field (Lookup) for Leads in Tasks.

This is the trigger, i adopted and it doesn´t work

trigger MoveAttToLead on Attachment (after insert, after update) {
    for (Attachment a : trigger.new){
    String parentIdString = String.valueof(a.parentId);
    if (parentIdString.substring(0,3) == '00T'){
        task parent = [SELECT LeadID__c FROM Task WHERE WhoId = :a.parentId];
        if (parent.LeadID__c != null){
            Attachment body = [SELECT Body FROM Attachment WHERE Id = :a.Id];
            Attachment newA = New Attachment(
                Name = a.Name,
                Body = body.Body,
                OwnerId = a.OwnerId,
                ParentId = parent.LeadID__c
            );
            insert newA;
        }}}}
Best Answer chosen by Ralf Wittenberger
ManojjenaManojjena
Hi Ralf,

Try with below code it wil work .
 
trigger Test on Attachment (after insert) {
    for (Attachment a : trigger.new){
    String parentIdString = String.valueof(a.parentId);
    if (parentIdString.substring(0,3) == '00T'){
        System.debug(a.parentId);
        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,
                OwnerId = a.OwnerId,
                ParentId = parent.WhoId
            );
            insert newA;
        }
      }
    }
}

 

All Answers

ManojjenaManojjena
Hi Ralf ,

Replace Lead Id __c with WhatId it will work .
Ralf WittenbergerRalf Wittenberger
Hi Manoj,

tried it, but unfortunately, it didn´t work. WhatId is only lookup field for Account, Opportunities etc but not or Lead. For Lead, the lookupfield is called WhoId.
Failure Message is after update: List has no rows for assignment to SObject: Trigger.MoveAttToLead: line 5, column 1
Ralf WittenbergerRalf Wittenberger
Hi again, thx for your early reply.

I already tried it this way, unfortunately, it didn´t work either.

There must be some other issue to the lookup or?
SaranSaran
Hi Ralf,

You have to write trigger in the task object.
Instead of writing in attachment object.

Thanks,
 
ManojjenaManojjena
Hi Ralf,

Try with below code it wil work .
 
trigger Test on Attachment (after insert) {
    for (Attachment a : trigger.new){
    String parentIdString = String.valueof(a.parentId);
    if (parentIdString.substring(0,3) == '00T'){
        System.debug(a.parentId);
        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,
                OwnerId = a.OwnerId,
                ParentId = parent.WhoId
            );
            insert newA;
        }
      }
    }
}

 
This was selected as the best answer
Ralf WittenbergerRalf Wittenberger
Hi Manoj,

this works great, but unfortunately only on updating an attachment, not for new attached. What could be missed here?

Thanks a lot!

Ralf
SaranSaran
Hi Ralf,

Use the below trigger. It will work when the task is created itself
 
trigger updateOppty on Task (after insert) {
  
 list<Attachment> attList = new list<Attachment>();
  Map<Id, Id> taskIds = new Map<Id, Id>();
  
  for(Task t : Trigger.new)
  {
      if(string.valueOf(t.WhoId).substring(0,3).EqualsIgnoreCase('00Q'))
         taskIds.put(t.Id, t.whoid);
  }
  
  system.debug('######' + taskIds);

  for(Attachment acc : [select id, body,parentid from attachment where parentid in:taskIds.keyset()])
  {
      system.debug('$$$ Should have looped');
      Attachment a = new Attachment();
      a.Name = 'test';
      a.Body = acc.body;
      a.ParentId = taskIds.get(acc.parentId);
      attList.add(a);
  }
  system.debug('######' + attList);
  insert attList;
}

Thanks.
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;
    }}
jeffrey martin 8jeffrey martin 8
Would you maybe be averse to helping me with a very similar trigger?  Move Case Attachment to Account Attachment
Ralf WittenbergerRalf Wittenberger
Hi Jeffrey, of course, if i can.