1

UPDATE: AFter I have tried with the @Adrian approach I'm getting the following error now:

|EXCEPTION_THROWN|[65]|System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: unknown duplicates value on record with id: a1cV00000005bDm: [] |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: []

Here is my code that executing my soql query

SObject existingRecord = Database.query('Select name, field2, field3....from where id='xyz'); 
SObject newRecord = sObjectId.getSObjectType().newSObject(sObjectId);

newRecord = existingRecord.clone(false, false, false, false);
insert newRecord;

UPDATE END

I'm in the process of creating a customize cloning so I have created custom button which will invoke from Standard page layout that will call the apex class... I can't use standard cloning functionality because I have other business rules that user will select (through config) which fields to be clone etc...

I'm stuck on how to populate the values from old record to new one.

the whole purpose of this is to make generic so no hard-coding any object name or fields or anything.

I have asked the question here you can have a look to get the context of my question or I can copy and past it here if needed.

I'm having issue with the code block:

INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id

Understand that you can't have Id when you are inserting a new record and I get it but given this scenario what will be the case?

String objPrefix = sObjectId.getSobjectType().getDescribe().getKeyPrefix();
SObject existRecord = Database.query(buildSOQL());
SObject newRecord = sObjectId.getSObjectType().newSObject(sObjectId);
newRecord = existRecord;

insert newRecord; //error

Any idea how would I (better) approach.

1 Answer 1

3

You've kind of answered your own question in the title. You need to use the SObject.clone method. Note that you can never query for and subsequently insert a record, because it will already have an Id value.

Incorrect

newRecord = existingRecord;
insert newRecord;

Correct

newRecord = existingRecord.clone(/*preserveId*/ false);
insert newRecord;

There are a few other parameters you can set, all of which are optional:

Signature
public sObject clone(Boolean preserveId, Boolean isDeepClone, Boolean preserveReadonlyTimestamps, Boolean preserveAutonumber)

You can read up on SObject methods in more detail here.

3
  • Thanks will take a look at the SObject but one quick question will this SObject.clone will get cloned only the fields that pulled or will it be entire object fields? as I said I have a requirement that I need to pull certain fields not entire from the object. thanks a lot
    – Nick
    Commented Mar 23, 2017 at 21:27
  • @NickKahn It will only clone fields in memory, I believe. In fact, it won't even copy all fields in memory without specifying additional parameters (preserveReadonlyTimestamps, preserveAutonumber).
    – Adrian Larson
    Commented Mar 23, 2017 at 21:29
  • Let us continue this discussion in chat.
    – Adrian Larson
    Commented Mar 24, 2017 at 3:09

You must log in to answer this question.

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