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
golugolu 

attachment cloning

Hi,

i am cponverting my lead to person account using the below code. I want to clone the attachment in lead to person account as well. How do i do it?
trigger LeadCreateAccount on Lead (Before Insert) {
    Public List<Account> lstAccts = new List<Account>();
    Public List<Account> lstNewAccts = new List<Account>();
    Public set<String> setLeadNames = new set<String>();
    public String name = '';
    Id personAccountRecordTypeId =  Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('PersonAccount').getRecordTypeId();
   
 for(Lead l: trigger.new)
    {
        
        Account acct = new account();
        acct.FirstName = l.firstname;
        acct.LastName = l.lastname;
        acct.Email__c = l.Email;
        acct.RecordTypeId = personAccountRecordTypeId;
        lstNewAccts.add(acct);
        
    }
    
    if (lstNewAccts.size() > 0)
        insert lstNewAccts;

}

 
Raj VakatiRaj Vakati
Use this code and can you please bulkify 

 
trigger LeadCreateAccount on Lead (after  Insert) {
    Public List<Account> lstAccts = new List<Account>();
    Public List<Account> lstNewAccts = new List<Account>();
    Public set<String> setLeadNames = new set<String>();
    public String name = '';
    Id personAccountRecordTypeId =  Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('PersonAccount').getRecordTypeId();
   
 for(Lead l: trigger.new)
    {
        
        Account acct = new account();
        acct.FirstName = l.firstname;
        acct.LastName = l.lastname;
        acct.Email__c = l.Email;
        acct.RecordTypeId = personAccountRecordTypeId;
       // lstNewAccts.add(acct);
        insert acct ; 
			List<Attachment> fileAttachment = [SELECT SELECT Id,
                              Body,
                              Name,
                              Description,
                              ParentId
                              FROM Attachment
                              WHERE ParentId=:l.Id] 
							  
	for( Attachment a :fileAttachment){
			Attachment newFile = a.clone();
			newFile.ParentId = acct.Id;
			insert newFile;
	}
							  
	
    }
    
    
}

 
kumud thakur 20kumud thakur 20
Please use this- I used Email as common key to link lead with Email. Assuming Email would be unique in Lead.
trigger LeadCreateAccount on Lead (after  Insert) {
    Public List<Account> lstAccts = new List<Account>();
    Public List<Account> lstNewAccts = new List<Account>();
    Public set<String> setLeadNames = new set<String>();
    public String name = '';
    Id personAccountRecordTypeId =  Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('PersonAccount').getRecordTypeId();
   List<Account> listAcc=new List<Account>();
   Map<String,Account> mapAccount=new Map<String,Account>();
   Map<Id,String> mapLead=new Map<Id,String>();
   List<Attachment> listAtt=new List<Attachment>();
 for(Lead l: trigger.new)
    {
        
        Account acct = new account();
        acct.FirstName = l.firstname;
        acct.LastName = l.lastname;
        acct.Email__c = l.Email;
        acct.RecordTypeId = personAccountRecordTypeId;
        
        lstNewAccts.add(acct);
        mapLead.put(l.ID,l.Email);
        //insert acct ; 
            /*List<Attachment> fileAttachment = [SELECT SELECT Id,
                              Body,
                              Name,
                              Description,
                              ParentId
                              FROM Attachment
                              WHERE ParentId=:l.Id]*/ 
                              
    /*for( Attachment a :fileAttachment){
            Attachment newFile = a.clone();
            newFile.ParentId = acct.Id;
            //insert newFile;
    }*/
                              
    
    }
  
    if(!lstNewAccts.isEmpty()){

        insert lstNewAccts;
    } 
    for(Account instAcc:lstNewAccts){
        
        mapAccount.put(instAcc.Email__c,instAcc);
    }
    for(Attachment instAcc:[SELECT SELECT Id,
                              Body,
                              Name,
                              Description,
                              ParentId
                              FROM Attachment
                              WHERE ParentId in :mapLead.keyset()] ){
        Attachment newFile = a.clone();
        newFile.ParentId = mapAccount.get(mapLead.get(instAcc.parentID)).Id;
        listAtt.add(newFile);
    }
    
    if(!listAtt.isEmpty()){
        
        insert listAtt;
    }
}

Thanks ! If its work pls mark as best answer.
golugolu
Hi,

I tried with the below code but it dosent work
trigger LeadCreateAccount on Lead (after  Insert) {
    Public List<Account> lstAccts = new List<Account>();
    Public List<Account> lstNewAccts = new List<Account>();
    Public set<String> setLeadNames = new set<String>();
    public String name = '';
    Id personAccountRecordTypeId =  Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agents').getRecordTypeId();
    List<Account> listAcc=new List<Account>();
    Map<String,Account> mapAccount=new Map<String,Account>();
    Map<Id,String> mapLead=new Map<Id,String>();
    List<Attachment> listAtt=new List<Attachment>();
    for(Lead l: trigger.new)
    {
        
        Account acct = new account();
        acct.FirstName = l.firstname;
        acct.LastName = l.lastname;
        acct.Email__c = l.Email;
        acct.RecordTypeId = personAccountRecordTypeId;
        
        lstNewAccts.add(acct);
        mapLead.put(l.Id,l.Email);
    }
    
    if(!lstNewAccts.isEmpty()){
        
        insert lstNewAccts;
    } 
    for(Account instAcc:lstNewAccts){
        
        mapAccount.put(instAcc.Email__c,instAcc);
    }
    for(Attachment a:[ SELECT Id,
                            Body,
                            Name,
                            Description,
                            ParentId
                            FROM Attachment
                            WHERE ParentId in :mapLead.keyset()] ){
                                Attachment newFile = new attachment();
                                newfile.Body = a.Body;
                                newfile.Name = a.Name;
                                newfile.Description = a.Description;
                                newFile.ParentId = mapAccount.get(mapLead.get(a.parentID)).id;
                                listAtt.add(newFile);
                            }
    if(!listAtt.isEmpty()){
        insert listAtt;
    }
}

 
kumud thakur 20kumud thakur 20
Could you please share error message ?