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
niharnihar 

when ever New opportunity created previous opportunity file should be added to new opportunity by using triggers? if The accounts are belongs to same account?

Hi Everone,
I have created opportunity1 record with Account1
and i want to create opportunity2 record with same Account1
automatically the file in opportunity1  should be dumped or add to opportunity2 record
can any one suggest me Trigger..............

Thanks in advance...................... 
Ajay K DubediAjay K Dubedi
Hi nihar,
Here is a solution to your Problem.

                ----------Trigger--------
                
trigger InsertFiletrigger on Opportunity (after insert) {
    InsertFile.fileInsertion(trigger.new); 
}

                ----------Trigger_Handler-------
                
public with sharing class InsertFile {
    public static void fileInsertion(List<Opportunity> lstOpp){
        Set<Id> stAccId = new Set<Id>();
        for(Opportunity opp: lstOpp){
            stAccId.add(opp.AccountId);
        }
        System.debug('List of AccountId>>>>>>'+ stAccId);
        List<Opportunity> lstAllOpp = new List<Opportunity>();
        lstAllOpp = [SELECT Id,Name,AccountId FROM Opportunity Where AccountId IN:stAccId];
        System.debug('ListAllOpp >>>>>>'+ lstAllOpp);
        
        List<Attachment> lstAttachment = new List<Attachment>();
        lstAttachment = [SELECT Id,ParentId,Name,Body FROM Attachment Where ParentId IN: lstAllOpp];
        System.debug('ListAttach >>>>>>'+ lstAttachment);
        
        List<Attachment> lstToAddAttch = new List<Attachment>();
        for(Opportunity opp: lstOpp){
            for(Attachment attach : lstAttachment) {
                Attachment objattach =new Attachment();
                objattach.body = attach.body;
                objattach.name = attach.name;
                objattach.ParentId = opp.Id;
                lstToAddAttch.add(objattach);
            }
        }
        System.debug('ListToAttach >>>>>>'+ lstToAddAttch);
        if(lstToAddAttch.size()>0)
            insert lstToAddAttch;
    }
}


Please mark this as Best Answer if you find this solution helpful.

Thank You
Ajay Dubedi​