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
mramaprasad.ax1826mramaprasad.ax1826 

How to get the Save and cancel button working on custom visual force page ?

I want to overwrite view button of the opportunity products so I can have specific fields based on the product type.

Here is the visual page code:
<apex:page standardController="OpportunityLineItem" extensions="overrideCon" sidebar="true" showHeader="true">
<apex:form >
<input value=" Save " class="btn" title="Save" name="save" type="submit"/>
<input value="Cancel" class="btn" title="Cancel" name="cancel" type="submit"/>
<apex:pageBlock id="xxxpb1">

Opportunity:&nbsp;&nbsp;{!opp.name}<br></br>
Product:&nbsp;&nbsp;{!pro.name}
Quantity: <apex:inputField value="{!OpportunityLineItem.Quantity}"/>
<apex:pageBlockSection id="xxxpbs2" rendered="{!OpportunityLineItem.Product2Id=='01t17000000BmMdAAK'}">
       product 1 fields
        </apex:pageBlockSection>
    
    <apex:pageBlockSection id="xxxpbs3" rendered="{!OpportunityLineItem.Product2Id=='01t17000000BmMTAA0'}">
        product 2 fields
    </apex:pageBlockSection>
    <apex:pageBlockSection id="xxxpbs4" rendered="{!OpportunityLineItem.Product2Id=='01t17000000BX9SAAW'}">
        product 3 fields
    </apex:pageBlockSection>   
</apex:pageBlock>
</apex:form>  
</apex:page>

Here is the apex code:

public class overrideCon {
private final ID oliId;
    public overrideCon (ApexPages.StandardController stdController) {
        this.oliId = (String)stdController.getId();
    }
    
    Opportunity opp;
    OpportunityLineItem oli;
    Product2 pro;

    public Product2 getPro(){
        if (pro == null) {
            pro = [select Id, name from Product2 where id = :getOli().Product2Id];
        }
    return pro;
    }
    public Opportunity getOpp(){
         if(opp == null){
              opp = [select Id, name, amount, Pricebook2.Name from Opportunity where id = :getOli().Opportunityid];
         }
         return opp;
     } 
     
     public OpportunityLineItem getOli() {
         if(oli == null){
             oli = [select Id, Product2Id, OpportunityID from OpportunityLineItem where ID = :oliID];
         }
         return oli;
     }   
     
     public PageReference save() {
        update oli;
        return null;
    }
    
    public PageReference cancel() {
    return null;
    }

   }


How do I get the save and cancel button to work so changes are saved.
BalajiRanganathanBalajiRanganathan
If you want to reuse the standard controller save and cancel, then dont have those methods(save and cancel) in your controller extension. remove them. thats all you need to do

 
mramaprasad.ax1826mramaprasad.ax1826
Since I don't have form action, When I click on the save button nothing happens. 

So I added
<input value=" Save " class="btn"  title="Save" action="{!save}" name="save" type="submit"/>

which gives me [Error] Error: Unknown property 'OpportunityLineItemStandardController.save'

Please help!
BalajiRanganathanBalajiRanganathan
You should be using apex:commandbutton instead of <input> tag.

        <apex:commandButton value="Save" action="{!save}"/>
        <apex:commandButton value="Cancel" action="{!cancel}"/>
mramaprasad.ax1826mramaprasad.ax1826
I think it worked. Thanks for your help.
mramaprasad.ax1826mramaprasad.ax1826
After saving View is still on the same custom page I created,But  I want  the control to go back to the opportunity page.  Any ideas how to do it?
mramaprasad.ax1826mramaprasad.ax1826
I was thinking override the save method and add redirect to the opportunity method. But not sure how to do it..