0

On click of save button a new record as to be created with the values inputted by user and it should be able to redirect user to company detail page. Add error messages on page if user clicks on this button leaving any field blank.I have created VF page as shown below.but even after entering the value for blank field I am getting the error message instead of redirecting to detail page.pls someone help me to solve this task.

apex Page:

<apex:page standardController="Company__c" extensions="errorMsg">
 <apex:form >
 <apex:pageBlock >

 <apex:pageBlockSection title="Company Record" collapsible="false" columns="1">
       <apex:inputField value="{!Company__c.Name}"/>
        <apex:pageMessages id="showMsg"></apex:pageMessages>
       <apex:inputField value="{!Company__c.company_Owner_Name__c}"/>
    <apex:pageBlockSection title="Address" collapsible="false" columns="1">
      <apex:inputField value="{!Company__c.Country__c}"/>
      <apex:inputField value="{!Company__c.State__c}"/>
      <apex:inputField value="{!Company__c.Zip_Code__c}"/>
  <apex:commandButton value="Save" action="{!save}">
  <apex:commandButton value="Cancel" action="{!cancel}" >
  </apex:commandButton>
  </apex:commandButton>
 </apex:pageBlockSection>
 </apex:pageBlockSection>
 </apex:pageBlock>
 </apex:form>
</apex:page>

extension:

public with sharing class errorMsg {

  public Company__c comp{get; set;}
    public errorMsg(ApexPages.StandardController controller) {
      comp=new Company__c();
    }

    public PageReference save(){

     if(comp.Name == null){
       ApexPages.Message myMsg = new  ApexPages.Message(ApexPages.Severity.ERROR,'Please enter Company Name');
            ApexPages.addMessage(myMsg); 
            return null;
        }

        // otherwise save as usual
        insert comp;

        return null;

}
} 
  • you are not returning any pagereference – Raul Jan 30 '17 at 10:32
1

There's a problem in your code.

In your VF Page, you use the standard controller record but in your controller you instanciate a new Company__c object.

You have to use the standard controller record in your controller to get the values updated in your VF page as below:

public class errorMsg {

  private final Company__c comp;
    public errorMsg(ApexPages.StandardController controller) {
      this.comp = (Company__c)stdController.getRecord();
    }

    public PageReference save(){

     if(comp.Name == null){
       ApexPages.Message myMsg = new  ApexPages.Message(ApexPages.Severity.ERROR,'Please enter Company Name');
            ApexPages.addMessage(myMsg); 
            return null;
        }

        // otherwise save as usual
        insert comp;

        return null;

}
} 
  • maybe having try catch in DML and return some URL in try would be good. – Raul Jan 30 '17 at 10:35
  • 1
    @RahulSharma Even better, use StandardController's save method. No try-catch is needed then. – sfdcfox Jan 30 '17 at 11:09
  • Indeed @sfdcfox ! – Raul Jan 30 '17 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.