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
Max_gMax_g 

Add record in custom object after insert in another custom object

I need to add a record to a custom object after I insert a record into another custom object.  I have the trigger working for a straight forward insert, but I don't want to insert the record if a record already exists with that customer number.  Here is the code for my existing trigger.  The matching field in both custom objects is JDE_Cust__c; 

 

trigger GetAcctIdTrigger on Sales_History__c (after insert) {
    list <sales_history_comp__c> newshc = new list<sales_history_comp__c>();
    for (Sales_History__c sh : Trigger.new){
    system.debug('GeT SH = ' + sh);
       
    sales_history_comp__c ishc = new sales_History_Comp__c(); 
     ishc.Account__c = sh.Customer__c;
     ishc.JDE_Cust__c = sh.JDE_Cust__c;
     ishc.Customer_Name__c = sh.Cust_Name__c;
     ishc.Sales_History__c = sh.id;
     ishc.Comp_Year_1__c = sh.sales_Year__c;
     ishc.Comp_Year_2__c = '0000';
   system.debug('ISHC RECORDS = ' +ishc);    
     newshc.add(ishc);
      }
insert newshc;     
        }

Best Answer chosen by Admin (Salesforce Developers) 
Damien_Damien_

Better Yet:

 

trigger GetAcctIdTrigger on Sales_History__c (after insert) {
  
  Set<String> custIds = new Set<String>();
  for (Sales_History__c sh: Trigger.new)
  {
    custIds.add(sh.JDE_Cust__c;
  }

  List <sales_history_comp__c> newshc = new List<sales_history_comp__c>();
  for (Sales_History__c sh : Trigger.new){
    system.debug('GeT SH = ' + sh);
    
    if (custIds.contains(sh.JDE_Cust__c))
      continue;

    sales_history_comp__c ishc = new sales_History_Comp__c(); 
    ishc.Account__c = sh.Customer__c;
    ishc.JDE_Cust__c = sh.JDE_Cust__c;
    ishc.Customer_Name__c = sh.Cust_Name__c;
    ishc.Sales_History__c = sh.id;
    ishc.Comp_Year_1__c = sh.sales_Year__c;
    ishc.Comp_Year_2__c = '0000';
    system.debug('ISHC RECORDS = ' +ishc);    
    newshc.add(ishc);
  }
  insert newshc;     
}

 

All Answers

JPClark3JPClark3

1. Create a SET to hold some JDE_Cust__c values (string, integer, decimal, whatever type it is .) [ex. IncomingCusts]

2. Loop through trigger.new, to retrieive all the JDE_Cust__c values coming into the trigger, and place them into this SET.

3. Query sales_history__c WHERE JDE_Cust__c IN :IncomingCusts

4. Create another SET to hold the items that aready exist [ex. ExistingCust].

5. Loop through the query results and get the existing Custs JDE_Cust__c value and place it into this new set.

6. Now add your existing code into the trigger, but just before the "new sales_history__c statement, add an if statement

 

list <sales_history_comp__c> newshc = new list<sales_history_comp__c>();

for (Sales_History__c sh : Trigger.new){
    system.debug('GeT SH = ' + sh);
      

                   if (! ExistingCust.Contains(sh.JDE_Cust__c))    //Note the NOT expression

                   {

                         <your code>

                   }

}

insert newshc;     
}

 

Damien_Damien_
trigger GetAcctIdTrigger on Sales_History__c (after insert) {
  
  Set<String> custIds = new Set<String>();
  for (Sales_History__c sh: Trigger.New)
  {
    custIds.add(sh.JDE_Cust__c;
  }

  Map<String, Sales_History_Comp__c> compMap = new Map<String, Sales_History_Comp__c>();
  for (Sales_History_Comp__c comp: [SELECT JDE_Cust__c FROM Sales_History_Comp__c WHERE JDE_Cust__c IN :custIds])
  {
    compMap.put(comp.JDE_Cust__c, comp);
  }

  List <sales_history_comp__c> newshc = new List<sales_history_comp__c>();
  for (Sales_History__c sh : Trigger.new){
    system.debug('GeT SH = ' + sh);
    
    Sales_History_Comp__c comp = compMap.get(sh.JDE_Cust__c);
    if (comp != null)
      continue;

    sales_history_comp__c ishc = new sales_History_Comp__c(); 
    ishc.Account__c = sh.Customer__c;
    ishc.JDE_Cust__c = sh.JDE_Cust__c;
    ishc.Customer_Name__c = sh.Cust_Name__c;
    ishc.Sales_History__c = sh.id;
    ishc.Comp_Year_1__c = sh.sales_Year__c;
    ishc.Comp_Year_2__c = '0000';
    system.debug('ISHC RECORDS = ' +ishc);    
    newshc.add(ishc);
  }
  insert newshc;     
}

 

Damien_Damien_

Better Yet:

 

trigger GetAcctIdTrigger on Sales_History__c (after insert) {
  
  Set<String> custIds = new Set<String>();
  for (Sales_History__c sh: Trigger.new)
  {
    custIds.add(sh.JDE_Cust__c;
  }

  List <sales_history_comp__c> newshc = new List<sales_history_comp__c>();
  for (Sales_History__c sh : Trigger.new){
    system.debug('GeT SH = ' + sh);
    
    if (custIds.contains(sh.JDE_Cust__c))
      continue;

    sales_history_comp__c ishc = new sales_History_Comp__c(); 
    ishc.Account__c = sh.Customer__c;
    ishc.JDE_Cust__c = sh.JDE_Cust__c;
    ishc.Customer_Name__c = sh.Cust_Name__c;
    ishc.Sales_History__c = sh.id;
    ishc.Comp_Year_1__c = sh.sales_Year__c;
    ishc.Comp_Year_2__c = '0000';
    system.debug('ISHC RECORDS = ' +ishc);    
    newshc.add(ishc);
  }
  insert newshc;     
}

 

This was selected as the best answer
Max_gMax_g

Thanks for the quick response.

 

I have installed

 

SteveBowerSteveBower

As a different way of thinking about it, you should also use declarative features.   So, just to ask, have you declared  the sales_History_Comp__c.JDE_Cust__c field to be a unique field?

 

If you did that, then your original trigger would have come along, tried to create a new Sales_History_Comp__c record and failed because of the uniqueness criteria.  The entire trigger would have rolled back.  If you threw exception handling code into your trigger (which is always a good idea anyway), you'd be able to catch the exception so it didn't propogate further back to the user with an ugly error message.

 

Even if you use the code provided (which is ostensibly better since it does the checking up front), you should still probably set the uniqueness constraint.

 

Best, Steve.

Max_gMax_g
That is a good idea. When I created the object, I did not set that as a unique field because I originally thought I might have a record for each year combination the customer would create. I later decided to limit them to a single record per account, so I could easily make that a unique field. Thanks for the additional thought. Max Gilbert IT Infrastructure and Operations National Envelope 3211 Internet Blvd. Suite200 Frisco, TX 75034 Tel: 972-731-2788 Fax: 972-731-1156 National Envelope...Where our customers come first