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 

apex create multiple records checkbox

I have a trigger that has multiple checkbox and when i user clicks any of them i need the trigger to create a new record with a specific record type. My code below only allows the creation of  the records but only if all the checkboxes are checked. Once the record has one product it doesnt create another product. Any help would be gratly appreciated. 
 
trigger TriggerNewYushinProd on Opportunity (before update, before insert) {

List<Yushin_Product__c> oppList = new List<Yushin_Product__c>();
for(Opportunity a:[SELECT Id,Name,AccountId,Robot__c,EOAT__c,Conveyor__c,Safety_Guarding__c,Type from Opportunity where Id IN:Trigger.new AND Id NOT IN(SELECT Opportunity__c from Yushin_Product__c)]){
for(Integer i=6;i<=100;i++)
    
{
if (a.Robot__c == true){
    
    oppList.add(New Yushin_Product__c(Opportunity__c=a.Id,Account__c=a.AccountId, RecordTypeId='012180000004huP',Equipment_Type__c='Robot' ));
    }
    
    if (a.EOAT__c == true){
    
    oppList.add(New Yushin_Product__c(Opportunity__c=a.Id,Account__c=a.AccountId, RecordTypeId='012180000004huU',Equipment_Type__c='EOAT' ));
    }
    
    if (a.Conveyor__c == true){
    
    oppList.add(New Yushin_Product__c(Opportunity__c=a.Id,Account__c=a.AccountId, RecordTypeId='012180000004huZ',Equipment_Type__c='Conveyor' ));
    }
    
    if (a.Safety_Guarding__c == true){
    
    oppList.add(New Yushin_Product__c(Opportunity__c=a.Id,Account__c=a.AccountId, RecordTypeId='012180000004hue',Equipment_Type__c='Safety Guarding' ));
    }
    If(oppList.Size()>0){
        upsert oppList;
       
        }
}
}
}

 
Michael Merino 1Michael Merino 1
if (a.Robot__c == true)
{ oppList.add(New Yushin_Product__c(Opportunity__c=a.Id,Account__c=a.AccountId, RecordTypeId='012180000004huP',Equipment_Type__c='Robot' )); }

ELSE if (a.EOAT__c == true)

{ oppList.add(New Yushin_Product__c(Opportunity__c=a.Id,Account__c=a.AccountId, RecordTypeId='012180000004huU',Equipment_Type__c='EOAT' )); }
Michael Merino 1Michael Merino 1
or if you are trying to create more than one Opportunity record, use Insert rather than upsert
Bob_zBob_z
I am trying to create multiple products at once. When i select EOAT and Robot checkboxes, the trigger will create both records. I also want the funionaltiy for a user to select Robot then save the opportunity, robot product is created (YEAH!), but then I need to add a EOAT at a seperate time, click the EOAT checkbox and save. At that time a new EOAT product record should be created. Currently the trigger does not do that  (Booo!)
Michael Merino 1Michael Merino 1
in order to do an upsert, you need an external Id,  is one of these fields an external id?  from your description, do you ever need to update an EOAT or Robot record?  If not just make this a before insert trigger and change upsert to insert.
Bob_zBob_z
Thanks for your help Michael,

When i change the trigger to a before insert nothing happens. When i change it to a before insert, before update I get an error. 
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger TriggerNewYushinProd caused an unexpected exception, contact your administrator: TriggerNewYushinProd: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 00618000007WZnc) is currently in trigger TriggerNewYushinProd, therefore it cannot recursively update itself: []: ()


I need to be able to insert a new product record if someone goes back into the opportunity and adds more product, but the trigger only fires when there are no products on the opportunity record.

 
trigger TriggerNewYushinProd on Opportunity ( before insert, before update) {

List<Yushin_Product__c> oppList = new List<Yushin_Product__c>();
for(Opportunity a:[SELECT Id,Name,AccountId,Robot__c,Product_Type__c,EOAT__c,Conveyor__c,Safety_Guarding__c,Type from Opportunity where Id IN:Trigger.new AND Id NOT IN(SELECT Opportunity__c from Yushin_Product__c)]){

    
{
    if (a.Product_Type__c.contains('Robot') ){
    
    oppList.add(New Yushin_Product__c(Opportunity__c=a.Id,Account__c=a.AccountId, RecordTypeId='012180000004huP',Equipment_Type__c='Robot' ));
    }
    
   if ( a.Product_Type__c.contains('EOAT')){
    
    oppList.add(New Yushin_Product__c(Opportunity__c=a.Id,Account__c=a.AccountId, RecordTypeId='012180000004huU',Equipment_Type__c='EOAT' ));
    }
    
    if (a.Product_Type__c.contains('Conveyor')){
    
    oppList.add(New Yushin_Product__c(Opportunity__c=a.Id,Account__c=a.AccountId, RecordTypeId='012180000004huZ',Equipment_Type__c='Conveyor' ));
    }
    
    if (a.Product_Type__c.contains('Safety Guarding')){
    
    oppList.add(New Yushin_Product__c(Opportunity__c=a.Id,Account__c=a.AccountId, RecordTypeId='012180000004hue',Equipment_Type__c='Safety Guarding' ));
    }
    
    
    
   
    If(oppList.Size()>0){
        insert oppList;
       
        }
}
}
}


 
Michael Merino 1Michael Merino 1
sorry.  I think you need an after trigger not before trigger.  "Before triggers can be used to update or validate record values before they are saved to the database.

After triggers can be used to access field values that are set by the database (such as a record's Id or lastUpdated field) and to affect changes in other records,"  from: https://developer.salesforce.com/forums/?id=9060G000000XaJjQAK