+ Start a Discussion
cedgcedg 

custom clone : ID creation only when saving

Hi,

for my project , i need to clone some records but with the possibility for the user to change some fields (reset the status, ...). So i don't use the standard Clone button.

In my Apex class, I use the clone() method and then insert :

 

 

request = [select Id, Circuit__c from MyObject where id = :request.id];
clonedRequest = request.clone(false);
insert clonedRequest;
return new PageReference('/'+clonedRequest.id+'/e?retURL=%2F'+clonedRequest.id);

 

The problem i have is in this case, this code has created a new record ID, even if the user cancels it (due to the insert).
Is it possible not to insert the new record, but to see a temporary version which is really created only when clicking on the Save button (as the standard Clone works) ?
Thanks for your help
Ced

 

Ritesh AswaneyRitesh Aswaney

Sounds like you're looking for Savepoints.

 

Before you insert the cloned record, set a SavePoint :

 

Savepoint sp = Database.setSavepoint();

 

If you can capture the cancel event (should be able to in the cancel controller method), then you just issue a rollback to restore the DB to the state prior to the insert :

 

Database.rollback(sp);

 

Here's a nifty lil post explaining how SavePoints work

http://th3silverlining.com/2009/06/15/salesforce-savepoints/

kritinkritin

Hi On buttom click just redirect a URL to

 

https://ap1.salesforce.com/00690000003M69U/e?clone=1&retURL=%2F00690000003M69U&opp10=Test

 

here OOP10 is the Field id of "Next Step" field on Oppty..once this oppty page will load it bydefault value of "Next Step" will be Test.

 

Just Read current id and make URL string like

 

id=Opportunity.Id;

string NextStep="Test";

string url=id+"/e?clone=1"+"&retURL=" + id + "opp10=" +NextStep;

 

pageReference pref=new pageReference(url);

 

like above custom url you can load default values on the cloned records...

 

 

cedgcedg

Hi,

I just tested this way but it doesn't work. The values are not loaded (Text, Picklist, ...), it's the same values than the original record.

Moreover, the field ID is changing between sandbox and production environment, right ?

cedgcedg

 

it's not clear for me how to capture the cancel event and when.
I've my VF page that overwrite the New/Edit of my object, so i've the Save and Cancel button in this page.

 

<apex:commandButton action="{!save}" value="Save"/>
<apex:commandButton action="{!cancel}" value="Cancel"/>

I've my constructor where i get the connector and initialize some dynamic dropdowns

 

   public SG_RequestExtension(Apexpages.Standardcontroller con) {
            request = (SG_Request__c)con.getRecord();
            ...
    }

and the method I call with a custom button to create the clone

 

	public PageReference createClone(){
    
        SG_Request__c clonedRequest = new SG_Request__c();
        Savepoint sp = Database.setSavepoint();
        
        try {
        
	    request = [select Id, Circuit__c from SG_Request__c where id = :request.id];
	    clonedRequest = request.clone(false);
	    insert clonedRequest;
        
        } catch (Exception e){
            Database.rollback(sp);
            ApexPages.addMessages(e);
            return null;
        }
	return new PageReference('/'+clonedRequest.id+'/e?retURL=%2F'+clonedRequest.id);
    }

 

How can i capture it in the cancel method, and how to make the difference of the cancel of a normal Edit (not clone) ?

 

Thanks for your help

 

Ritesh AswaneyRitesh Aswaney

Set the savepoint before inserting into salesforce - 

Savepoint sp = Database.setSavepoint();

 

To intercept the Cancel event, you just need to override the default Cancel method in the StandardController, by implementing it in your Controller Extension.


Define a method Cancel

 

public PageReference cancel()

{

Database.rollback(sp);

super.cancel();

}

cedgcedg

In this case, when calling the Cancel button on the Edit page of my object, I get an error as "sp" is not defined.

 

I guess it's a dummy question, but i'm lost right now  ...

 

Ced

Ritesh AswaneyRitesh Aswaney

That is prolly coz your variable is a local variable defined within the clone method.

Declare sp as an instance variable of the controller class.

 

so

 

public class ControllerExtension

{

 

Savepoint sp;

 

public...... clone()

{

......

sp = Database.setSavePoint....

 

}

kritinkritin

Yes,Field id will be different but you can overcome this issue using Custom Settings.

 

cedgcedg

Finally,

i just make a redirect to my page with &clone=1 in the querystring, and in the constructor i catch the parameter and reset some fields.

bfeather19bfeather19

can you paste what your end solution looked like I am trying to get the same functionality in place right now