Duplicate entity

Hi to all,
which is the correct way to duplicate an entity?
i didn’t find anything similar to “duplicate record”.
thanks.
Davide

I suppose:

entity.toObject()
ds.DataClass.new()
entity.fromObject()

i have already tried but you have also to remember to clean the primary key.
in my case usual is a long int auto increment otherwise you simple rewrite the existing entity.

Hi,

Yes,you have to put the primary key to Null.

Here is a sample of generic code to duplicate entity $e to entity $e2 (with last V17R5 features):

$e2:=$e.getDataClass().new()
$e2.fromObject($e.toObject())
$e2[$e.getDataClass().getInfo().primaryKey]:=Null
$status:=$e2.save()

ok thanks,
as a workaround is ok… but i think is strange not to have a command as in the past the do this in one shot…
we don’t use in production R version, so at the moment i cannot use getInfo () command in order to have the primary key.
i’ll ask for in in Features requests i think is useful in general to duplicate a record. without convert, reconvert and manipolate .

Davide

I thought that entity.clone() was the way to duplicate a record. Am I misunderstanding this function?

Hi,

The entity.clone() method returns a new reference to the same entity.

As said in the documentation:
–> https://doc.4d.com/4Dv17R5/4D/17-R5/entityclone.305-4128710.en.html

The entity.clone( ) method creates in memory a new entity referencing the same record as the original entity.

Hi Marie-Sophie,

Ah, I see. I misread it. Probably because clone seemed the same as duplicate.

I’m curious, is there a difference between these:

$entity2:=$entity1.clone()
$entity2:=$entity1

From what I can see they do the same thing.

Thanks.

imagine, you made changes on $entity1 and now you want to get the old values (like the Old command).

Then you can call
$old:=$entity1.clone()
$old.reload()

Then you have two entities with different content but pointing to the same record.

Oh! I see. That makes sense.

Thanks, Christian.