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
mandycmandyc 

Bulkify Trigger

Hello,

 

Can anyone help me bulkify the below trigger? I would like to insert the same Test_Opportunity__c records to the current opportunity record as was attached to the opportunity I cloned from (using the Opportunity_Clone_Id__c field).

 

 

trigger CreateOpportunityRelated on Opportunity (after insert) 
{ 

    for (Opportunity opp : Trigger.new)     
    { 
        ID cloneID = opp.Opportunity_Clone_ID__c; //OPPORTUNITY WE'RE CLONING FROM 
        ID oppID = opp.Id; //OPPORTUNITY WE CLONED INTO


        List<Test_Opportunity__c> tps = new List<Test_Opportunity__c>(); //EMPTY LIST OF TEST OPPORTUNITY OBJECTS 

        //POPULATE Test OPPORTUNITY OBJECTS
        for (Test_Opportunity__c test_opp : [SELECT Test_Number__c FROM Test_Opportunity__c WHERE Opportunity_Name__c = :cloneID])
        {
                        Test_Opportunity__c new_test_opp = New Test_Opportunity__c(
                        Opportunity_Name__c = oppId, 
                        Test_Number__c = test_opp.Test_Number__c);
                        tps.add(new_test_opp);
        }
        INSERT tps; //DO A SINGLE INSERT OF ALL Test OPPORTUNITY RECORDS 
    }
}

 Thank you!

Best Answer chosen by Admin (Salesforce Developers) 
Suresh RaghuramSuresh Raghuram

I think this will answer your question.

 

trigger CreateOpportunityRelated on Opportunity (after insert)

       List<Test_Opportunity__c> tps = new List<Test_Opportunity__c>(); //EMPTY LIST OF TEST OPPORTUNITY OBJECTS

       Set<Id> ids =new set<Id>();

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

    for (Opportunity opp : Trigger.new)    

    {

                ids.add( opp.Opportunity_Clone_ID__c);

                oppIds.add(opp.Id);

       }

        List< Test_Opportunity__c> test=[SELECT Test_Number__c FROM Test_Opportunity__c WHERE Opportunity_Name__c = :ids]

 

        //POPULATE Test OPPORTUNITY OBJECTS

        for (Test_Opportunity__c test_opp :test)

        {

                        Test_Opportunity__c new_test_opp = New Test_Opportunity__c(

                        Opportunity_Name__c = test_opp .Name,

                        Test_Number__c = test_opp.Test_Number__c);

                        tps.add(new_test_opp);

        }

 

        INSERT tps; //DO A SINGLE INSERT OF ALL Test OPPORTUNITY RECORDS

    }

}

All Answers

Suresh RaghuramSuresh Raghuram

I think this will answer your question.

 

trigger CreateOpportunityRelated on Opportunity (after insert)

       List<Test_Opportunity__c> tps = new List<Test_Opportunity__c>(); //EMPTY LIST OF TEST OPPORTUNITY OBJECTS

       Set<Id> ids =new set<Id>();

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

    for (Opportunity opp : Trigger.new)    

    {

                ids.add( opp.Opportunity_Clone_ID__c);

                oppIds.add(opp.Id);

       }

        List< Test_Opportunity__c> test=[SELECT Test_Number__c FROM Test_Opportunity__c WHERE Opportunity_Name__c = :ids]

 

        //POPULATE Test OPPORTUNITY OBJECTS

        for (Test_Opportunity__c test_opp :test)

        {

                        Test_Opportunity__c new_test_opp = New Test_Opportunity__c(

                        Opportunity_Name__c = test_opp .Name,

                        Test_Number__c = test_opp.Test_Number__c);

                        tps.add(new_test_opp);

        }

 

        INSERT tps; //DO A SINGLE INSERT OF ALL Test OPPORTUNITY RECORDS

    }

}

This was selected as the best answer
mandycmandyc

Thank you!