You need to sign in to do that
Don't have an account?
jprosser
Clone question
Hi Folks,
I would like to make a copy of an opportunity and all its line-items when someone clicks on a custom button (via an S-Control calling a web-service using Apex)
I figure I will pass the Opportunity ID as a parameter in the webservice call, but now I'm having a problem with the cloning part.
If I initialize an Opportunity object "opp" with an SOQL select statement, and then call "opp.clone(true), am I only cloning the object that I initialized? I want to make a copy of the entire thing. I'm missing a fundamental concept I think.
here's the Apex routine.
WebService static String duplicateOpp(String arg) {
String test = '005E0000003Relo';
Opportunity opp = [ select Id,Name from opportunity where id=:test];
Opportunity newOpp = opp.clone( true);
update newOpp;
return 'Copied ' + newOpp.Name + ' of type ' + newOpp.Type;
}
I would like to make a copy of an opportunity and all its line-items when someone clicks on a custom button (via an S-Control calling a web-service using Apex)
I figure I will pass the Opportunity ID as a parameter in the webservice call, but now I'm having a problem with the cloning part.
If I initialize an Opportunity object "opp" with an SOQL select statement, and then call "opp.clone(true), am I only cloning the object that I initialized? I want to make a copy of the entire thing. I'm missing a fundamental concept I think.
here's the Apex routine.
WebService static String duplicateOpp(String arg) {
String test = '005E0000003Relo';
Opportunity opp = [ select Id,Name from opportunity where id=:test];
Opportunity newOpp = opp.clone( true);
update newOpp;
return 'Copied ' + newOpp.Name + ' of type ' + newOpp.Type;
}
When you are cloning a record, it's an in-memory reference; clearing the ID field lets you insert it as a new opportunity.
All Answers
The optional opt_preserve_id argument determines whether the ID of the original object is preserved or cleared in the duplicate.
If I execute this:
I don't get a copy, presumably because I've told clone to preserve the ID.
If I call clone like this:
Opportunity newOpp = opp.clone(false, true);
then the Id is cleared and the update throws an exception.
How do I generate a new Id for the clone? Maybe this is all I need to do?
Thanks,
-Joe
The exception you get must be because of something else.
I get the following:
it looks like the id of the clone is null, and this is what is causing the update to fail.
-Joe
When you are cloning a record, it's an in-memory reference; clearing the ID field lets you insert it as a new opportunity.
I tried this, not all fields are getting copied. Any idea????
You must select all the fields in the SOQL statement you want to clone.
Micwa is quite right, all of the fields have to be present in the object in your Apex code, either through using a SOQL query or populating the details manually.
Since coming accross this cloning details issue, I have created a generic cloning method that can be used to create a copy of any object, populating all of the fields values in the process without having to retrieve the details through SOQL before hand.
This method can be found in my blog post: How to dynamically clone Salesforce objects with all fields populated.
Hope it helps anyone still looking at this issue.