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
Shubham SengarShubham Sengar 

VF question

how to add New Button in VF search page.
like if our data is not found then a NEW Button there with the help of this button we can create new record 

my Search page works only m looking for NEW Button 


<apex:page StandardController="Work_Order__c" extensions="Search">
<apex:form >
   
            <apex:outputLabel >Enter The Work Order Number ::- </apex:outputLabel>
            
            
            <apex:inputText value="{!searchText}"/><br/>
            
            <apex:commandButton value="Search" Action="{!Search}"/><br/>
            
            <apex:pageBlock >
            <apex:pageBlockSection >
            
            <apex:outputText >Message: {!msg}</apex:outputText>
            </apex:pageBlockSection>
          </apex:pageBlock>
</apex:form>  
</apex:page>


Controller 

public class Search{
    
    public String SearchText{get;set;}
    public Work_Order__c wo{get;set;}
    public string msg{get;set;}
    

    public Search(ApexPages.StandardController controller)
     {

    }
    public void search()
    {
        List<Work_Order__c> wolist=[select id,name from Work_Order__c];
        for(Work_Order__c w:wolist)
        {
            if(w.name.containsIgnorecase(searchText))
            wo=w;                   
        }
        if(wo==null)
        {
            msg='Work Order Number Not Exist u can create New by given link below';
             
                        
        }
        else
        {
            msg='Work Order Number already Exist' ;
        }
    }
    }
 
srlawr uksrlawr uk
You can simply add a new button to the page next to your existing one along the lines of:
 
<apex:commandButton value="New" Action="{!createNew}"/>

And then in your controller you can have a method that redirects a user off to the create method...
public PageReference createRecord() {
        PageReference createPage = new ApexPages.StandardController(Work_Order__c);  
        createPage.setRedirect(true);
      return createPage;
}

Of course, the button will always there, so you might want to use the "Rendered" attribute to determine if it shows. You could put a flag on the controller and set it when the search is performed:
 
apex:commandButton value="New" Action="{!createNew}" rendered="{!showNew}"/>
and then in the controller have
 
public boolean showNew = false;
and after your message about there being no search results, set showNew to true.


 
Shubham SengarShubham Sengar
Hi srlawr uk.

thanks for your halp .but m geting error plz let me know where m doing worng .....

<apex:page StandardController="Work_Order__c" extensions="Search">
<apex:form >
   
            <apex:outputLabel >Enter The Work Order Number ::- </apex:outputLabel>
            
            
            <apex:inputText value="{!searchText}"/><br/>
            
            <apex:commandButton value="Search" Action="{!Search}"/><br/>
           <apex:commandButton value="New" Action="{!createNew}" rendered="{!showNew}"/>
           


            
            
            <apex:pageBlock >
            <apex:pageBlockSection >
            
            <apex:outputText >Message: {!msg}</apex:outputText>
            </apex:pageBlockSection>
          </apex:pageBlock>
</apex:form>  
</apex:page>

controller................................................................................................................................................................................

public class Search
{
    
    public String SearchText{get;set;}
    public Work_Order__c wo{get;set;}
    public string msg{get;set;}
    public boolean showNew = false;

    

    public Search(ApexPages.StandardController controller)
     {

    }
    public void search()
    {
        List<Work_Order__c> wolist=[select id,name from Work_Order__c];
        for(Work_Order__c w:wolist)
        {
            if(w.name.containsIgnorecase(searchText))
            wo=w;                   
        }
        if(wo==null)
        {
            msg='Work Order Number Not Exist u can create New by given link below';
             
                        
        }
        else
        {
            msg='Work Order Number already Exist' ;
        }
    }
   
    public PageReference createRecord() {

        PageReference createPage = new ApexPages.StandardController(Work_Order__c); 

        createPage.setRedirect(true);

      return createPage;

}

 }


 
srlawr uksrlawr uk
oh yeah, sorry. that controller advice was a bit bum. Easy to debug though, try something like:
 
public class Search
{    
    public String SearchText{get;set;}
    public Work_Order__c wo{get;set;}
    public string msg{get;set;}
    public boolean showNew { get; set;}

    public Search(ApexPages.StandardController controller)
     {
        showNew = false;
     }

    public void search()
    {
        List<Work_Order__c> wolist=[select id,name from Work_Order__c];
        for(Work_Order__c w:wolist)
        {
            if(w.name.containsIgnorecase(searchText))
            wo=w;                   
        }
        if(wo==null)
        {
            msg='Work Order Number Not Exist u can create New by given link below';
             showNew = true;
                        
        }
        else
        {
            msg='Work Order Number already Exist' ;
            showNew = true;
        }
    }
   
    public PageReference createRecord() {

        PageReference createPage = new ApexPages.StandardController(Work_Order__c); 

        createPage.setRedirect(true);

      return createPage;

}

 }
see - I was missing a get and set on the boolean definition (unless you wrote getCreateNew() instead) and then I've added the setting of it to true in your code (and also a set to false, to hide the button should they reperform the search)

any better?
Shubham SengarShubham Sengar
Hi srlawr uk
 now m geting this error Error: Search Compile Error: Variable does not exist: Work_Order__c at line 67 column 67

in this line 
 PageReference createPage=new ApexPages.StandardController(Work_Order__c);

i have Work_Order__c but its giving error .i have no idea why..

thanks
Shubham SengarShubham Sengar
ya m try hard but geting same error ...u know any other way to fix it or solve this problem