+ Start a Discussion

Adding a single sobject to list<sobject> overwrites sobject in list with VF Wizard

Hopefully that subject makes sense.


I'm having a puzzling issue. This issue cannot be reproduced on purpose and my debug logs haven't shown anything to hint what's going on. Here's the scenario:


I have a visualforce page that is intended to build up a series of Sobjects and then insert those Sobjects with a single DML. The user enters each item in called "NewProduct" which is then added to the list<sobjects>. The new Product is then "reset" for the user to enter a new one -- based on the previous values.

The goal is to give the user the capability to enter a new record, have the system perform a series of client side (javascript) calculations, add the records to a "product cart" and then save the whole cart to the server.


This works... most of the time. Every now and then the list of sobjects - or my "product cart" - has one of the items in the list over-written by the incoming "NewProduct." It doesn't happen all the time. I can't reproduce it, and I'm not sure what I'm doing wrong in my code that could cause this problem.


So with no further ado - here's the method in the controller that is "adding" the product to the cart:

    public void AddProduct()
        //values of current New Product
        system.debug('Starting Values of NewProduct!');
        system.debug('UQ: ' + NewProduct.Unit_Quantity__c);		
        system.debug('UP: ' + NewProduct.Unit_Price__c );
        system.debug('TAP: ' + NewProduct.Total_Amount_Paid__c );
        system.debug('SD: ' + NewProduct.Start_Date__c);         
        system.debug('ED: ' + NewProduct.End_Date__c );
        system.debug('ARRT: ' + NewProduct.Annualized_Rec_Rev_Term__c);
        system.debug('MRP: ' + NewProduct.Monthly_Recurring_Price__c );
        system.debug('ARP: ' + NewProduct.Annual_Recurring_Price__c );        
        system.debug('OTP: ' + NewProduct.One_Time_Price__c);
        ID UIProd = NewProduct.Contract_Product__c;
        date StartDate = NewProduct.Start_Date__c;
        date EndDate =   NewProduct.End_Date__c;
		integer UQ = integer.valueof(NewProduct.Unit_Quantity__c);		
        decimal UP = NewProduct.Unit_Price__c ;
        decimal TAP = NewProduct.Total_Amount_Paid__c;
        decimal CA = NewProduct.Capped_Amount__c;
        UpdatedField = 'All';

        system.debug('Size after variable: ' + prods.size());
        system.debug('NewProduct to Add: ' + NewProduct);
        if(Schedule =='Month')
            //NewProduct.Annual_Price__c = (getPriceBookOpp().Amount / NewProduct.Quantity__c);
		/* Add NewProduct into ProductCart */
		ProductCart.add(new cProd(NewProduct));
        for(cProd cp :ProductCart)
            if(cp.vname == 'changeme')
                cp.vname = string.valueof(counter)+ string.valueof(NewProduct.Contract_Product__c);
        counter = counter+1;
        ID AccountID = NewProduct.Account__c;
        ID ProductID = NewProduct.Contract_Product__c;

        boolean NewClient = NewProduct.New_Client__c;
		/* Next 3 lines Clone Product to "Reset" new product with same values */
        Contract_Product__c TempProduct = NewProduct.clone(false,true,false, false);
        NewProduct = new Contract_Product__c(); 
        NewProduct = TempProduct.clone(false,true,false, false);          


/*Wrapper Class and Product Cart Methods */
    public class cProd
        public Contract_Product__c Prod {get; set;}
        public Boolean cSelected {get; set;}
        public string vName {get; set;}
        public cProd(Contract_Product__c p)
            Prod = p; //Contract Product object
            cSelected = false; //Selected Checkbox
    private list<cProd> ProductCart {get;set;}  
    public list<cProd> getProductCart()
        system.debug('This is the getProductCart: ' +ProductCart);
        return ProductCart;


Anyone see what I'm doing wrong here to cause my problem? Anyone have suggestions on how to make this better?

Is there a reason why you can't just start with a new Product(...) instead of reusing an existing NewProduct instance all the time ? Rup

Rup thanks for a reply. I did New Contract_product__c in a previous version.  The issue I described above still occured. I'm doing the temp product and a clone just to save lines of code and minimize re-writes when we have to add new fields.