You need to sign in to do that
Don't have an account?

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; } } } }
{ 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' )); }
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.
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