You need to sign in to do that
Don't have an account?
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:

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:
https://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/
Apex Class
VisualForce Page
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:
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:
https://blog.jeffdouglas.com/2009/11/19/apex-deep-clone-controller/
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,
Accountid,
CloseDate,
Promised_Delivery_Date__c,
Requested_Delivery_Date__c,
Description,
DocType__C,
CurrencyIsoCode,
Name,
Ownerid,
RecordTypeid,
Pricebook2id,
Campaignid,
Probability,
StageName,
warehouse__c,
Delivery_Address_if_diff_from_main_add__c,
Invoice_type__c,
Type,
A6_Combined_Discount__c,
A6_Total_Trade_Disc__c,
A6_Contributions_and_Provisions__c,
O1_2_return_reason__c,
Sale_Or_Return_Record__c
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;
items.add(newOPL);
}
insert items;
} catch (Exception e){
// roll everything back in case of error
Database.rollback(sp);
ApexPages.addMessages(e);
return null;
}
return new PageReference('/'+newOP.id+'/e?retURL=%2F'+newOP.id);
}
}
//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,
Accountid,
CloseDate,
Promised_Delivery_Date__c,
Requested_Delivery_Date__c,
Description,
DocType__C,
CurrencyIsoCode,
Name,
Ownerid,
RecordTypeid,
Pricebook2id,
Campaignid,
Probability,
StageName,
warehouse__c,
Delivery_Address_if_diff_from_main_add__c,
Invoice_type__c,
Type,
A6_Combined_Discount__c,
A6_Total_Trade_Disc__c,
A6_Contributions_and_Provisions__c,
O1_2_return_reason__c,
Sale_Or_Return_Record__c
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;
items.add(newOPL);
}
insert items;
} catch (Exception e){
// roll everything back in case of error
Database.rollback(sp);
ApexPages.addMessages(e);
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>
Any ideas on how I can get this code to work in Lightening?
Regards
Sarah