You need to sign in to do that
Don't have an account?

cloning Master+detail records
I would appreciate some pointers on this.
I am trying to clone a master record (say M__c) that has some children detail records (MC__c)
I queried the master records and created a deep copy by calling
M__c.clone(false,true); //do not preserve Id & do a deep clone
This does not seem to null out the children/detail record ids.
When I insert the cloned master record, it creates a copy alright but there are no details.
I tried cloning the individual MC__c records and adding them to the cloned parent's child relationship array.
That did not help.
Could some one point me to a code snippet or documentation for the correct way to do this?
Thanks
Helo, im having problems with cloning to. When doing a clone for a detail object the values entered for the new object are not saved.
I needed to to the following to get the Id cleared out but now the information entered for the new object is not being saved, it stays with the information of the original record.
Any ideas?, here's a snippet of my code.
private final object__c theobject;public Object_Ext(ApexPages.StandardController controller){ if(System.currentPageReference().getParameters().get('clone') == null){ theobject = (object__c )controller.getRecord();}else{ theobject = ((object__c )controller.getRecord()).clone(false,false);}}
I think the problem is that you're trying to do an insert on all the child objects/details by relating them the master. If you're inserting the master object, you'll need to insert all the other related objects separately.
But you'll need to insert the master object first before inserting the related objects, simply because you need the ID generated by the master object during the insert to set the foreign key on your related objects properly.
@jonathan rico:
Your clone call is incorrect.
clone( Boolean opt_Preserve_Id, Boolean opt_isDeepClone )
you called clone( false, false ), which means, do not copy the ID of the original, and create a REFERENCE to the original sObject.
you should be calling clone ( false, true ), which means, do not copy the ID, but make a copy of all the details into a seperate object.
private final object__c theobject;publicObject_Ext(ApexPages.StandardController controller){ if(System.currentPageReference().getParameters().get('clone') == null){ theobject = (object__c )controller.getRecord();}else{ theobject = ((object__c )controller.getRecord()).clone(false,false); theobject.a_field__c = 'avalue';}}
But the new field is not filled up with the value that I assigned... any ideas?
Thanks in advance.
@jonathan rico
Not sure if you heard me the first time, but your clone call is still the same
referring to this line
theobject = ((object__c )controller.getRecord()).clone(false,false);
which should be
theobject = ((object__c )controller.getRecord()).clone(false,true);
note the bold, and my explanation in my previous post.
Oops.. sorry I just copied the code that I had in my previous post. I did changed it to false,true and i'm still getting the same behavior.
:smileyindifferent: sorry for that.
To be honest, I haven't worked with VisualForce APEX yet, so some of that code looks a little foreign to me.
I don't know if there is enough information for me to answer your question.
Maybe System.currentPageReference().getParameters().get('clone') always equals null?
Did you return theobject back to the method calling this one?
Hello again,
I made some tests concerning the clone parameter and it is returning a value during a clone operation, otherwise it return nulls.
However, it seems like theobject is not being returned to the standardController as the current Record(this code snippet belongs to an extension for the Standard Controller).So all of the changes that I made to the record seem to be saved somewhere else and not in the record that I just cloned..
Thank you very much for your help, i really appreciate it.. I'm pretty sure I need to update the controller's record reference so that it now points to the cloned record but I can't find a way to do this.
As a last ditch attempt to help... do you know at what point your theobject gets saved to Salesforce? I'm not sure where you perform your save or your insert statement.
insert theobject;
I'm not sure how this page referencing business works, but if you require theobject's ID, then you should be able to retrieve theobject's new ID after the insertion call. After that, you should point your page reference to the new object's ID.
Hopefully whatever I am saying is making sense since I'm not even sure of the answer when it comes to VisualForce.
The only way I was able to save new record was overriding save method and upserting:
public PageReference save() { Database.UpsertResult res = Database.upsert(campaign); return (new ApexPages.StandardController(campaign)).view(); }