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
Manu Mahajan10Manu Mahajan10 

Action method not working after error message in visualforce page

Hi I have the following code. If the Account Name is equal to Mukesh,error message is shown on clicking save button.But when I correct the Account Name to some other Account Name, and click second time on the record, it is not getting saved.

Class
public class AccountFieldController
{

    public Account accountNew{get;set;}
    public Id acId{get;set;} 
    public ApexPages.StandardController controllerTest;

    public AccountFieldController(ApexPages.StandardController stdcontroller)
    {
         controllerTest=stdcontroller;    
        accountNew=(Account)stdcontroller.getRecord();

    }

    public PageReference save()
    {
        if(accountNew.AnnualRevenue>1000)
        {
          accountNew.Rating='Warm' ; 
        }

        else
        {
            accountNew.Rating='Cold'; 

        }

        controllerTest.save();

        acId=controllerTest.getId();



       if(accountNew.Name=='Mukesh')
       {

           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Correct Account name'));
           return null;

       }
        PageReference pr=new PageReference('/apex/fieldupdatedisplayrecord');
        pr.setRedirect(false);
        return pr;



    }



Page:

Page:FieldUpdatePage

<apex:page standardController="Account" extensions="AccountFieldController">
<apex:form id="formTest">
<apex:outputPanel id="panelTest">
<apex:outputText value="Hello Manu"/>
<apex:outputPanel id="panelTest1" rendered="{!IF(accountNew.Name=='Mukesh',false,true)}">
<apex:outputText value="Hello Manu1"/>
</apex:outputPanel>
</apex:outputPanel>
<apex:pageMessages escape="false" id="messageId"/>
<apex:pageBlock title="My Content">
<apex:pageBlockSection title="AccountInfo">
<apex:inputField value="{!accountNew.Name}"/>
<apex:inputField value="{!accountNew.Phone}"/>
<apex:inputField value="{!accountNew.AnnualRevenue}"/>
</apex:pageBlockSection>
<apex:pageBlockButtons location="bottom">
<apex:commandButton value="save" action="{!save}" reRender="messageId,panelTest"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Page to be redirected after Click on save-fieldupdatedisplayrecord

<apex:page standardController="Account" extensions="AccountFieldController" >
<apex:form >
    <apex:pageBlock title="Data Display">
        <apex:pageBlockTable value="{!accountNew}" var="item">
            <apex:column value="{!item.Name}"/>
            <apex:column value="{!item.Phone}"/>
            <apex:column value="{!item.AnnualRevenue}"/>
            <apex:column value="{!item.Rating}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>
</apex:page>
William TranWilliam Tran
If you put in Mukesh, the account is already saved since save is called before the error message appear.

 controllerTest.save(); --> Save is called first!!!

       if(accountNew.Name=='Mukesh') --> called after saved it done!!!!
       {

           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Correct Account name'));
           return null;

       }

thx
Manu Mahajan10Manu Mahajan10
Hi William..thanks for your reply.i changed my code and the record is getting saved in salesforce
  
       // insert accountNew;
        acId=controllerTest.getId();
       
        // return null;
      
       if(accountNew.Name=='Mukesh')
       {
       
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Correct Account name'));
           return null;
           //return ApexPages.CurrentPage();
       }
       controllerTest.save();
        PageReference pr=new PageReference('/apex/fieldupdatedisplayrecord');
        pr.setRedirect(false);
        return pr;


But after the error message, I am not able to go the new page ie fieldupdatedisplayrecord which displays the saved record.
William TranWilliam Tran
For what you need set redirect parameter to true like:

   pr.setRedirect(false);   change to     pr.setRedirect(true);
Manu Mahajan10Manu Mahajan10
In that case my context and view state would be lost.I dont want to lose context.Main Problem which I have observed is:
If I remove the rendered attribute ,error message is shown and after correction I am able to save record and redirect to new page also.I am not able to understand why the output panel is not allowing to save the record
Manu Mahajan10Manu Mahajan10
Sorry It is reRender...
William TranWilliam Tran
Is this issue resolved?  If not, please describe clearly what is the issue.

Thx