-1

I am new in salesforce development.I am started learning visualforce. Please help to resolve this issue.

Vf page :

<apex:page controller="UpdateOpportunity">
    <apex:form>
        <apex:pageBlock title="Opportunity_list">
            
                <apex:pageBlockTable value="{!OpptyDetail}" var = "opporty">
                <apex:inputField value = "{!opporty.Name}"/>
                  <apex:inputField value = "{!opporty.StageName}"/>    
                
            </apex:pageBlockTable>
            
            <apex:pageBlockButtons >
                <apex:commandButton action="{!Save}" value="Save"/>
                <apex:commandButton action="{!cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
                </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class:

public class UpdateOpportunity {
    public List<Opportunity> getOpptyDetail(){
        List<Opportunity> opptylist =[SELECT Id,Name,StageName FROM opportunity WHERE StageName = 'Prospecting'];
        return opptylist;
        
    }
}

It's thrown error:Unknown method UpdateOpportunity.Save()

1
  • you need to add a PageReference save() {..do the DML} method in the custom controller as Hengky stated
    – cropredy
    Jun 26, 2020 at 5:48

1 Answer 1

3

I would recommend to go through this trailhead module to get started with Visualforce.

To answer your question, your Visualforce page is using Custom Controller. So in order for your code to work, you must define you own save() method in the controller class.

Save and Cancel are standard controller methods. Unless you use standardController, it won't work.


Edit: Some changes needed in your custom controller class. You need to define a variable to hold the list of Opportunity records for update later, and implement a save() method to call the DML update.

public class UpdateOpportunity {
    List<Opportunity> opportunities = new List<Opportunity>();

    public UpdateOpportunity() {
        opportunities = [SELECT Id,Name,StageName FROM opportunity WHERE StageName = 'Prospecting'];
    }

    public List<Opportunity> getOpptyDetail(){
        return opportunities;
    }

    public PageReference save() {
        update(opportunities);
        return null;
    }
}

The example above is actually very similar to Salesforce Documentation.

5
  • I gone through the module but didn't get clear idea to create save() method for custom controller .Can you please explain me how to do this.
    – Jane
    Jun 26, 2020 at 5:23
  • Hi Hengky, Thankyou for your help. After I tried this got one error. Error: Could not resolve the entity from <apex:inputField> value binding '{!opporty.Name}'. <apex:inputField> can only be used with SObjects, or objects that are Visualforce field component resolvable.
    – Jane
    Jun 26, 2020 at 6:19
  • I saw you have accepted the answer. Did you manage to solve the issue above? Jun 26, 2020 at 7:45
  • No Hengky.It throws same error
    – Jane
    Jun 26, 2020 at 7:53
  • That's weird because I tried with the same code, it worked for me. Btw, in your vfpage, since you use apex:pageBlockTable you need to put the inputfield inside an apex:column tag. Jun 27, 2020 at 2:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .