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
Subhodeep Dey 1Subhodeep Dey 1 

Bulkification issue


trigger CloneInternshipOpportunityTrigger on alu_Internship_Cycle__c  after insert, before update, after update, before delete, after delete, after undelete){
    if(Trigger.isInsert && Trigger.isAfter){

Helper class:-
public class CloneInternshipOpportunity {
    public static void createInternshipOpportunity(List<alu_Internship_Cycle__c> internCycle) {
            Map<Id,alu_Internship_Cycle__c> mapIdbyNewInternCyc = new  Map<Id,alu_Internship_Cycle__c>();
            //Fetch Existing Internship cycle      
            for(alu_Internship_Cycle__c newInternCyc : internCycle){   
               mapIdbyNewInternCyc.put(newInternCyc.Id, newInternCyc);
            Map<Id,alu_Internship_Cycle__c> mapIdbyOldInternCyc = new Map<Id,alu_Internship_Cycle__c> ([
                SELECT Id, Name, Start_Date__c 
                FROM alu_Internship_Cycle__c 
                WHERE Id NOT IN: mapIdbyNewInternCyc.keySet() 
                AND Start_Date__c =: mapIdbyNewInternCyc.values().Start_Date__c.addYears(-1) ]);
            System.debug('mapIdbyOldInternCyc '+mapIdbyOldInternCyc);
            List<Opportunity> oppListByOldCyc = [
                SELECT id, Name, RecordTypeId, AccountId, Account.Name, Internship_Cycle__c, Internship_Cycle__r.Name, CloseDate,  StageName 
                FROM Opportunity 
                WHERE Internship_Cycle__c IN: mapIdbyOldInternCyc.KeySet()
             System.debug('oppListByOldCyc '+oppListByOldCyc);
            //Set<Opportunity> oppsToClone = new Set<Opportunity>();
            List<Opportunity> oppsToClone = new List<Opportunity>();
            for(Id tmp : mapIdbyNewInternCyc.keySet()){                 
                for(Opportunity opp : oppListByOldCyc){
                    Opportunity opps = new Opportunity();
                    opps.Name = 'Clone Opportunity';
                    opps.RecordTypeId = opp.RecordTypeId;
                    opps.CloseDate = opp.CloseDate;
                    opps.StageName = opp.StageName;
                    opps.AccountId = opp.AccountId;
                    opps.Internship_Cycle__c = tmp;
            insert oppsToClone;
            }catch (Exception e){
            system.debug('Error '+e.getMessage() +' '+e.getLineNumber());

I have a task assigned to clone child record with new parent identity by referring the parent StartDate fields value with the record contains exactly one-year difference value. 


If Internship Cycle 'Cycle 1' have date 1/1/2016  and have 10 child records 'opportunities', If a user will create a new  Internship Cycle record 'Cycle 2' have date 1/1/2017. Then it'll clone all the opportunities from 'Cycle 1' to 'Cycle 2' with new parent identity.
This task needs to be in upsert logic on the opportunity. i.e., if the 'Cycle 1's one of the child records 'opportunity 1' will update, then the cloned opportunity from 'opportunity 1' needs to be updated.
This is a high priority task for me, plz help me!  :)