+ Start a Discussion
GailGail 

Upsert is creating duplicates rather than updating

I have an upsert statement that is creating duplicates of Opportunity Line Items instead of updating them. I probably have not got the id in the set I'm upserting but I've looked and can't see how.

Basically, I have a Add Product Page (thanks michaelforce.org for providing the base code) that has everything on one page. 

The apex controller creates a list (shoppingCart) of the existing opportunity line items, as follows:

public opportunityLineItem[] shoppingCart {get;set;}

shoppingCart = [select Id, Quantity, UnitPrice, Budget__c, FlightEnd__c, CampaignName__c,
        SpecialTerms__c, Targeting__c, PricingModel__c, DailyCap__c, Pacing__c, ServiceDate, Description, PriceBookEntryId,
        PriceBookEntry.Name, PriceBookEntry.IsActive, PriceBookEntry.Product2Id, PriceBookEntry.Product2.Name,
        PriceBookEntry.PriceBook2Id from opportunityLineItem where OpportunityId=:theOpp.Id];
//note: I have queried the opp prior to this...

The save function (which is duplicating every line item) is:
// method to delete any removed lines and add/update selected rows
    public PageReference onSave(){
        Savepoint sp = Database.setSavepoint();
        try{
            // If previously selected products are now removed, we need to delete them
            if(forDeletion.size()>0)
                delete(forDeletion);
       
            // Previously selected products may have new quantities and amounts, and we may have new products listed, so we use upsert here
            // if we have products, upsert them
            if(shoppingCart.size()>0) {
                // grab all OLIs and upsert them
                List<OpportunityLineItem> olis = new List<OpportunityLineItem>();
                for (OpportunityLineItem oli : shoppingCart) olis.add(oli);
                upsert olis;
            }
        }
        catch(Exception e){
            Database.rollback(sp);         
            ApexPages.addMessages(e);
            return null;
        }

So, it seems like ID isn't getting added to the olis list? I tried upsert olis id; just for kicks but then got an error "field integrity exception: unknown (scheduling not enabled on product)". Then for fun, I tried update olis; and got the issue "id not specified in an update call". But didn't I specify the ids in the initial query for ShoppingCart (i.e. [select Id from OpportunityLineItem where...])

I also tried to create the shopping cart list a little differently:

(for OpportunityLineItem oli : [select Id, Quantity, UnitPrice, Budget__c, FlightEnd__c, CampaignName__c,
        SpecialTerms__c, Targeting__c, PricingModel__c, DailyCap__c, Pacing__c, ServiceDate, Description, PriceBookEntryId,
        PriceBookEntry.Name, PriceBookEntry.IsActive, PriceBookEntry.Product2Id, PriceBookEntry.Product2.Name,
        PriceBookEntry.PriceBook2Id from opportunityLineItem where OpportunityId=:theOpp.Id]){
shoppingcart.add(oli) }

but then get an attempt to dereference a null object error when I open the page. 

I ran this code and added system debugs and it's finding the opp but my system debug message that comes right after assigning to the shopping cart never does appear.
Best Answer chosen by Gail
GailGail
turns out my code was fine. There was an old trigger running on Opp Line Item that was essentially duplicating each line item!