function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
pmozz01pmozz01 

Clone with Items - cannot rollback to savepoint

I have previously posted a request for help, received a reply, but am still unable to get this code to work.  What is happening is as soon as the opportunity custom clone button is clicked the new opportunity clone is created and, if someone cancels, the clone remains and does not rollback.  As I click the custom clone button to clone an opportunity, I see the new opportunity created in the sidebar "Recent Items" list. 

 

I would greatly appreciate some help with this.  I have something wrong with my savepoint that I cannot get it to rollback.

 

Thanks!

 

public class OppCloneController {  
    private ApexPages.StandardController controller {get; set;}  
//DECLARE SAVEPOINT AS INSTANCE VARIABLE   
    public Savepoint sp;
    public Opportunity opp {get;set;} 
    public List<OpportunityLineItem> items = new List<OpportunityLineItem>();
    Schema.DescribeSObjectResult R = Opportunity.SObjectType.getDescribe();
    List<Schema.RecordTypeInfo> RT = R.getRecordTypeInfos();
    
    // set the id of the record that is created -- ONLY USED BY THE TEST CLASS  
    public ID newRecordId {get;set;}  

//OVERRIDE THE DEFAULT CANCEL METHOD
public PageReference cancel()
{
 
Database.rollback(sp);

return null;
 
}
     // initialize the controller  
    public OppCloneController(ApexPages.StandardController controller) {  
     //initialize the standard controller  
      this.controller = controller;  
    // load the current record  
         opp = (Opportunity)controller.getRecord(); 
            // setup the save point for rollback  
         sp = Database.setSavepoint(); 
     }  
    
        
    // method called from the VF's action attribute to clone the opp 
    public PageReference cloneWithItems() {  
 
        Opportunity newOP;  
         try {  
          //copy the opportunity - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE  
             opp = [select Id, Name, Account.Name, AccountID, StageName, CloseDate, OwnerID, Type_of_Job__c, 
             Invoice_Requirements__c, Customer_Account__c, Contact_Name__c, Customer_E_mail__c, RecordTypeID,
             Phone__c, fax__c, Pricebook2Id
             from Opportunity where id = :opp.id]; 
             
 
             newOP = opp.clone(false);  
           //check to see if the opportunity name contains DTV or DirecTV, if so apply the appropriate record type,
           //otherwise use standard record type
                          
                    
   
                      // VALUE MAPPING 
            newOp.Name = opp.Name;
            newOp.Account.Name = opp.Account.Name;     
            newOp.AccountId = opp.AccountId;
            newOp.CloseDate = opp.CloseDate;
            newOp.StageName = 'New Work Order';
            newOp.OwnerID = opp.OwnerID;
            newOp.Type_of_Job__c = opp.Type_of_Job__c;
            newOp.Invoice_Requirements__c = opp.Invoice_Requirements__c;
            newOp.Customer_Account__c = opp.Customer_Account__c;
             newOp.RecordTypeId = opp.RecordTypeId;      
            newOp.Contact_Name__c = opp.Contact_Name__c;
            newOp.Customer_E_mail__c = opp.Customer_E_mail__c;
            newOp.Phone__c = opp.Phone__c;
            newOp.fax__c = opp.fax__c;
            newOp.Pricebook2Id = opp.Pricebook2Id;
            
             If (newOp.Name.contains('DirecTV'))
            newOp.RecordTypeId = [Select Id From RecordType RT Where RT.Name='DTV New Standard' limit 1].Id;
  
            else if (newOp.Name.contains('DTV'))
            newOp.RecordTypeId = [Select Id From RecordType RT Where RT.Name='DTV New Standard' limit 1].Id;
             else 
                newOp.RecordTypeID = [Select ID From RecordType r Where r.Name='New Standard' limit 1].ID;
                
                   insert newOP;  
             // set the id of the new opp created for testing  
              newRecordId = newOP.id;  
 
  // copy over the line items - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE 
               
               for(OpportunityLineItem oli : [Select ID, OpportunityID,  
               UnitPrice, Quantity, PricebookEntryID
               
               from OpportunityLineItem oliList where OpportunityID = :opp.id]
               )
           
               {
               OpportunityLineItem newOli = new OpportunityLineItem();
                   newOli.PricebookEntryID = oli.PricebookEntryID;
                   newOli.Quantity = oli.Quantity;
                   newOli.UnitPrice = oli.UnitPrice;
                   newOlI.OpportunityID=newOP.id;
              
                   items.add(newOli);
                   } //end loop
                   
                   insert items;
           
         } catch (Exception e){  
              // roll everything back in case of error  
             Database.rollback(sp); 
             //delete newOp; 
             ApexPages.addMessages(e);
             opp = opp.clone(false);


  
             return null;  
         }  
        return new PageReference('/'+newOp.id+'/e?retURL=%2F'+newOp.id);  
     }  
    
 }

 

 

gm_sfdc_powerdegm_sfdc_powerde

Savepoints are valid only during the request lifecyle.  When cloneWithItems is successfully completed, the transaction is committed automatically and your save point is not valid anymore.   For the "Cancel" button to do what you want, you will have to explicitly delete the newly created opportunity in cancel() method.

 

steve456steve456

Did u get the solution for it.If so could you please post it.I am having the same issue