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
JDevJDev 

Upsert error

Getting an upsert error in refreshed sandbox on CS1 - error is in a test class. The upsert statement is using the very field reported as the duplicate as the basis for the upsert match (case sensitive external id) on a custom object - test upserts a single row that should match an existing record and update, instead seems to attempt an insert and gets an error.

 

Division SummariesSystem.DmlException: Upsert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: Division_Summary_Unique__c duplicates value on record with id: a0ES0000000OXUR:

NasipuriNasipuri

Hi , This is a proper error message , Salesforce.com is doing Update not insert and Update of the record is failing. From the Test method you are programmatically supplying a value to the "Division_Summary_Unique__c" field that is matching to an existing record in the system. Seems you have configured the Division_Summary_Unique__c field as Unique field . So when from the test method Upsarting the record supply a value in the Division_Summary_Unique__c field which is very unique , will not match to your record in the table. 

 

Thanks and Regards,

Dinesh Nasipuri

 

jkucerajkucera

Is there an elegant resolution to remove dupes?  The best I can think of for my situation:

 

 

Set<String> keys=new Set<String>();
for (Campaign c:campaigns){
keys.add(c.WebEx_Session_Key__c);
}

List<Campaign> campaignsThatAreDupes=[SELECT WebEx_Session_Key__c FROM Campaign WHERE WebEx_Session_Key__c IN :keys];

Set<String> dupeKeys=new Set<String>();


for (Campaign c2:campaignsThatAreDupes){
dupeKeys.add(c2.WebEx_Session_Key__c);
}

for (Integer i=0; i<=Campaigns.size();i++ ){
if (dupeKeys.contains(campaigns[i].WebEx_Session_Key__c)){
system.debug('Campaign removed:'+campaigns[i].name);
Campaigns.remove(i);
}
}
try{
upsert campaigns;
}catch(System.DMLException e){
system.debug('Campaigns were not inserted: '+e);
}//try

 

 

 

 

 

I wish chrome could copy & paste nicely :(


Message Edited by jkucera on 03-29-2010 09:01 PM