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

DML Statments with Related Object Records
I'm trying to insert two object records, but I'm getting an error because the 2nd object list doesn't see the 1st object id yet and I'm getting a "Missing Required Field validation error". Is there a way I can time, or chain these so they are both inserted with the correct relationship.
public StoreFront2(){ ct=new Purchase_Line_Items__c(); ct2=new Contact(); flag = false; flip = false; flag2 = true; } public PageReference buy(){ List<Merchandise__c> toMerch = new List<Merchandise__c>(); List<id> updateMerch = new List<id>(); PageReference send = new PageReference('/apex/StoreCart2'); if(ct != null && cart !=null ){ List<DisplayMerchandise> counter = new List<DisplayMerchandise>(); List<Merchandise_Line_Item__c> merchi = new List<Merchandise_Line_Item__c>(); Decimal total = 0; counter = cart.values(); for(Integer i = 0; i < counter.size();i++){ Decimal totalPrice = counter.get(i).count; total +=totalPrice; ct.Item_Quantity__c=total; system.debug(ct); merchi.add(new Merchandise_Line_Item__c(Name ='Pencils',Purchases__c=ct.id, Merchandise_Item_Stable__c='a02F000000AuhTh')); } insert ct; insert merchi;
You have to insert your CT object first because it is does not have an Id value until it is inserted. Try moving your insert ct line above your for loop, then update your ct object after your loop with your updated Item Quantity value.
I moved it up, but it's still not working.
Apex