+ Start a Discussion
PatcsPatcs 

clone button override using Apex class

Hi


I want to override the clone functionality in Apex code. My scenario is that I want my opportunity to be cloned with the close date of cloned oppty+365 days, and the cloned oppty should not save unless and until I hit the save, it should not save.


I want this code to be deliver by tomorrow. please help me....

Thanks in Advance!

pankaj.raijadepankaj.raijade

hide the standard clone button 

 

create new button lable it 'Clone', redirect user to VF page. 

On this VF page you can put your other functionalty you need.

 

Regards,

Pankaj Raijade.

PatcsPatcs

Hi

 

I wrote a VF page and I override with that standard clone button. In that I don't have any problem, my problem starts in VF page 

 

below is the code,

 

Opportunity opp2= [SELECT Id, Name, StageName, CloseDate, Status__c FROM Opportunity where id=:Opp.Id];     

Opportunity newOpp= opp2.clone(false,true);     

newOpp.StageName='xxxxxxx';   

newOpp.CloseDate=opp2.CloseDate+365;   

insert newOpp;

PageReference OpptyPage = new PageReference('/'+newOpp.Id +'/e');

OpptyPage.setRedirect(true);     

return OpptyPage;

 

In the above code it is cloning the opportunity, but when I cancelled cloned  oppty, it is saving. I know it is because of inserting the record, I have to use some savepoint, but I don't know how to get the standard cancel function to use savepoint and rollback. 

 

Please help on this it is very important and urgent for me....

 

Thanks in Advance

LakshmanLakshman

Patcs,

Do it as follows:

 

In the constructor of ClonePageController write like this:

Public Opportunity newOpp{get;set;}

public constructor(){

Opportunity opp2= [SELECT Id, Name, StageName, CloseDate, Status__c FROM Opportunity where id=:Opp.Id];     

newOpp= new opportunity();

//set all the fields from all the above fields or use the clone function which you were using earlier 

newOpp.StageName=opp2.StageName;  

newOpp.CloseDate=opp2.CloseDate+365;  

//and so on

}

And then on save button do this action

public PageReference save()

{

 insert newOpp;

PageReference OpptyPage = new PageReference('/'+newOpp.Id);

OpptyPage.setRedirect(true);     

return OpptyPage;

}



and now you have to use newOpp in the visualforce page to display all the desired fields and provide Save, Cancel etc buttons in it with the desired actions present in controller.

Let me know if you are facing any issues in it.

 

Regards,

Lakshman