+ Start a Discussion
hisrinuhisrinu 

Deleting duplicate records using spring10

Hi,

 

I would like to delete duplicate records in Opportunity line items. I am using the below query to delete the records.

 

 List<AggregateResult> OLIdupes = [select count(id),opportunityId, pricebookentryid from opportunitylineitem group by opportunityId, pricebookentryid having count(id)>1];

 

Here I am getting the duplicates, but the problem is I am not able to get the OppLineItemId to delete them.

 

Is there something I am missing here? Any suggestions is highly appreciated.

 

 

JimRaeJimRae

You then need to loop through your AggregateResult list to get the opportunityid.

 

 

Like this:

 

 

List<AggregateResult> OLIdupes = [select count(id),opportunityId, pricebookentryid from opportunitylineitem group by opportunityId, pricebookentryid having count(id)>1]; for(AggregateResult ar : OLIdupes){ system.debug('\n\nOpportunityID='+ ar.get('opportunityid')); }

 

 

 

hisrinuhisrinu

Hey Jim,

 

Thanks for your response, I need to delete the OppLineItems not the Opportunities.

 

Any idea on this?

JimRaeJimRae

Ah, much more complicated, that is for sure.

Do you want to delete all of the OLI's? Or just 'dedup' down to one of the duplicates per opportunity?

My question would be, how will you know programatically, which duplicate is the "right one"  In our org, when we have this, sometimes, one will be at a zero value, and the other will have pricing applied to it.  You will need to determine how to loop through the duplicates, determine which is the "good one" and delete the rest.

 

You will probably need to map a list of OLI's to each opportunity, and then process through the list of maps.