+ Start a Discussion
balraj singh 33balraj singh 33 

VISUALFORCE PAGE IS NOT THOWRING MY CUSTOM ERROR MESSAGE. VAR2 VARIABLE IS FROM VISUALFORCE PAGE INPUTFIELD

public class assignment {
    public list<account>var;
    public string var2{get;set;}   

 public void mymethod()
    {
        system.debug(vAR2);
        If(var2!=nULL)
        {
            List<List<SObject>> searchList= [find :var2 in all fields RETURNING account(name,id)];
            var = searchlist[0];        }
        else{
               
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please enter ant keyword'));
        
        }



page:

<apex:inputtext value="{!var2}"/>
    <apex:commandbutton value="Search" action="{!mymethod}" rerender="B"></apex:commandbutton>
Best Answer chosen by balraj singh 33
Raj VakatiRaj Vakati

THIS IS THE Wokring for me 
 

 

public class assignment {
    public list<account> var {get;set;}
    public string var2 {get;set;}   
    
    public void mymethod()
    {
        If(var2!=nULL)
        {
            List<List<SObject>> searchList= [find :var2 in all fields RETURNING account(name,id)];
            System.debug('searchList'+searchList);
            var = searchlist[0];      
        }
        else{
            
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please enter ant keyword'));
            
        }
    }
}
<apex:page controller="assignment"> 
    <apex:form id="B">
        <apex:pageBlock >
            <apex:pageBlockSection>
                <apex:inputtext value="{!var2}"/>
                <apex:commandbutton value="Search" action="{!mymethod}" rerender="B"></apex:commandbutton>
                
            </apex:pageBlockSection>
            <apex:pageBlockSection id="B">
                
                <apex:pageBlockTable value="{!var}" var="r">
                    <apex:column >
                        {!r.Name}
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
    
</apex:page>

 

All Answers

Raj VakatiRaj Vakati
add <apex:pageMessages> tag in your code 

 
Raj VakatiRaj Vakati
sampple code
 
<apex:page standardController="Opportunity" recordSetVar="opportunities"
    tabStyle="Opportunity" sidebar="false">
    <p>Enter an alphabetic character for the "Close Date," 
       then click Save to see what happens.</p>
    <apex:form >
        <apex:pageBlock >
        <apex:pageMessages />
        <apex:inputtext value="{!var2}"/>
    <apex:commandbutton value="Search" action="{!mymethod}" rerender="B"></apex:commandbutton>
    </apex:form>
</apex:page>

 
balraj singh 33balraj singh 33
IT IS NOT WORKING. IT IS GOING INSIDE THE IF 
 ERROR LINE :            List<List<SObject>> searchList= [find :var2 in all fields RETURNING account(name,id)];

BUT ACCORDING TO THE LOGIC IT SHLD NOT GO INSIDE IF 
 
Raj VakatiRaj Vakati

THIS IS THE Wokring for me 
 

 

public class assignment {
    public list<account> var {get;set;}
    public string var2 {get;set;}   
    
    public void mymethod()
    {
        If(var2!=nULL)
        {
            List<List<SObject>> searchList= [find :var2 in all fields RETURNING account(name,id)];
            System.debug('searchList'+searchList);
            var = searchlist[0];      
        }
        else{
            
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please enter ant keyword'));
            
        }
    }
}
<apex:page controller="assignment"> 
    <apex:form id="B">
        <apex:pageBlock >
            <apex:pageBlockSection>
                <apex:inputtext value="{!var2}"/>
                <apex:commandbutton value="Search" action="{!mymethod}" rerender="B"></apex:commandbutton>
                
            </apex:pageBlockSection>
            <apex:pageBlockSection id="B">
                
                <apex:pageBlockTable value="{!var}" var="r">
                    <apex:column >
                        {!r.Name}
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
    
</apex:page>

 
This was selected as the best answer
balraj singh 33balraj singh 33
Thanks Raj , i used If(var2.lenght()> 0) condition and now it is working