Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
Hi ,

I have written the below trigger code. It is working fine but is not at all optimised. It is having three for loops. Can anyone please guide me with a code snippet.

trigger AttachmentTrigger on Attachment (after insert) {

List<Account> accountList = new List<Account>();

List<Lead> leadlist = new List<Lead>();

List<String> emails = new List<String>();

Set<Id> accIds = new Set<Id>();

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

for(Attachment att : trigger.New){

if(att.ParentId.getSobjectType() == Lead.SobjectType){

accIds.add(att.ParentId);

}

leadList = [Select id , email from lead where id in : accIds];

for(Lead l : leadlist){

emails.add(l.email);

}

accountList = [select id, Email__c from Account where email__c in : emails];

if(accountList!=null && accountList.size()>0){

for(Account acc : accountList){

for(Lead ld : leadlist){

if(ld.email == acc.email__c){

Attachment newFile = new attachment();

newfile.Body = att.Body;

newfile.Name = att.Name;

newfile.Description = att.Description;

newfile.ParentId = acc.id;

atts.add(newFile);

}

}

}

}

}

if(!atts.isEmpty()){

insert atts;

}

}

 
4 answers
  1. Apr 16, 2019, 7:00 AM
    Hi  Golu,

    As best practice says that we can not use a query in a loop so please take care of that.

    trigger AttachmentTrigger  on Attachment(before insert) {

        List<Account> accountList = new List<Account>();

        List<Lead> leadlist = new List<Lead>();

        List<String> emails = new List<String>();

        Set<Id> accIds = new Set<Id>();

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

        Map<Id ,Attachment>attMap = new Map<Id, Attachment>();

        for(Attachment att : trigger.New) {

            if (att.ParentId.getSobjectType() == Lead.SobjectType) {

                accIds.add(att.ParentId);

            }

                attMap.put(att.Id,att);

        }

        leadList = [Select id , email from lead where id in : accIds];

        for(Lead l : leadlist){

            emails.add(l.email);

        }

        accountList = [select id, Email__c from Account where email__c in : emails];

        if(accountList!=null && accountList.size()>0){

            for(Account acc : accountList){

                for(Lead ld : leadlist){

                    if(ld.email == acc.email__c){

                        Attachment newFile = new attachment();

                        newfile.Body =attMap.get(ld.Id).Body;

                        newfile.Name = attMap.get(ld.Id).Name;

                        newfile.Description = attMap.get(ld.Id).Description;

                        newfile.ParentId = acc.id;

                        atts.add(newFile);

                    }

                }

            }

        }

        if(!atts.isEmpty()){

            insert atts;

        }

    }

    I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

    Thanks and Regards,

    Deepali Kulshrestha

     
Loading
0/9000