function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RajeevlsHydRajeevlsHyd 

Before Insert or Upsert list must not have two identically equal elements

I am getting this error when I am selecting two items to be inserted from a list . This is working fine when I am inserting a single record, but giving error when multiple items are selected.

 

Please help here :

 

--------------------

 

public PageReference addSelectedProductAction()
{
for(ProductWrapper pw: products){
if(pw.isSelected){
pw.insertFirstProdAndDealSplit();
selectedProds.add(pw);
}
}

//------------------------------------- Opportunity Line Item Insert
String OppId = ApexPages.currentPage().getParameters().get('Id');
For (ProductWrapper p : selectedProds)
{
OpportunityLineItem oppItem = new OpportunityLineItem();
oppItem.OpportunityId = OppId ;
oppItem.PriceBookEntryId = p.product.Id;
oppItem.Roll_Call__c = p.summaryRecord.totalRollCall;
oppItem.UK_Setup__c = p.summaryRecord.totalSetup;
oppItem.ARC__c = p.summaryRecord.totalARC;
mapOppLineItems.put(p, oppItem);
p.firstDealSplit.dsObj.NAS_Opportunity__c = OppId;
lstDealSplitsFirstInsert.add(p.firstDealSplit.dsObj);
}

if(mapOppLineItems.size()>0)
{
Insert mapOppLineItems.values();
}
if(lstDealSplitsFirstInsert.size()>0)
{
Insert lstDealSplitsFirstInsert; /// Giving error on this line of the code
}
return null;
}

 

---------------------

Sagarika RoutSagarika Rout

Hi Rajeev,

        You are getting the same record inside the list twice.
        just initialize the object inside the for loop , I think you will not get error

 

public PageReference addSelectedProductAction()
		
		
{
for(ProductWrapper pw: products){
if(pw.isSelected){
pw.insertFirstProdAndDealSplit();
selectedProds.add(pw);
}
}

//------------------------------------- Opportunity Line Item Insert
String OppId = ApexPages.currentPage().getParameters().get('Id');
For (ProductWrapper p : selectedProds)
{
OpportunityLineItem oppItem = new OpportunityLineItem();
oppItem.OpportunityId = OppId ;
oppItem.PriceBookEntryId = p.product.Id;
oppItem.Roll_Call__c = p.summaryRecord.totalRollCall;
oppItem.UK_Setup__c = p.summaryRecord.totalSetup;
oppItem.ARC__c = p.summaryRecord.totalARC;
mapOppLineItems.put(p, oppItem);
//just for your understanding i have written, modify it with Api definition
dsObj dsObjnew = new dsObj();
dsObjnew.NAS_Opportunity__c = OppId;
lstDealSplitsFirstInsert.add(dsObjnew);
}

if(mapOppLineItems.size()>0)
{
Insert mapOppLineItems.values();
}
if(lstDealSplitsFirstInsert.size()>0)
{
Insert lstDealSplitsFirstInsert; /// Giving error on this line of the code
}
return null;
}

 

 

Sagarika Rout

SFDC Developer

RajeevlsHydRajeevlsHyd

Thanks for the help .. But after declaring also , it is not solving the issue , it is giving the same error

Sagarika RoutSagarika Rout

Other wise do one thing , declare a set of ID,

 like set<ID> setOfOpptID = new set<ID>();

 

then store all opportunity Id in to it. and iterate the set inside a for loop, let me know whether it is working or not !!!

 

 

Regards

Sagarika Rout

SFDC Developer

RajeevlsHydRajeevlsHyd

Thanks for your inputs.. the error still remains the same