+ Start a Discussion
Gaurav AgnihotriGaurav Agnihotri 

How to reset the input Text value to blank on a click of a button

Gurus, 
I am doing a simple search and displaying the result. I am getting the value of the input field by
Apexpages.currentPage().getParameters().get()
String Name = Apexpages.currentPage().getParameters().get('Name'); 
    String Description= Apexpages.currentPage().getParameters().get('Description'); 
    String ProductType= Apexpages.currentPage().getParameters().get('ProductType');
Now, I want to clear the values on ProcessSelected() method
by using the code below:
Apexpages.currentPage().getParameters().put ('Name',null);

However, it does not update the value to blank on the visualforce page.

Any suggestions?

 
Best Answer chosen by Gaurav Agnihotri
Andy BoettcherAndy Boettcher
Ok - I see what you're doing now.

Without gutting your code and pursuing a different approach, if you create another Javascript function:
 
function doSearch() {
        searchServer(
          document.getElementById("Name").value,
          document.getElementById("Description").value,
          document.getElementById("ProductType").value
          );
}

function clearName() {
     document.getElementById("Name").value = '';
}

Then just add an "oncomplete" to your actionFunction to clear that field via the Javascript function:
 
<apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors" oncomplete="clearName();">
          <apex:param name="Name" value="" />
          <apex:param name="Description" value="" />
          <apex:param name="ProductType" value="" />
      </apex:actionFunction>

That should do it without refactoring!

 

All Answers

Andy BoettcherAndy Boettcher
Those parameters are for pulling values in when the page loads and setting them for the next page you go to.  You'll want to reference the controller variable you are binding your "search" input field to.
AmitSahuAmitSahu
In ProcessSelected() method set all these variables to null and rerender the search section.
Name=null;
Description=null;
ProductType=null;
Gaurav AgnihotriGaurav Agnihotri
Hi Andy, 
Can you give me an example.  I am totally lost here. 
Andy BoettcherAndy Boettcher
To do that, you'd need to post your code so we can see what you're really doing. :)
Gaurav AgnihotriGaurav Agnihotri
Here is my code.
public class ContactSearchController7 {
  private String soql {get;set;}
  public List<ProductWrapper> lstWrapper {get;set;}
  public List<ProductWrapper> lstSetController{get;set;}
  CustomIterablePBE obj;

  public integer QuantityProduct {get;set;}  
  private String QuoteNumber {get;set;}
  private String QuoteName {get;set;}
  private String OptyId {get;set;}
  //public String firstNameval{get;set;}
  public list<wrapproduct> wrapproductList { get; set; }
  public list<PricebookEntry> selectedAccounts{get; set; }
  public String debugSoql {
    get { return soql; }
    set;
  }
  public ContactSearchController7(ApexPages.StandardController controller) {
    wrapproductList = new list<wrapproduct>();
    selectedAccounts=new list<PricebookEntry>();
   //GA 8/26/2015 commented- Code Coverage
   // items=new list<QuoteLineItem>();
   //coping it from class CustomPaginationDemoPBE
    lstWrapper =  new List<ProductWrapper>();
    lstSetController = new List<ProductWrapper>();
    //copy done
   soql ='SELECT product2.Product_Type__c,product2.Description__c,product2.Item__c,product2.CCO_Standard_Cost__c ,name, ProductCode,UnitPrice,Quantity__c,select_product__c FROM PricebookEntry WHERE UnitPrice  IN (null)';
   QuoteNumber=System.currentPageReference().getParameters().get('id');
   QuoteName=System.currentPageReference().getParameters().get('name');
   OptyId=System.currentPageReference().getParameters().get('OpportunityId');
   runQuery();
  }
  // runs the actual query
  Public PageReference AddProduct(){
  return null;
  }
  public void runQuery() {
    try {
          
      soql=soql + ' limit 100';
       lstWrapper.clear();
        wrapproductlist.clear();
      List<PricebookEntry> lstContact = Database.query(soql);
        
        for(PricebookEntry PBE : lstContact )
        {
            lstWrapper.add(new ProductWrapper(PBE ,false));
        }

        obj = new CustomIterablePBE (lstWrapper); 
        obj.setPageSize = 17;
        next();       

    } catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'+e));
    }
  }

  // runs the search with parameters passed via Javascript
  public PageReference runSearch() {
    String Name = Apexpages.currentPage().getParameters().get('Name'); 
    String Description= Apexpages.currentPage().getParameters().get('Description'); 
    String ProductType= Apexpages.currentPage().getParameters().get('ProductType'); 
    //soql = 'SELECT ProductCode FROM Product2';
    if(ProductType.length()>1 || Description.length()>1 || Name.length()>1)
     {

        if (ProductType.length()>0 && Description.length()>0 && Name.length()==0)
            {
             	soql='SELECT product2.Product_Type__c,product2.Description__c,Id,product2.Item__c,Name,product2.CCO_Standard_Cost__c,UnitPrice,Quantity__c,select_product__c FROM PricebookEntry where Product2.Description__c LIKE \''+String.escapeSingleQuotes(Description)+'%\'' +' and Product2.Product_Type__c LIKE \''+String.escapeSingleQuotes(ProductType)+'%\'';
            }
        if (ProductType.length()>0 && Description.length()==0 && Name.length()==0)
            {
             soql='SELECT product2.Product_Type__c,product2.Description__c,Id,product2.Item__c,Name,product2.CCO_Standard_Cost__c,UnitPrice,Quantity__c,select_product__c FROM PricebookEntry where Product2.Product_Type__c LIKE \''+String.escapeSingleQuotes(ProductType)+'%\'';
            }
        if (ProductType.length()>0 && Description.length()>0 && Name.length()>0)
            {
             soql='SELECT product2.Product_Type__c,product2.Description__c,Id,product2.Item__c,Name,product2.CCO_Standard_Cost__c,UnitPrice,Quantity__c,select_product__c FROM PricebookEntry where Product2.Description__c LIKE \''+String.escapeSingleQuotes(Description)+'%\'' +' and Product2.Product_Type__c LIKE \''+String.escapeSingleQuotes(ProductType)+'%\'' +' and name LIKE \''+String.escapeSingleQuotes(Name)+'%\'';
             }
         if (ProductType.length()>0  && Name.length()>0 && Description.length() == 0)
            {
             soql='SELECT product2.Product_Type__c,product2.Description__c,Id,product2.Item__c,Name,product2.CCO_Standard_Cost__c,UnitPrice,Quantity__c,select_product__c FROM PricebookEntry where  Product2.Product_Type__c LIKE \''+String.escapeSingleQuotes(ProductType)+'%\'' +' and name LIKE \''+String.escapeSingleQuotes(Name)+'%\'';
             }
         if (ProductType.length() == 0 && Description.length() >0 && Name.length() >0)
            {
             soql='SELECT product2.Product_Type__c,product2.Description__c,Id,product2.Item__c,Name,product2.CCO_Standard_Cost__c,UnitPrice,Quantity__c,select_product__c FROM PricebookEntry where Product2.Description__c LIKE \''+String.escapeSingleQuotes(Description)+'%\''  +' and name LIKE \''+String.escapeSingleQuotes(Name)+'%\'';
             }
         if (ProductType.length() == 0 && Description.length() == 0 && Name.length() >0)
            {
             soql='SELECT product2.Product_Type__c,product2.Description__c,Id,product2.Item__c,Name,product2.CCO_Standard_Cost__c,UnitPrice,Quantity__c,select_product__c FROM PricebookEntry where name LIKE \''+String.escapeSingleQuotes(Name)+'%\'';
             }
     }//end if
     if (ProductType.length()==0 && Description.length()>0 && Name.length()==0)
       {
           soql='SELECT product2.Product_Type__c,product2.Description__c,Id,product2.Item__c,Name,product2.CCO_Standard_Cost__c,UnitPrice,Quantity__c,select_product__c FROM PricebookEntry where Product2.Description__c LIKE \''+String.escapeSingleQuotes(Description)+'%\'';
       }

    // run the query again
      if(ProductType.length()>1 || Description.length()>1 || Name.length()>1){
            runQuery();  
      }
     if (ProductType.length()==0 && Description.length()==0 && Name.length()==0){
            soql='SELECT product2.Product_Type__c,product2.Description__c,Id,product2.Item__c,Name,product2.CCO_Standard_Cost__c,UnitPrice,Quantity__c,select_product__c FROM PricebookEntry where name=null  and Product2.Product_Type__c=null and Product2.Description__c =null';
            runQuery();  
      }

    return null;
  }
  
   
   
    //### SELECTED PRODUCT SHOWN BY THIS METHOD
      public pagereference ProcessSelected(){
       Apexpages.currentPage().getParameters().put ('Name',null); 
          Name=null;
       //system.currentPageReference().getParameters().put('Name',null);   
       integer sAccountCount=[SELECT count() FROM Quote WHERE Pelco_Account_Name__c != null AND Id =: QuoteNumber];
       String CustomerNumber='0';
       integer CustomerNumberCount;
       string AccountName;
       String ItemNumber='0';
       if (sAccountCount>0){
           Id AccountId= [SELECT Pelco_Account_Name__c FROM Quote WHERE Pelco_Account_Name__c != null AND Id =: QuoteNumber].Pelco_Account_Name__c;
            CustomerNumberCount=[SELECT count() from Account WHERE Id =: AccountId and AccountNumber != null ];
                if(CustomerNumberCount == 0){
                    AccountName=[SELECT name from Account where Id =: AccountId].name;
                    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Pricing not possible. Customer Number is null. Please add Customer Number for Account Name: '+AccountName));
                }
            if (CustomerNumberCount> 0){
            	CustomerNumber=[SELECT AccountNumber FROM Account WHERE Id =: AccountId].AccountNumber;
            	}
        }
        if (sAccountCount == 0){
           Id AccountId= [SELECT AccountId FROM Quote WHERE Pelco_Account_Name__c != null AND Id =: QuoteNumber].Pelco_Account_Name__c;
           CustomerNumber=[SELECT AccountNumber FROM Account WHERE Id =: AccountId].AccountNumber;
        }
     Integer Count=0;
          for(productwrapper wrapobj1:lstWrapper){
           if(wrapobj1.isSelected==true){
            Count=Count+1;}//end if
           }//end for
      if (Count > 5 ){
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please select 5 or less items at a time to add to Quote'));
             }//end if
     if(QuantityProduct> 0 && CustomerNumberCount>0 && Count<=5){ 
      //for(wrapproduct wrapobj:wrapproductlist){
		//count=wraprojectlist.isSelected.size();
         for(productwrapper wrapobj:lstWrapper){
           if(wrapobj.isSelected==true){
                        Integer ProductCount=[SELECT count() FROM QuoteLineItem WHERE Product2Id =: wrapobj.PBE.Product2.Id AND QuoteId =: QuoteNumber];
                       if (ProductCount == 0 ){
                              wrapobj.PBE.Quantity__c=QuantityProduct; 
                              selectedAccounts.add(wrapobj.PBE);
                               ItemNumber= wrapobj.PBE.Name;
                               QuoteLineItem pi= new QuoteLineItem();
                                     //double DealerPrice=wrapobj.PBE.UnitPrice;
                                     CalculatePelcoDiscount1 NewDiscount= new CalculatePelcoDiscount1();
                                     double sDiscount=NewDiscount.CalculatePelcoDiscount1(ItemNumber,CustomerNumber )*100;
                                     CalculatePelcoPrice newPrice=new CalculatePelcoPrice();
                                     double DealerPrice=newPrice.CalculatePelcoPrice(ItemNumber,CustomerNumber );
                                     string pelcoCurrency=newPrice.CalculatePelcoCurrency(CustomerNumber);
                                     //double DealerPrice=pelcoPrice;
                                     //double sDiscount=PelcoDiscount;
                                     //sDiscount=sDiscount*100;
                                    //QuoteLineItem QLI = new QuoteLineItem(QuoteId = QuoteNumber,PricebookEntryId=wrapobj.accn.Id,Product2Id=wrapobj.accn.Product2.Id ,Quantity = wrapobj.accn.Quantity__c, UnitPrice=wrapobj.accn.UnitPrice,discount=sDiscount);
                                     //Gaurav Added 9/2- Due to changes in percentage
                           			 sDiscount=sDiscount/100;
                                     QuoteLineItem QLI = new QuoteLineItem(Currency__c=pelcoCurrency,QuoteId = QuoteNumber,PricebookEntryId=wrapobj.PBE.Id,Product2Id=wrapobj.PBE.Product2.Id ,CCO_Standard_Cost__c =wrapobj.PBE.Product2.CCO_Standard_Cost__c,Quantity = QuantityProduct, UnitPrice=DealerPrice,Dealer_Price__c=DealerPrice, discount=sDiscount);
                                     insert QLI; 
                                   /*Database.SaveResult[] lsr=Database.insert(new QuoteLineItem[]{QLI});
                                    for(Database.SaveResult sr: lsr){
                                    if(!sr.isSuccess()){
                                        Database.Error err = sr.getErrors()[0];
                                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Error in Insert'));
                                        }//end if sr
                                     }//end for
                                    */
                                   // populating and then adding data in the List
                                 /*  
                              		pi.Currency__c=pelcoCurrency;
	                          		pi.QuoteId = QuoteNumber;
	                          		pi.PricebookEntryId=wrapobj.PBE.Id;
	                          		pi.Product2Id=wrapobj.PBE.Product2.Id;
	                          		pi.Quantity = QuantityProduct; 
	                          		pi.UnitPrice=DealerPrice;
	                          		pi.Dealer_Price__c=DealerPrice; 
	                          		pi.discount=sDiscount;
                              items.add(pi);*/
                            }//end if ProductCount
                            if (ProductCount > 0 ){
                              ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'You have already added '+wrapobj.PBE.name));
                           }//end if
                  // }//end if
                  /* */

           }//end if

         }//end for
        }//end if QuantityProduct> 0
      // inserting the records in Quote Items
      

      if(QuantityProduct== 0){ 
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Add the Qantity of Item'));
        } 
       return null;
      }//end function

   
    public PageReference BackToQuote(){
           string Oppty_Id=[SELECT OpportunityId FROM Quote WHERE Id =: QuoteNumber].OpportunityId;
           boolean AccntOpty=[SELECT Account_Opportunity__c FROM Opportunity WHERE Id =: Oppty_Id].Account_Opportunity__c;
           if (AccntOpty== true){
           return new PageReference('/apex/newquotepage?id='+QuoteNumber);
           }
           if (AccntOpty == false){
            return new PageReference('/'+QuoteNumber);
            }
            return new PageReference('/'+QuoteNumber);
         }
   
  public Boolean hasNext {
            get 
            {
                return obj.hasNext();
            }
            set;
        }
        
        public Boolean hasPrevious {
            get 
            {
                return obj.hasPrevious();
            }
            set;
        }
        
        public void next() 
        {
            lstSetController = obj.next();
        }
        
        public void previous() 
        {
            lstSetController = obj.previous();
        }
    

}
 and the visualforce page 
<apex:page standardController="Quote" extensions="ContactSearchController7" sidebar="false">
  <apex:form >
  <apex:pageMessages id="errors" />
  <apex:commandButton action="{!BackToQuote}" value="Back to Quote"/>
  <apex:pageBlock title="Find Me An Item!" mode="edit">
   <!--apex:commandButton value="Add Product" action="{!AddProduct}"/-->
   <apex:commandButton action="{!ProcessSelected}" value="Add Selected Items" reRender="block2,errors"/>
   <!--apex:commandButton action="{!ProcessRemoved}" value="Remove Selected Products" reRender="block2,errors"/-->
 
  

  <table width="100%" border="0">
  <tr>  
    <td width="200" valign="top">

      <apex:pageBlock title="Parameters" mode="edit" id="criteria">

      <script type="text/javascript">
      function doSearch() {
        searchServer(
          document.getElementById("Name").value,
          document.getElementById("Description").value,
          document.getElementById("ProductType").value
          );
      }
      </script> 

      <apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors">
          <apex:param name="Name" value="" />
          <apex:param name="Description" value="" />
          <apex:param name="ProductType" value="" />
      </apex:actionFunction>

      <table cellpadding="2" cellspacing="2">
       <tr>
        <td style="font-weight:bold;">Item #<br/>
        <input type="text" id="Name" onkeyup="doSearch();"/>
        </td>
      </tr> 
       <tr>
        <td style="font-weight:bold;">Description<br/>
        <input type="text" id="Description" onkeyup="doSearch();"/>
        </td>
      </tr> 
       <tr>
        <td style="font-weight:bold;">Product Type<br/>
        <input type="text" id="ProductType" onkeyup="doSearch();"/>
        </td>
      </tr>  
      </table>

      </apex:pageBlock>
       <apex:pageBlock title="Quantity" mode="edit" id="criteria2">
       <table cellpadding="2" cellspacing="2">
       <tr>
        <td style="font-weight:bold;">Quantity<br/>
        <apex:inputText value="{!QuantityProduct}" label="Quantity"/>
        </td>
      </tr> 
      </table>
       </apex:pageBlock>
    </td>
    <td valign="top">

    <apex:pageBlock mode="edit" id="results">

        <apex:pageBlockTable value="{!lstSetController}"  var="obj">
           <apex:column >
             <apex:facet name="header">
               <apex:inputCheckbox />
             </apex:facet>
            <apex:inputCheckbox value="{!obj.isSelected}" id="InputId"/>
           </apex:column>
           <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Item"  rerender="results,debug">
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!obj.PBE.Name}"/>
            </apex:column>
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Description"  rerender="results,debug">
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!obj.PBE.Product2.Description__c}"/>
            </apex:column>
             <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="Product Type"  rerender="results,debug">
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!obj.PBE.Product2.Product_Type__c }"/>
            </apex:column>
             
            <apex:column >
                <apex:facet name="header">
                    <apex:commandLink value="List Price"  rerender="results,debug">
                    </apex:commandLink>
                </apex:facet>
                <apex:outputField value="{!obj.PBE.UnitPrice}"/>
            </apex:column>
            <!--apex:column >
                 <apex:facet name="header">Quantity</apex:facet>
                <apex:inputfield label="Qantity" value="{!wacc.accn.Quantity__c}"/>
            <!--/apex:column-->
        </apex:pageBlockTable>     
           <apex:outputPanel >
           <apex:commandButton value="<<Previous" action="{!previous}" rendered="{!hasPrevious}" reRender="results" />
           <apex:commandButton value="Next >>" action="{!next}" rendered="{!hasNext}" reRender="results" />
        </apex:outputPanel> 
 
    </apex:pageBlock>

    </td>
  </tr>
  </table>
<!--apex:pageBlockTable value="{!selectedAccounts}" var="sa" id="block2"-->
<apex:pageBlock title="Selected Products" mode="edit">
<apex:pageBlockTable value="{!selectedAccounts}" var="obj" id="block2">
      <table cellpadding="2" cellspacing="2">
      <tr>
      <td>
            <apex:column value="{!obj.product2.Item__c}" />
      </td>
      </tr>
             <apex:column value="{!obj.product2.Description__c}"/>
            <apex:column value="{!obj.Product2.Product_Type__c}"/>
            <apex:column value="{!obj.UnitPrice}"/>
            <apex:column value="{!obj.Quantity__c}"/>
      </table>
</apex:pageBlockTable>
</apex:pageBlock> 
<!--
  <apex:pageBlock title="Debug - SOQL" id="debug">
      <apex:outputText value="{!debugSoql}" />   
      <!--apex:outputText value="{!QuoteId}" /-->  
       <!--apex:outputText value="{!QuantityProduct}" /-->  
      <!--apex:outputText value="{!Productcount}" /-->  
       <!--  <br></br> -->
      <!--apex:outputText value="{!sProductid}"/--> 
 <!--
 </apex:pageBlock> 
 -->

  </apex:pageBlock>

  </apex:form>

</apex:page>


 
Andy BoettcherAndy Boettcher
Ok - I see what you're doing now.

Without gutting your code and pursuing a different approach, if you create another Javascript function:
 
function doSearch() {
        searchServer(
          document.getElementById("Name").value,
          document.getElementById("Description").value,
          document.getElementById("ProductType").value
          );
}

function clearName() {
     document.getElementById("Name").value = '';
}

Then just add an "oncomplete" to your actionFunction to clear that field via the Javascript function:
 
<apex:actionFunction name="searchServer" action="{!runSearch}" rerender="results,debug,errors" oncomplete="clearName();">
          <apex:param name="Name" value="" />
          <apex:param name="Description" value="" />
          <apex:param name="ProductType" value="" />
      </apex:actionFunction>

That should do it without refactoring!

 
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
try below code in your function

Name='';
Description='';
ProductType='';
 
Gaurav AgnihotriGaurav Agnihotri
Hi Andy, 
Your changes are clearing any text I type in the search . How can I use this for action ​ProcessSelected 
Andy BoettcherAndy Boettcher
Gaurav,

I think we are misunderstanding each other.  What is the actual desired result (not technical) that you're trying to achieve?  I understood that you were trying to clear the "Name" field after your search ran via "ProcessSelected".
Gaurav AgnihotriGaurav Agnihotri
Hi Andy, 
I was able to get the desired result by adding oncomplete on apex:commandButton
<apex:commandButton action="{!ProcessSelected}" value="Add Selected Items" reRender="block2,errors" oncomplete="clearName();"/>

It works fine.
Thank you for your help.
Regards, 
Gaurav
Andy BoettcherAndy Boettcher
Awesome!  I'm glad you figured it out.  Happy coding!