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
Bob_zBob_z 

Flow to create multiple child records

I was wondering if there is a way to create mutiple child records for an opportunity by using a number field on the opportunity record? 

Below I am using this trigger to create new products, but if a user wants to add more products my code looks at that field and creates the number of product that match that number field. Iwant to give them the ability to add mor eproducts to the opportunity record if needed and I can't figure out how to update my trigger to do so that is why i am wondering if there's a way to do this with a Flow instead. 

If anyone can give me advice to either update my trigger to accomadate this request or how to create that flow I would greatly appreciate it.

Thanks,
Bob
 
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());
            }
        }
    }
    
}

 
rajavardhan shontireddyrajavardhan shontireddy
@Bob_poliquin 
Hi Bob,
Yes you can create or delete  child records from flow  . but what happens when user reduces count on Product_Qauntity__c 
then child records should be deleted

you have to launch flow from Process builder whenever Product_Qauntity__c  is changed and Product_Qauntity__c  > prior value of Product_Qauntity__c  
then launch a flow which creates Opportunity Child records .

 2. whenever Product_Qauntity__c  is changed and Product_Qauntity__c  < prior value of Product_Qauntity__c  
then launch a flow which deletes  Opportunity Child records .

Note
you design flow but while  loading data through data loader you need to disable  process builder  as flow doesn't handle bulk load
or else you need to start a flow such that it avoids  bulk loading of data by using custom settings or custom label . 



Its better to handle this scenario in Sync Triggers or Async Triggers Logic  which is much better than flows.

if your org has less data then go for flow approach .




.

 
Bob_zBob_z
Good afternoon rajavardhan, I was wondering if your solution could be update so the user can click a button to add products? I want the product quantity field hidden on the page, so they would enter the product quantity amount through the flow screen. Or maybe my trigger can so this requirement  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?  trigger tr_MultiProductsCreated on Opportunity (after insert, after update) { List ProductRecordsFinalListToInsert = New List(); If(Trigger.IsInsert || Trigger.IsUpdate) { For(Opportunity opp : Trigger.New) { If(opp.Product_Qauntity__c != null) { List 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()); } } } }