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: []
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 thatbuildSOQL()
is creating (and if you're building the string using the fields from theSObjectDescribeResult
, you can useDescribeFieldResult.isUnique()
to check if it is unique or not). If you don't query the field, it'd impossible forclone()
to duplicate that value.DescribeFieldResult.isUnique()
it fixed my problem, please post this as answer.