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
Swaraj Behera 7Swaraj Behera 7 

Attachments from lead to opportunity trigger

Hi,
While converting lead to opportunity,i want to attach all the attachments from lead to opportunity.
I have written a trigger for this but its nor firing.Can you please help me on this.
 
trigger LeadConvert on Lead (after update) {

List<Attachment> lstattach=new List<Attachment>();



if (Trigger.old[0].isConverted == false && Trigger.new[0].isConverted == true) {


	System.debug('oppid' + Trigger.new[0].ConvertedOpportunityId);
	Attachment aa=[Select Id,ParentId from Attachment where ParentId=:Trigger.new[0].ConvertedaccountId];
	System.debug('attachment in lead' + aa );

if (Trigger.new[0].ConvertedOpportunityId != null) {

for(Attachment a:[Select Id,ParentId from Attachment where ParentId=:Trigger.new[0].ConvertedaccountId]){


  a.ParentId=Trigger.new[0].ConvertedOpportunityId;
  System.debug('parentid' +a.ParentId);

  lstattach.add(a);
}

update lstattach;
}

}


}

 
Best Answer chosen by Swaraj Behera 7
Swaraj Behera 7Swaraj Behera 7
Hi All,
Thank you for all the replies.
I got the solution.Parent Id is not rewritable so my code was not working.Now i have created a new attachment and inserting it.
Below is the solved code.
 
trigger LeadConvert on Lead (after update) {

if (Trigger.old[0].isConverted == false && Trigger.new[0].isConverted == true) {

// if a new opportunity was created
    System.debug('oppid' + Trigger.new[0].ConvertedOpportunityId);
    

if (Trigger.new[0].ConvertedOpportunityId != null) {

for(Attachment a:[Select Id,ParentId,body,name,ContentType from Attachment where ParentId=:Trigger.new[0].ConvertedaccountId]){

    Attachment attach = new Attachment();
    attach.Body = a.body;
    attach.Name = a.name;
    attach.ParentId = Trigger.new[0].ConvertedOpportunityId;
    attach.IsPrivate = false;
    attach.ContentType=a.ContentType;
    insert attach;
    delete a;  
}
}
}
}

Thanks,
Swaraj

All Answers

Akash_CRMAkash_CRM
Hi Swaraj,

Use the Before Update context of Trigger, it should work.

Regards,
Akash.
Navee RahulNavee Rahul
Hello Swaraj,

i think we cannot update the attachment parentId and so we need to add the attachment from
Lead to Oppourtunity.

Then delete the Attachment from Lead.

Thanks
D Naveen Rahul.
VineetKumarVineetKumar
Your trigger is wrong, what I can understand is that you want to assign the attachments associated to lead, to the converted opportunity.
Refer the below code:
trigger LeadConvert on Lead (after update) {
	List<Attachment> lstattach=new List<Attachment>();
	if (Trigger.new[0].isConverted) {
		if (Trigger.new[0].ConvertedOpportunityId != null) {
			for(Attachment a : [Select Id,ParentId from Attachment where ParentId=:Trigger.new[0].Id]){
				a.ParentId=Trigger.new[0].ConvertedOpportunityId;
				System.debug('parentid' +a.ParentId);
				lstattach.add(a);
			}
			update lstattach;
		}
	}
}
Vasani ParthVasani Parth
trigger LeadConvert on Lead (after update) {

List<Attachment> lstattach=new List<Attachment>();

 // no bulk processing; will only run from the UI
 if (Trigger.new.size() == 1) {

   if (Trigger.old[0].isConverted == false && Trigger.new[0].isConverted == true) {

   // if a new opportunity was created
      if (Trigger.new[0].ConvertedOpportunityId != null) {

   for(Attachment a:[Select Id,ParentId from Attachment where ParentId=:Trigger.new[0].Id]){
      a.ParentId=Trigger.new[0].ConvertedOpportunityId;
      lstattach.add(a);
  }

   update lstattach;
 }
  }
 }

This sample code is not tested but will give you an idea to proceed.
Swaraj Behera 7Swaraj Behera 7
Hi All,
Thank you for all the replies.
I got the solution.Parent Id is not rewritable so my code was not working.Now i have created a new attachment and inserting it.
Below is the solved code.
 
trigger LeadConvert on Lead (after update) {

if (Trigger.old[0].isConverted == false && Trigger.new[0].isConverted == true) {

// if a new opportunity was created
    System.debug('oppid' + Trigger.new[0].ConvertedOpportunityId);
    

if (Trigger.new[0].ConvertedOpportunityId != null) {

for(Attachment a:[Select Id,ParentId,body,name,ContentType from Attachment where ParentId=:Trigger.new[0].ConvertedaccountId]){

    Attachment attach = new Attachment();
    attach.Body = a.body;
    attach.Name = a.name;
    attach.ParentId = Trigger.new[0].ConvertedOpportunityId;
    attach.IsPrivate = false;
    attach.ContentType=a.ContentType;
    insert attach;
    delete a;  
}
}
}
}

Thanks,
Swaraj
This was selected as the best answer