You need to sign in to do that
Don't have an account?
KelseyB
redirect after save or cancel button
I created a Visualforce page and Apex controller to clone a record. What I need now is to have the Save and Cancel button redirect back to the originating record. I have no experience with Apex or VF and was able to create this with the help of this developer forum. Can anyone help me with what code to add and where to put it, to get the Save Button and Cancel Button to redirect back to the original record?
Here's the vf page code:
<apex:page standardController="OpportunityLineItem" extensions="customcloneOpportunityLineItem">
<apex:form >
<apex:pageBlock title="Opportunity Product details">
<apex:pageBlockSection title="Opportunity Prodcut Edit" columns="1">
<apex:inputfield value="{!OpportunityLineItem.OpportunityId}"/>
<apex:inputField value="{!OpportunityLineItem.Product2Id}" />
<apex:inputField value="{!OpportunityLineItem.Quantity}" />
<apex:inputField value="{!OpportunityLineItem.Unit_List_Price__c}" />
<apex:inputfield value="{!OpportunityLineItem.Unit_Sales_Price__c}"/>
<apex:inputfield value="{!OpportunityLineItem.Unit_Cost__c}"/>
<apex:inputfield value="{!OpportunityLineItem.ProductCode}"/>
<apex:inputfield value="{!OpportunityLineItem.Product_Family__c}"/>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton action="{!cloneopportunitylineitem}" value="Save" />
<apex:commandButton action="{!cloneopportunitylineitem}" value="Cancel" />
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Here's the Apex Class:
public class customcloneOpportunityLineItem
{private opportunitylineitem l;
public customcloneOpportunityLineItem(apexpages.standardcontroller std){
this.l = (opportunitylineitem)std.getrecord();
}
public void cloneOpportunityLineItem(){
opportunitylineitem l1 = new OpportunityLineItem();
l1 =l.clone();
insert l1;
}
}
Here's the vf page code:
<apex:page standardController="OpportunityLineItem" extensions="customcloneOpportunityLineItem">
<apex:form >
<apex:pageBlock title="Opportunity Product details">
<apex:pageBlockSection title="Opportunity Prodcut Edit" columns="1">
<apex:inputfield value="{!OpportunityLineItem.OpportunityId}"/>
<apex:inputField value="{!OpportunityLineItem.Product2Id}" />
<apex:inputField value="{!OpportunityLineItem.Quantity}" />
<apex:inputField value="{!OpportunityLineItem.Unit_List_Price__c}" />
<apex:inputfield value="{!OpportunityLineItem.Unit_Sales_Price__c}"/>
<apex:inputfield value="{!OpportunityLineItem.Unit_Cost__c}"/>
<apex:inputfield value="{!OpportunityLineItem.ProductCode}"/>
<apex:inputfield value="{!OpportunityLineItem.Product_Family__c}"/>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton action="{!cloneopportunitylineitem}" value="Save" />
<apex:commandButton action="{!cloneopportunitylineitem}" value="Cancel" />
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Here's the Apex Class:
public class customcloneOpportunityLineItem
{private opportunitylineitem l;
public customcloneOpportunityLineItem(apexpages.standardcontroller std){
this.l = (opportunitylineitem)std.getrecord();
}
public void cloneOpportunityLineItem(){
opportunitylineitem l1 = new OpportunityLineItem();
l1 =l.clone();
insert l1;
}
}
All Answers
In the controller for cancel, it is creating a clone. Replace the Controler code with this
Regards,
Tyler
Yes all Apex Classes, controllers, and triggers need to have a test class to ensure the code won't break in your org.
Here is the documentation for testing controllers: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm
You will need to create a separate class similar to what the documentation shows to test against the controller.
I've constructed a test class for you that gave me 100% coverage so it should work for you, will only need to change the visualforce page name