+ Start a Discussion
TC AdminTC Admin 

Apex Code Created new record but doe not take user to that new record

Hi All, Sorry I'm quit new to Apex coding and I'm sure the answer is staring my in the face.
 have an Apex Class that clones an opportunity with items. When the user clicks "Clone" the VisualForece page button action takes them to an edit screen which is perfect. However once the users Saves the new record it takes them to a blank screen, see below:
New record saved

How can I change my code so that once the users presses "Save" they are taken to the new record created?

I have been working with code from the below post:

Apex Class
public class OpportunityCloneWithItemsController {

     //added an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
     // add the instance for the variables being passed by id on the url
    private opportunity OP {get;set;}
    // set the id of the record that is created -- ONLY USED BY THE TEST CLASS
    public ID newRecordId {get;set;}

    // initialize the controller
    public OpportunityCloneWithItemsController(ApexPages.StandardController controller) {

        //initialize the standard controller
        this.controller = controller;
        // load the current record
        OP = (Opportunity)controller.getRecord();


    // method called from the VF's action attribute to clone the OP
    public PageReference cloneWithItems() {

         // setup the save point for rollback
         Savepoint sp = Database.setSavepoint();
         Opportunity newOP;

         try {

              //copy the Opportunity - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             OP = [select Id,
                    from Opportunity where id = :OP.id];
             newOP = OP.clone(false);
             newOP.Date_sent_to_warehouse__c = NULL;
             newOP.Sage_status__c ='Not Ready';
             insert newOP;

             // set the id of the new op created for testing
               newRecordId = newOP.id;

             // copy over the line items - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             List<OpportunityLineItem> items = new List<OpportunityLineItem>();
             for (OpportunityLineItem OPL : [Select OL.Id, OL.UnitPrice, OL.Quantity, OL.opportunityid, OL.pricebookentryid From OpportunityLineItem OL where Opportunityid = :OP.id]) {
                  OpportunityLineItem newOPL = OPL.clone(false);
                  newOPL.OpportunityId = newOP.id;
             insert items;

         } catch (Exception e){
             // roll everything back in case of error
            return null;

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


VisualForce Page
<apex:page standardController="Opportunity" extensions="OpportunityCloneWithItemsController" action="{!cloneWithItems}"> <apex:pageMessages /> </apex:page> 
Vivek S 42Vivek S 42
Seems like you are trying it in the Lightning Environment where url Redirect won't work. Is it working for you in classic. 
TC AdminTC Admin
Hi Vivek, I have just tested in Classic and yes it works.
Any ideas on how I can get this code to work in Lightening?