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
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='';
}
}
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=''; } }
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='';
}
}
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='';
}
}
All Answers
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='';
}
}
<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>