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
Dev2IndiaDev2India 

Apex trigger : Creating child records

I am using the below code to create child record, based on certain conditions. Howeever, in the below code , do not want to check the values in trigger.old. I want to query for the child records if those exists are not, then to create the child record. If child record exist ,then do not create the child record and if not then create the child record.
trigger createSplitCommissionOnOpportunityX on Opportunity (after insert, after update) {
   
    List<Split_Commissions__c> SCToInsert = new List<Split_Commissions__c>  ();
  
    for (Opportunity o : Trigger.new) {
       
      
    if ( ((Trigger.isInsert) ||
          (Trigger.isUpdate) && (Trigger.oldMap.get(o.id).StageName !=  'Closed Won')) &&
         (o.StageName == 'Closed Won') )
    {
        Split_Commissions__c SC = new Split_Commissions__c ();        
       
        sc.Opportunity_Name__c = o.id;
       
        SCToInsert.add(sc);
       
       
        }//end if
       
    }//end for o
   
    //once loop is done, you need to insert new records in SF
    // dml operations might cause an error, so you need to catch it with try/catch block.
    try {
        insert SCToInsert;
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
}

Please suggest the code how to query to check in the code if child exists or not.
Much appreciated.




hitesh90hitesh90
Hi,

You have to use inner query to check child record of opportunity.

see below code:

Apex Trigger:
trigger createSplitCommissionOnOpportunityX on Opportunity (after insert, after update) {
List<Split_Commissions__c> SCToInsert = new List<Split_Commissions__c>();
List<Opportunity> lstOpp = [select id, StageName, (select id from Split_Commissionss__r) from Opportunity where id in: trigger.newmap.keyset()];
for (Opportunity o : lstOpp) {

  if ( ((Trigger.isInsert) || (Trigger.isUpdate) && (Trigger.oldMap.get(o.id).StageName !=  'Closed Won')) && (o.StageName == 'Closed Won') && o.Split_Commissionss__r.size == 0){
   Split_Commissions__c SC = new Split_Commissions__c ();
   sc.Opportunity_Name__c = o.id;
   SCToInsert.add(sc);
  }//end if
}//end for o

//once loop is done, you need to insert new records in SF
// dml operations might cause an error, so you need to catch it with try/catch block.
try {
  insert SCToInsert;
} catch (system.Dmlexception e) {
  system.debug (e);
}
}

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
Email :- hiteshpatel.aspl@gmail.com
My Blog:- http://mrjavascript.blogspot.in/
Dev2IndiaDev2India
I received the below error , please have a look in my code :
Error: Compile Error: Didn't understand relationship 'TM_Buildout__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name.
Apex trigger :



trigger createTMOB on Opportunity (after insert, after update) {
           
            List<TM_Buildout__c> TMToInsert = new List<TM_Buildout__c>  ();
            List<Opportunity> lstOpp = [select id, StageName ,(select id from TM_Buildout__r) from Opportunity where id in: trigger.newmap.keyset()];
            for (Opportunity o : lstOpp) {
                       
                        
            if ( ((Trigger.isInsert) || (Trigger.isUpdate) )
            && (o.StageName == 'Closed Won' && o.Product__c == 'TM' && o.Product_Type__c.startswith('BB') ) && o.TM_Buildout__r.size == 0 )
           
                   
                {
                TM_Buildout__c TM = new TM_Buildout__c ();        
                tm.Name=o.Account_Name_update__c;
                tm.Opportunity__c = o.Id;
              
                TMToInsert.add(tm);
               
               
                }   
               
            }
                insert TMToInsert;
          
        }

Dev2IndiaDev2India
I corrected the relationship name after looking Opportunity child relation ship, then got the error :
Invalid foreign key relationship: Opportunity.TM_Onboarding__r at line 8 column 112