0

I have attempted to build a custom Save method for a visualforce page I created, but when I click Save, the page reloads with nothing happening. What have I done wrong? :/

Here is my controller code:

public class MannaOppController {

    ApexPages.StandardController controller;
    public static Boolean IsEditMode {get;set;}
    public Boolean thisUserMayCreate {get; set;}

    public Opportunity opp {get;set;}

    public MannaOppController(ApexPages.StandardController controller){

        this.opp = (Opportunity)controller.getRecord();
        if(opp.Id != null){
            opp = [SELECT Id, App_Opp_For__c, AccountId, OwnerId, Contact__c, StageName, Name, CreatedDate,
                   Membership_Effective_Date__c, T_C_Rcvd_Date__c, Application_Fee_Paid__c, CreatedBy.Name,
                   Appl_Rcvd_Date__c, Loss_Reason_NFI__c, CloseDate, Netsuite_EID__c, MS_Desired_Start_Date__c
                   FROM Opportunity
                   WHERE Id = :opp.Id 
                   LIMIT 1];
        }

        Schema.DescribeSObjectResult oObj = Schema.SObjectType.Opportunity;
        thisUserMayCreate = oObj.isCreateable();
        String mode = Apexpages.currentPage().getParameters().get('mode');
        Id accountId;
        if(Apexpages.currentPage().getParameters().get('accid') != ''){
            accountId = Apexpages.currentPage().getParameters().get('accid');
        } else {
            accountId = null;
        }
        Id contactId = Apexpages.currentPage().getParameters().get('conid');

        Id userId = System.UserInfo.getUserId();
        String oppFor = 'Manna';
        String appType = 'Standard';
        Datetime todayD = Date.today();
        String todayF = todayD.format('MMddYYYY');        
        String oppName = 'Manna Standard Opp '+todayF;
        String stage = 'New Prospect';
        Date close = Date.today().addDays(30);

        if(opp.Id == null){
            opp.AccountId = accountId;
            opp.Contact__c = contactId;
            opp.App_Opp_For__c = oppFor;
            opp.OwnerId = userId; 
            opp.Name = oppName;
            opp.App_Type__c = appType;
            opp.StageName = stage;
            opp.CloseDate = close;
        } 

        if(opp.Id == null || mode == 'edit'){
            IsEditMode = true;
        } else {
            IsEditMode = false;
        } 
    }

    public PageReference customEdit(){

        Id currentOpp = ApexPages.CurrentPage().getParameters().get('Id');
        PageReference pr = new PageReference('/apex/mannaopp?id='+currentOpp+'&mode=edit');
        pr.setRedirect(true);
        return pr;
    }


    public PageReference customSave(){

        try{
            upsert opp;
            PageReference pr = new PageReference('/apex/mannaopp?id='+opp.Id+'&mode=view');
            pr.setRedirect(true);
            return pr;
        }
        catch (System.DmlException ex){
            String msg = ex.getDmlMessage(0);
            String newmsg = msg.replace('&','&');
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, newmsg));
        } return null;
    }

    public PageReference customCancel(){

        Id currentOpp = ApexPages.CurrentPage().getParameters().get('Id');
        if(currentOpp==null){
            String contact = Apexpages.currentPage().getParameters().get('conid');
            if(contact != null){
                PageReference pr = new PageReference('/'+Apexpages.currentPage().getParameters().get('conid')); //redirect to Contact after Cancel event on new opp       
                pr.setRedirect(true);
                return pr;} 
            else {
                PageReference pr = new PageReference('/home/home.jsp'); //redirect to home if no Contact ID
                pr.setRedirect(true);
                return pr;}
        }else{
            PageReference pr = new PageReference('/apex/mannaopp?id='+currentOpp); //redirect to mannaopp after Cancel event on edit opp
            pr.setRedirect(true);
            return pr;
        }
    }

    public PageReference appFeeRcvd(){
        Opportunity o = (Opportunity)controller.getRecord();
        o.Application_Fee_Paid__c = true;
        o.Appl_Rcvd_Date__c = Date.today();
        o.StageName = 'App Fee Received';
        update o;

        Id currentOpp = controller.getId();
        PageReference pr = new PageReference('/apex/mannaopp?id='+currentOpp+'&mode=view');
        pr.setRedirect(true);
        return pr;
    }

}

And here is my Visualforce page code:

<apex:page extensions="MannaOppController" standardController="Opportunity" tabStyle="Manna_Opp__tab" standardStylesheets="true" applyHtmlTag="true" cache="false">
    <p/>
    <img src="https://c.cs21.content.force.com/servlet/servlet.ImageServer?id=015q00000007HJ4&oid=00Dq0000000CSTt" /> 
    <p/>
    <apex:pageMessages /> 
    <apex:form>
        <apex:pageBlock id="nocreate" rendered="{!(opp.Id == null && thisUserMayCreate = false)}" title="Cannot Create Opportunity">
            <apex:pageblockButtons location="bottom" >          
                <apex:commandButton action="{!customCancel}" value="Cancel"/>
            </apex:pageblockButtons>
            <apex:pageMessage severity="error" strength="1">You do not have access to create Opportunities. Please contact your manager or Salesforce Administrator.</apex:pageMessage>
        </apex:pageBlock>

        <apex:pageblock id="new" rendered="{!(opp.Id == null && thisUserMayCreate = true) || (opp.Id != null && IsEditMode)}" title="Manna Opportunity">
            <apex:pageblockButtons >
                <apex:commandButton action="{!customCancel}" value="Cancel"/>
                <apex:commandButton rendered="{!opp.Id != null}" action="{!customSave}" value="Save"/>
                <apex:commandButton rendered="{!opp.Id = null}" action="{!customSave}" value="Submit"/>
            </apex:pageblockButtons>
            <br />
            <apex:pageblockSection showHeader="false" collapsible="false" title="Opp" columns="2">
                <apex:inputField value="{!opp.App_Opp_For__c}" />
                <apex:inputField value="{!opp.Name}" /> 
                <apex:inputField value="{!opp.AccountId}" />
                <apex:inputField value="{!opp.OwnerId}" />  
                <apex:inputField value="{!opp.Contact__c}" />                    
                <apex:inputField value="{!opp.StageName}" />
                <apex:pageblockSectionItem helpText="If the applicant has indicated they desire to start at different or later date than next eligible start date." >
                    <apex:outputLabel value="Desired Start Date"/>
                    <apex:inputField value="{!opp.MS_Desired_Start_Date__c}" label="Desired Start Date"/>
                </apex:pageblockSectionItem>
                <apex:inputField value="{!opp.Loss_Reason_NFI__c}" />
                <apex:inputField value="{!opp.CloseDate}" />                 
                <apex:inputField value="{!opp.Appl_Rcvd_Date__c}" />   
                <apex:inputField value="{!opp.Application_Fee_Paid__c}" />
                <apex:inputField value="{!opp.T_C_Rcvd_Date__c}" />
                <apex:inputField value="{!opp.Membership_Effective_Date__c}" />
                <apex:inputField value="{!opp.NetSuite_EID__c}" />
            </apex:pageblockSection>
        </apex:pageblock>

       <apex:inlineEditSupport event="ondblClick" 
                                showOnEdit="saveButton, cancelButton" hideOnEdit="editButton, appFeeButton" />

        <apex:pageblock id="current" mode="mainDetail" rendered="{!(opp.Id != null && thisUserMayCreate = true && !IsEditMode)}" title="Opportunity Details" >
            <apex:pageblockButtons location="top" >
                <apex:commandButton action="{!customEdit}" id="editButton" value="Edit"/>
                <apex:commandButton action="{!customCancel}" id="cancelButton" style="display: none" value="Cancel"/>
                <apex:commandButton action="{!customSave}" id="saveButton" style="display: none" value="Save"/>
            </apex:pageblockButtons>
            <br />
            <apex:pageblockSection showHeader="false" collapsible="false" title="" columns="2">
                <apex:outputField value="{!opp.App_Opp_For__c}" />
                <apex:outputField value="{!opp.Name}" />
                <apex:outputField value="{!opp.AccountId}" />
                <apex:outputField value="{!opp.OwnerId}" />  
                <apex:outputField value="{!opp.Contact__c}" />                    
                <apex:outputField value="{!opp.StageName}" />
                <apex:pageblockSectionItem helpText="If the applicant has indicated they desire to start at different or later date than next eligible start date." >
                    <apex:outputLabel value="Desired Start Date"/>
                    <apex:outputField value="{!opp.MS_Desired_Start_Date__c}"/>
                </apex:pageblockSectionItem>
                <apex:outputField value="{!opp.Loss_Reason_NFI__c}" />
                <apex:outputField value="{!opp.CloseDate}" />                 
                <apex:outputField value="{!opp.Appl_Rcvd_Date__c}"/>   
                <apex:outputField value="{!opp.Application_Fee_Paid__c}" rendered="{!opp.Application_Fee_Paid__c = true}" />
                <apex:pageBlockSectionItem rendered="{!opp.Application_Fee_Paid__c = false}">
                    <apex:outputLabel value="Application Fee Paid"/>
                    <apex:commandButton action="{!appFeeRcvd}" id="appFeeButton" value="App Fee Received" rendered="{!opp.Application_Fee_Paid__c = false}"/> 
                </apex:pageBlockSectionItem>
                <apex:outputField value="{!opp.T_C_Rcvd_Date__c}" />
                <apex:outputField value="{!opp.Membership_Effective_Date__c}" />
                <apex:outputField value="{!opp.NetSuite_EID__c}" />
                <apex:pageBlockSectionItem />
                <apex:pageBlockSectionItem />
                <apex:outputField value="{!opp.CreatedDate}" />
                <apex:outputField value="{!opp.CreatedBy.Name}" label="Created By" />
            </apex:pageblockSection>
        </apex:pageblock> 
    </apex:form>
    <apex:relatedList subject="{!Opportunity}" list="OpenActivities"/>
    <apex:relatedList subject="{!Opportunity}" list="ActivityHistories"/>
    <apex:relatedList subject="{!Opportunity}" list="Packet_Requests__r"/>
</apex:page>
2
  • 1
    At the very least you need to add a rerender on your save button. You should specify your <apex:pageMessages /> tag so that any error messages will actually display. You need to add an id attribute to the latter for any of that to work. Leery of posting it as a proper answer since you may have other issues but it's a start.
    – Adrian Larson
    Aug 20, 2018 at 19:40
  • Thanks Adrian! I have an <apex: pageMessages /> tag in Line 5 of the page already. Do I need to add it anywhere else? Can you show me how I would add a rerender on my save button and an id attribute? Aug 20, 2018 at 20:09

1 Answer 1

2

A basic save operation with error handling should have markup similar to the below. You are missing an id attribute on your <apex:pageMessages> tag and also missing a rerender attribute on your <apex:commandButton> tag.

<apex:page ...>

    <apex:pageMessages id="msgs" />

    <!-- other markup-->

    <apex:commandButton action="{!save}" rerender="msgs" />

</apex:page>
1
  • Thank you! So when I add these elements in... now I push the Save button and nothing happens at all. ? Aug 21, 2018 at 14:46

You must log in to answer this question.

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