• rajavardhan shontireddy
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
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());
            }
        }
    }
    
}

 
  • July 11, 2019
  • Like
  • 0
Hello Everyone,

I have a requirement wherein I need to make a Opportunity record read only when the opportunity stage reaches to Closed won or either Closed Lost.
Any help is much appreciated.

Thanks