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
Chenna4a2Chenna4a2 

Before Insert or Upsert list must not have two identically equal elements ( Error Message)

Here i am trying to insert the ShiptoCompanyID by checking whether the SAP Account ID exists on account object , then if not  create new account by populating Shiptocompany ID into  SAP Account ID through integration

 

Its throwing error message:

 

Error:System.ListException: Before Insert or Upsert list must not have two identically equal elements

 

Map<String,SAP_SODO__c> SODO_Map = new Map<String,SAP_SODO__c>();
List<SODO_Line_Item__c> SODO_Lineitems = new List<SODO_Line_Item__c>();

Map<string,List<SODO_Line_Item__c>> SAP_SODO_Lines = new Map<string,List<SODO_Line_Item__c>>();
//list<Account> acclist = new list<Account>();
map<String,Account> AccountIDs = new map<String,Account>();
// map<String,String> ShipSAPCompanyID = new map<String,string>();

//Test changes begin for address fix
List<String> ListShiptoSAPCompanyId = new List<String>();
for(Object olineAccount : SODOLine)
{
map<String,Object> oSODOAccount = (map<string,Object>)olineAccount;
if(oSODOAccount.get('ShipToSAPCompanyID') != null)
{
ListShiptoSAPCompanyId.add((string)oSODOAccount.get('ShipToSAPCompanyID'));
}
}
list<Account> acclist = [SELECT Name, Id,SAP_AccountID__c,BillingCity,BillingCountry,BillingPostalCode,BillingState,BillingStreet,
ShippingCity,ShippingCountry,ShippingPostalCode,ShippingState,ShippingStreet
FROM Account where SAP_AccountID__c IN :ListShiptoSAPCompanyId];

//Test changes end for address fix
// to populate accounts
for(Object olineAccount : SODOLine)
{
map<String,Object> oSODOAccount = (map<string,Object>)olineAccount;
system.debug('Test SODO ITEM ShipToSAPCompanyID::->' + (string)oSODOAccount.get('ShipToSAPCompanyID'));
system.debug('Test SODO Company Name::->' + (string)oSODOAccount.get('BillToCompanyName1'));

}
for(Object olineAccount : SODOLine)
{
map<String,Object> oSODOAccount = (map<string,Object>)olineAccount;
Account oAcc;
boolean foundexistingAcc = false;
system.debug('Test before for SODO ITEM ::->' + (string)oSODOAccount.get('ShipToSAPCompanyID'));
for(Account oExistingAcc :acclist )
{
system.debug('Test inside for loop-1');
system.debug('Test INFO ::' + (string)oSODOAccount.get('ShipToSAPCompanyID')+ '<->'+ oExistingAcc.SAP_AccountID__c);
if(((string)oSODOAccount.get('ShipToSAPCompanyID') != null) && ((string)oSODOAccount.get('ShipToSAPCompanyID') == oExistingAcc.SAP_AccountID__c))
{
oAcc = oExistingAcc ;
foundexistingAcc = true;
system.debug('Test found EXISTING ACCOUNT' + oAcc.id + '<->' + oAcc.Name);
break;
}
}
if(foundexistingAcc == false)
{
system.debug('Test coould not found EXISTING ACCOUNT CREATING NEW' );
oAcc = new Account(); //could not find existing acc... create a new one.

}
else
{
foundexistingAcc = false;

}
if(oSODOAccount.get('ShipToSAPCompanyID') != null)
{
if(oSODOAccount.get('BillToCompanyName1') != null)
{

//ShipSAPCompanyID.put((string)oSODOAccount.get('ShipToSAPCompanyID'),(string)oSODOAccount.get('BillToCompanyName1'));

oAcc.Name =(string)oSODOAccount.get('BillToCompanyName1');
oAcc.SAP_AccountID__c = (string)oSODOAccount.get('ShipToSAPCompanyID');

if(oSODOAccount.get('BillToStreet') != null)
oAcc.BillingStreet = (String)oSODOAccount.get('BillToStreet');

if(oSODOAccount.get('BillToCity') != null)
oAcc.BillingCity = (String)oSODOAccount.get('BillToCity');

if(oSODOAccount.get('BillToState') != null)
oAcc.BillingState = (String)oSODOAccount.get('BillToState');

if(oSODOAccount.get('BillToZipCode') != null)
oAcc.BillingPostalCode = (String)oSODOAccount.get('BillToZipCode');

if(oSODOAccount.get('BillToCountry') != null)
oAcc.BillingCountry = (String)oSODOAccount.get('BillToCountry');

if(oSODOAccount.get('ShipToStreet') != null)
oAcc.ShippingStreet = (String)oSODOAccount.get('ShipToStreet');

if(oSODOAccount.get('ShipToCity') != null)
oAcc.ShippingCity = (String)oSODOAccount.get('ShipToCity');

if(oSODOAccount.get('ShipToState') != null)
oAcc.ShippingState = (String)oSODOAccount.get('ShipToState');

if(oSODOAccount.get('ShipToZipCode') != null)
oAcc.ShippingPostalCode = (String)oSODOAccount.get('ShipToZipCode');

if(oSODOAccount.get('ShipToCountry') != null)
oAcc.ShippingCountry = (String)oSODOAccount.get('ShipToCountry');
}

}
acclist.add(oAcc);
// Test changes ends for account address


}
system.debug('Test UPSERTING ACCOUNTS:::->' + acclist);
// upsert acclist ;//SAP_AccountID__c;
for(Account oacc : [SELECT ID,SAP_AccountID__c FROM Account WHERE ID IN: acclist])
{
AccountIDs.put(oacc.SAP_AccountID__c,oacc);
}
upsert acclist ;

Satyendra RawatSatyendra Rawat

Hi

 

Hi,

I am got your problem, Problem is that which list you are insert/upsert in this list have duplicate record,

I have updated your code ,below code help for you. 

 

 

Map<String,SAP_SODO__c> SODO_Map = new Map<String,SAP_SODO__c>();
List<SODO_Line_Item__c> SODO_Lineitems = new List<SODO_Line_Item__c>();
Map<string,List<SODO_Line_Item__c>> SAP_SODO_Lines = new Map<string,List<SODO_Line_Item__c>>();
//list<Account> acclist = new list<Account>();
map<String,Account> AccountIDs = new map<String,Account>();
// map<String,String> ShipSAPCompanyID = new map<String,string>();
//Test changes begin for address fix
List<String> ListShiptoSAPCompanyId = new List<String>();
for(Object olineAccount : SODOLine)
{
map<String,Object> oSODOAccount = (map<string,Object>)olineAccount;
if(oSODOAccount.get('ShipToSAPCompanyID') != null)
{
ListShiptoSAPCompanyId.add((string)oSODOAccount.get('ShipToSAPCompanyID'));
}
}
list<Account> acclist = [SELECT Name, Id,SAP_AccountID__c,BillingCity,BillingCountry,BillingPostalCode,BillingState,BillingStreet,ShippingCity,ShippingCountry,ShippingPostalCode,ShippingState,ShippingStreet
FROM Account where SAP_AccountID__c IN :ListShiptoSAPCompanyId];

List<Account>lstUpdateAccount = new List<Account>();
//Test changes end for address fix
// to populate accounts
for(Object olineAccount : SODOLine)
{
map<String,Object> oSODOAccount = (map<string,Object>)olineAccount;
system.debug('Test SODO ITEM ShipToSAPCompanyID::->' + (string)oSODOAccount.get('ShipToSAPCompanyID'));
system.debug('Test SODO Company Name::->' + (string)oSODOAccount.get('BillToCompanyName1'));
}
for(Object olineAccount : SODOLine)
{
map<String,Object> oSODOAccount = (map<string,Object>)olineAccount;
Account oAcc;
boolean foundexistingAcc = false;
system.debug('Test before for SODO ITEM ::->' + (string)oSODOAccount.get('ShipToSAPCompanyID'));
for(Account oExistingAcc :acclist )
{
system.debug('Test inside for loop-1');
system.debug('Test INFO ::' + (string)oSODOAccount.get('ShipToSAPCompanyID')+ '<->'+ oExistingAcc.SAP_AccountID__c);
if(((string)oSODOAccount.get('ShipToSAPCompanyID') != null) && ((string)oSODOAccount.get('ShipToSAPCompanyID') == oExistingAcc.SAP_AccountID__c))
{
oAcc = oExistingAcc ;
foundexistingAcc = true;
system.debug('Test found EXISTING ACCOUNT' + oAcc.id + '<->' + oAcc.Name);
break;
}
}
if(foundexistingAcc == false)
{
system.debug('Test coould not found EXISTING ACCOUNT CREATING NEW' );
oAcc = new Account(); //could not find existing acc... create a new one.
}
else
{
foundexistingAcc = false;
}
if(oSODOAccount.get('ShipToSAPCompanyID') != null)
{
if(oSODOAccount.get('BillToCompanyName1') != null)
{
//ShipSAPCompanyID.put((string)oSODOAccount.get('ShipToSAPCompanyID'),(string)oSODOAccount.get('BillToCompanyName1'));
oAcc.Name =(string)oSODOAccount.get('BillToCompanyName1');
oAcc.SAP_AccountID__c = (string)oSODOAccount.get('ShipToSAPCompanyID');
if(oSODOAccount.get('BillToStreet') != null)
oAcc.BillingStreet = (String)oSODOAccount.get('BillToStreet');
if(oSODOAccount.get('BillToCity') != null)
oAcc.BillingCity = (String)oSODOAccount.get('BillToCity');
if(oSODOAccount.get('BillToState') != null)
oAcc.BillingState = (String)oSODOAccount.get('BillToState');
if(oSODOAccount.get('BillToZipCode') != null)
oAcc.BillingPostalCode = (String)oSODOAccount.get('BillToZipCode');
if(oSODOAccount.get('BillToCountry') != null)
oAcc.BillingCountry = (String)oSODOAccount.get('BillToCountry');
if(oSODOAccount.get('ShipToStreet') != null)
oAcc.ShippingStreet = (String)oSODOAccount.get('ShipToStreet');
if(oSODOAccount.get('ShipToCity') != null)
oAcc.ShippingCity = (String)oSODOAccount.get('ShipToCity');
if(oSODOAccount.get('ShipToState') != null)
oAcc.ShippingState = (String)oSODOAccount.get('ShipToState');
if(oSODOAccount.get('ShipToZipCode') != null)
oAcc.ShippingPostalCode = (String)oSODOAccount.get('ShipToZipCode');
if(oSODOAccount.get('ShipToCountry') != null)
oAcc.ShippingCountry = (String)oSODOAccount.get('ShipToCountry');
}
}
acclist.add(oAcc);
lstUpdateAccount.add(oAcc0;
// Test changes ends for account address
}
system.debug('Test UPSERTING ACCOUNTS:::->' + acclist);
// upsert acclist ;//SAP_AccountID__c;
for(Account oacc : [SELECT ID,SAP_AccountID__c FROM Account WHERE ID IN: acclist])
{
AccountIDs.put(oacc.SAP_AccountID__c,oacc);
}
upsert lstUpdateAccount ;
Chenna4a2Chenna4a2
Now if it find duplicate record it will inserts or updates the records ???

Just asking for confirmation. Thanks for your help ...