Reason being, there are over a 100 columns I would like to reuse and only 4 columns will be changed and then inserted as a new record. My last resort would be to load the entity I would like to clone then manually set the new entity’s properties with loaded entity.
I have tried the following.
<!--- load entity I would like to clone -->
<cfset mainObj= EntityLoad("myBean",{fkOtherId = 2},true)>
<!--- create new entity to save -->
<cfset newObj = EntityNew( "myBean" )>
<!--- clone entity -->
<cfset newObj = EntityMerge(mainObj)>
<cfset newObj.setFirstName(‘John’)>
<cfset newObj.setLastName(‘Smith’)>
<cfset entitySave(newObj)>
Solved: use
<cfset newObj = duplicate(mainObj)>
<cfset newObj.setId(‘’)>
<cfset newObj.setFirstName(‘John’)>
<cfset newObj.setLastName(‘Smith’)>
<cfset entitySave(newObj, true)>
entitySave has a second argument called forceInsert. That should work here.