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
BobPBobP 

Create multiple child records on an opportunity with the ability to create more records

Below I am using this trigger to create new products, but if a user wants to add more products, my trigger does not allow it because it sees a record is already assigned to the opportunity it does nothing.

I tried to change part of my code to allow a user to add more products, but I get errors in my code.

I want to give my users the ability to add more products to the opportunity record if needed and I can't figure out how to update my trigger to handle this request.

Is there a way to change the behavior in the trigger so once a user enters a number in the product quantity field and the products are created the trigger then blanks the product quantity field so if a user enters a new product quantity number, it then created that number of products for that record? 

If anyone can give me advice to  update my trigger to accomadate this request or if there is another trigger out there to accomplish this request I would greatly appreciate it.
 
trigger tr_MultiProductsCreated on Opportunity  (after insert, after update) 
{
  
   List<Yushin_Product__c> ProductRecordsFinalListToInsert = New List<Yushin_Product__c>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate)
    {
        For(Opportunity opp : Trigger.New)
        {
            If(opp.Product_Qauntity__c != null)
            {
                List<Yushin_Product__c> fetchingAlreadyExistsedRecords = [Select Id FROM Yushin_Product__c WHERE Opportunity__c =:opp.Id];
                
                If(fetchingAlreadyExistsedRecords.IsEmpty())
                {
                    // I need to create more records if a new number is added to the field.
                    For(Integer I = 0; I < opp.Product_Qauntity__c; I++)
                    {
                        Yushin_Product__c prd = New Yushin_Product__c();
                        
                        prd.Opportunity__c = opp.Id;
                        ProductRecordsFinalListToInsert.add(prd);
                    }
                }
                
            }
            
            try{
                If(!ProductRecordsFinalListToInsert.IsEmpty()){
                    insert ProductRecordsFinalListToInsert;
                }
            }
            Catch(Exception e){
                System.debug('The thrown exception for CreatingAutoRecords is:: ' + e.getMessage());
            }
        }
    }
    
}

 
Deepali KulshresthaDeepali Kulshrestha
Hi,

Please try the below code as it may help you in solving your problem.
trigger tr_MultiProductsCreated on Opportunity(after insert, after update) {

    List < Yushin_Product__c > ProductRecordsFinalListToInsert = New List < Yushin_Product__c > ();

    If(Trigger.IsInsert || Trigger.IsUpdate) {
        For(Opportunity opp: Trigger.New) {
            If(opp.Product_Qauntity__c != null) {
                List < Yushin_Product__c > fetchingAlreadyExistsedRecords = [Select Id FROM Yushin_Product__c WHERE Opportunity__c =: opp.Id];

                If(fetchingAlreadyExistsedRecords.IsEmpty()) {
                   
                    For(Integer I = 0; I < opp.Product_Qauntity__c; I++) {
                        Yushin_Product__c prd = New Yushin_Product__c();

                        prd.Opportunity__c = opp.Id;
                        ProductRecordsFinalListToInsert.add(prd);
                    }
                } else if (fetchingAlreadyExistsedRecords.size() < opp.Product_Qauntity__c) {
                    For(Integer I = 0; I < opp.Product_Qauntity__c - fetchingAlreadyExistsedRecords.size(); I++) {
                        Yushin_Product__c prd = New Yushin_Product__c();

                        prd.Opportunity__c = opp.Id;
                        ProductRecordsFinalListToInsert.add(prd);
                    }
                } else if (fetchingAlreadyExistsedRecords.size() > opp.Product_Qauntity__c) {
                    delete ProductRecordsFinalListToInsert;
                    For(Integer I = 0; I < opp.Product_Qauntity__c; I++) {
                        Yushin_Product__c prd = New Yushin_Product__c();

                        prd.Opportunity__c = opp.Id;
                        ProductRecordsFinalListToInsert.add(prd);
                    }
                }

            }

            try {
                If(!ProductRecordsFinalListToInsert.IsEmpty()) {
                    insert ProductRecordsFinalListToInsert;
                }
            }
            Catch(Exception e) {
                System.debug('The thrown exception for CreatingAutoRecords is:: ' + e.getMessage());
            }
        }
    }
}

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
BobPBobP
Hi Deepali,
Thank you for helping. I tried your updated code and the trigger is still behaving the same way. It only allows me to create products once. When i tried it again, no products are created becuase it sees the value in the Product quantity field. 
Deepali KulshresthaDeepali Kulshrestha
Hi,

Please try the below code as I have made some changes. Hope this will do the trick and will help you in finding the solution to your problem.
trigger testTrigger on Opportunity(after insert, after update) {

    List <Yushin_Product__c> ProductRecordsFinalListToInsert = New List <Yushin_Product__c> ();

    If(Trigger.IsInsert || Trigger.IsUpdate) {
        For(Opportunity opp: Trigger.New) {
            If(opp.Product_Qauntity__c != null) {
                List < Yushin_Product__c > fetchingAlreadyExistsedRecords = [Select Id FROM Yushin_Product__c WHERE Opportunity__c =: opp.Id];

                If(fetchingAlreadyExistsedRecords.IsEmpty()) {

                    For(Integer I = 0; I < opp.Product_Qauntity__c; I++) {
                        Yushin_Product__c prd = New Yushin_Product__c();

                        prd.Opportunity__c = opp.Id;
                        ProductRecordsFinalListToInsert.add(prd);
                    }
                } else if (fetchingAlreadyExistsedRecords.size() < opp.Product_Qauntity__c) {
                    For(Integer I = 0; I < opp.Product_Qauntity__c - fetchingAlreadyExistsedRecords.size(); I++) {
                        Yushin_Product__c prd = New Yushin_Product__c();

                        prd.Opportunity__c = opp.Id;
                        ProductRecordsFinalListToInsert.add(prd);
                    }
                } else if (fetchingAlreadyExistsedRecords.size() > opp.Product_Qauntity__c) {
                    delete fetchingAlreadyExistsedRecords;
                    For(Integer I = 0; I < opp.Product_Qauntity__c; I++) {
                        Yushin_Product__c prd = New Yushin_Product__c();

                        prd.Opportunity__c = opp.Id;
                        ProductRecordsFinalListToInsert.add(prd);
                    }
                }

            }

            try {
                If(!ProductRecordsFinalListToInsert.IsEmpty()) {
                    insert ProductRecordsFinalListToInsert;
                }
            }
            Catch(Exception e) {
                System.debug('The thrown exception for CreatingAutoRecords is:: ' + e.getMessage());
            }
        }
    }
}

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
BobPBobP
Hi Deepali, 
Thank you for your help. The trigger is working pretty good now,but if a opportunity already has say two products created already, and if I try to create two more products. The trigger changes the two products already created to the new products I entered. I was wondering if there is a way to have the trigger ignore products that were previously created and just add more products from the quantity field. So if I already have two products and I want to add 3 more I would enter the number 3 in the product quantity field save the opportunity and the 3 products are added to the opportunity along with the two previosuly created. I using this trigger in conjunction with a Flow. The screenshots below show how a user enters a number value in one screen, when the user clicks next the trigger creates those products and  then my flow shows a new screen where the user can enter the product information for each product they want to create based off of the number value that the user enter in the first flow screen. 

User-added image
User-added image