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
Subrahmanyam KonathlapalliSubrahmanyam Konathlapalli 

HI All, I am in learning state of SFDC. I trying to write prog with the requirement as. If age is not entered throw an error message,if age is entered navigate to diff page. I written the code but it is redirecting to diff page with both the conditions

Best Answer chosen by Subrahmanyam Konathlapalli
Raj VakatiRaj Vakati
Here is the code
 
public class Apex_Message_Example2{
    public string name{set;get;}
    public integer age{set;get;}
    public string city{set;get;}
    public PageReference submit(){
        PageReference p;
        System.debug('age'+age);
        if(age==null || age ==0){
            System.debug('In IF ');
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Age cannot be blank'));
            return null;
            
        }        
        else{
            System.debug(' in else ');
            p =page.Sample_Page;
            return p ;
        }
        //  return null ;
    }    
    public void Cancel(){
        name='';
        age=null;
        City='';
    }  
}
 
<apex:page controller="Apex_Message_Example2"  >
    <apex:form >
        <apex:pageblock title="ApexMessage">
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="save" action="{!Submit}" />
                <apex:commandButton value="Cancel" action="{!Cancel}"/>
            </apex:pageBlockButtons>
            <apex:pageMessages />
            
            <apex:pageBlockSection columns="1" >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Name"/>
                    <apex:inputText value="{!Name}"/>           
                </apex:pageBlockSectionItem> 
                <apex:pageblocksectionItem >
                    <apex:outputLabel value="Age"/>
                    <apex:inputText value="{!Age}" />
                </apex:pageblocksectionItem>  
                <apex:pageblocksectionItem >
                    <apex:outputLabel value="city"/>
                    <apex:inputText value="{!City}" />
                </apex:pageblocksectionItem>         
            </apex:pageBlockSection> 
        </apex:pageblock>
    </apex:form>
</apex:page>

 

All Answers

Subrahmanyam KonathlapalliSubrahmanyam Konathlapalli
public class Apex_Message_Example2{
public string name{set;get;}
public integer age{set;get;}
public string city{set;get;}
public PageReference submit(){
    PageReference p; 
         if(age==null){
        Apexpages.Message msg=new Apexpages.Message(Apexpages.severity.Error,'Age cannot be blank');
        Apexpages.addMessage(msg);
        }        
     if(age!=null){
        p =page.Sample_Page;
     }
     return p;
 }    
 public void Cancel(){
        name='';
        age=null;
        City='';
   }  
}
Raj VakatiRaj Vakati
try ths
 
public class Apex_Message_Example2{
    public string name{set;get;}
    public integer age{set;get;}
    public string city{set;get;}
    public PageReference submit(){
        PageReference p; 
        if(age==null){
            Apexpages.Message msg=new Apexpages.Message(Apexpages.severity.Error,'Age cannot be blank');
            Apexpages.addMessage(msg);
            return null ;
        }        
        if(age!=null){
            p =page.Sample_Page;
            return p ;
        }
        return null ;
    }    
    public void Cancel(){
        name='';
        age=null;
        City='';
    }  
}

 
Subrahmanyam KonathlapalliSubrahmanyam Konathlapalli
Hi Raj, I did changes to the code but the out is is redirecting to diff page with both the conditions..
 
Raj VakatiRaj Vakati
Can you give me page as well ??
Subrahmanyam KonathlapalliSubrahmanyam Konathlapalli
Hi Raj, This is the page

<apex:page controller="Apex_Message_Example2">
<apex:form id="one">
<apex:pageMessages />
    <apex:pageblock title="ApexMessage">
    <apex:pageBlockButtons location="top">
        <apex:commandButton value="save" action="{!Submit}" reRender="one"/>
        <apex:commandButton value="Cancel" action="{!Cancel}"/>
    </apex:pageBlockButtons>
        <apex:pageBlockSection columns="1">
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Name"/>
                <apex:inputText value="{!Name}"/>           
            </apex:pageBlockSectionItem> 
            <apex:pageblocksectionItem >
                 <apex:outputLabel value="Age"/>
                 <apex:inputText value="{!Age}" />
            </apex:pageblocksectionItem>  
            <apex:pageblocksectionItem >
                 <apex:outputLabel value="city"/>
                 <apex:inputText value="{!City}" />
            </apex:pageblocksectionItem>         
        </apex:pageBlockSection>   
    </apex:pageblock>
</apex:form>
</apex:page>
Raj VakatiRaj Vakati
Here is the code
 
public class Apex_Message_Example2{
    public string name{set;get;}
    public integer age{set;get;}
    public string city{set;get;}
    public PageReference submit(){
        PageReference p;
        System.debug('age'+age);
        if(age==null || age ==0){
            System.debug('In IF ');
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Age cannot be blank'));
            return null;
            
        }        
        else{
            System.debug(' in else ');
            p =page.Sample_Page;
            return p ;
        }
        //  return null ;
    }    
    public void Cancel(){
        name='';
        age=null;
        City='';
    }  
}
 
<apex:page controller="Apex_Message_Example2"  >
    <apex:form >
        <apex:pageblock title="ApexMessage">
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="save" action="{!Submit}" />
                <apex:commandButton value="Cancel" action="{!Cancel}"/>
            </apex:pageBlockButtons>
            <apex:pageMessages />
            
            <apex:pageBlockSection columns="1" >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Name"/>
                    <apex:inputText value="{!Name}"/>           
                </apex:pageBlockSectionItem> 
                <apex:pageblocksectionItem >
                    <apex:outputLabel value="Age"/>
                    <apex:inputText value="{!Age}" />
                </apex:pageblocksectionItem>  
                <apex:pageblocksectionItem >
                    <apex:outputLabel value="city"/>
                    <apex:inputText value="{!City}" />
                </apex:pageblocksectionItem>         
            </apex:pageBlockSection> 
        </apex:pageblock>
    </apex:form>
</apex:page>

 
This was selected as the best answer
Subrahmanyam KonathlapalliSubrahmanyam Konathlapalli
Hi Raj, Thank you somuch...