0

It's vague error message and I have already spent several hours trying to find out what I'm doing but could not able to find the cause I'm trying to do SObject.clone and I'm not preserving the Id here is my code and hopefully I will get some shed on this issue of mine.

Id sObjectId =  'xxxxx';
String objectName = ((Id)sObjectId).getSobjectType().getDescribe().getName();
String sSOQL = buildSOQL();
SObject existingRecord = Database.query(sSOQL);
SObject newRecord = sObjectId.getSObjectType().newSObject(sObjectId);
//existingRecord.Id = null;
newRecord = existingRecord.clone(false, false, false, false);

insert newRecord;

I have followed this blog about how to deal with this type of error message but no avail.

One possible option I'm thinking is how do I compare if I have a duplicate value in the above code and use Set first and then try to insert them?

19:05:03.189 (1189951565)|EXCEPTION_THROWN|[94]|System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: unknown duplicates value on record with id: a1cV00000005bDm: [] 19:05:03.189 (1190540595)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: unknown duplicates value on record with id: a1cV00000005bDm: []

2
  • 1
    The blog post you linked mentions that this is probably due to having fields marked as unique on your SObject. Does your custom object have any unique fields? If so, I think the easiest way around this error is to simply not include any unique fields in the query string that buildSOQL() is creating (and if you're building the string using the fields from the SObjectDescribeResult, you can use DescribeFieldResult.isUnique() to check if it is unique or not). If you don't query the field, it'd impossible for clone() to duplicate that value.
    – Derek F
    Commented Mar 25, 2017 at 2:54
  • Derek, Thank you so much... that's exactly I was looking for DescribeFieldResult.isUnique() it fixed my problem, please post this as answer.
    – Nick
    Commented Mar 25, 2017 at 3:15

1 Answer 1

2

Yep, that there is one unhelpful error message. It only provides two useful pieces of information:

  1. It's a System.DmlException, so we know that it's not due to a validation rule, or dividing by 0, or something like that
  2. There is a duplicate value somewhere

The blog post that you linked got it right, this is the telltale sign that you have one or more fields that are marked as Unique on the SObject that you're trying to insert, and the SObject that you're trying to insert contains a value in one or more unique fields that are already being used by some other record(s).

Using SObject.clone() also makes this a likely scenario. clone() works by taking an SObject instance, and copying all of its in-memory data to a new SObject instance.

There are two ways to get SObject data to be in-memory: setting a field value directly in apex, or querying for the field. Based on the code that you've provided, it looks like you're getting these unique fields via your dynamic query.

Null values in unique fields seem to not cause issues, so the solution to your problem is to not query unique fields.

You haven't provided the code for buildSOQL(), but working with generic SObjects makes me think that you're probably generating your query string by looping over all of the fields for a given SObject (from Schema.DescribeSObjectResult.fields)

From there, you can check to see if a field is unique by looking at the Describe information for the individual fields and calling DescribeFieldResult.isUnique() (Documentation on DescribeFieldResult)

An example of this would be

// Not sure if I have the syntax 100% correct, and I'm unable to check if this compiles
//    at this time. 
//  Feel free to edit this example to correct the syntax, if necessary
for(Schema.SObjectField field :SObjectType.Account.getDescribe().fields){
    if(!field.getDescribe().isUnique()){
        // add this field to your query, or to a List<String> that you can then call
        //   String.join(myList, ', ') on to generate the fields to put into your 
        //   query's WHERE clause
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .