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
Gaurav AgnihotriGaurav Agnihotri 

How to nullify "value" of apex:inputText when a button is clicked

I am working on an apex page. I want the value of Quantity product to be NULL 
<apex:inputText value="{!QuantityProduct}" label="Quantity"/>
when command button action is executed as shown below:
<apex:commandButton action="{!ProcessSelected}" value="Add Selected Items" reRender="block2,errors" oncomplete="clearName();"/>
Below is VF 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 action="{!ProcessSelected}" value="Add Selected Items" reRender="block2,errors" oncomplete="clearName();"/>
  	<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
          	);
      	}
     	function clearName() {
     			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: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: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>

and class
public class ContactSearchController7 {
 // the soql without the order and limit
  private String soql {get;set;}
      //coping it from class CustomPaginationDemoPBE
  public List<ProductWrapper> lstWrapper {get;set;}
  public List<ProductWrapper> lstSetController{get;set;}
  CustomIterablePBE obj;
  //copy done
  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; }
  //GA 8/26/2015 commented- Code Coverage
  //this list is added by GA 8-17
  //The main purpose of this list is to inserts records in Quote Line Item
 /* Public List<QuoteLineItem> items{get; set; }
  public String QuoteId{
     get{return 'Quote#='+QuoteNumber +'  name:'+QuoteName+'   OptyId=' +OptyId;}
     set;
   }
*/
  // format the soql for display on the visualforce page
  public String debugSoql {
    get { return soql; }
    set;
  }
    /*
  public ApexPages.StandardSetController con { get; 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';
      //wrapproductlist.clear();
       lstWrapper.clear();
        wrapproductlist.clear();
      //cleaning values in Con : Standard Set Controller
      //con=null;
      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'); 
    //GA added 9/9/2015
     // String Quantity =Apexpages.currentPage().getParameters().get('Quantity'); 
    /* Gaurav Commented 09/02
      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';
       if (ProductType!=null)
    soql += ' and Product2.Product_Type__c LIKE \''+String.escapeSingleQuotes(ProductType)+'%\'';
        if (Description!=null)
    soql += ' and Product2.Description__c LIKE \''+String.escapeSingleQuotes(Description)+'%\'';
         if (Name!=null)
    soql += ' and name LIKE \''+String.escapeSingleQuotes(Name)+'%\''; 
    */
    //soql = 'SELECT ProductCode FROM Product2';
    if(ProductType.length()>1 || Description.length()>1 || Name.length()>1)
     {

        //if (ProductType==null && Description==null && Name==null)
        	//{
         	//	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';
        	//}

        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)+'%\'';
       }
    /*if (ProductType.length()>3)
        	{
         		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';
        	}  
   */
    // 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); 
       //system.currentPageReference().getParameters().put('Name',null);  
       //GA Added 09/09/2015
        //String Quantity=Apexpages.currentPage().getParameters().get('Quantity');
      //QuantityProduct=  integer.valueOf(Quantity);
       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; 
                           			//QuantityProduct=null;
                                      //wrapobj.PBE.Quantity__c=null;
                                   /*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
         QuantityProduct=null;
        }//end if QuantityProduct> 0
      // inserting the records in Quote Items
      
      /* if(items.size() > 0 )
        {
            insert 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();
        }
    

}

Regards, 
Gaurav
 
Amit Chaudhary 8Amit Chaudhary 8
Please try below code
 public pagereference ProcessSelected(){
QuantityProduct = 0;
}

Please let us know if this will help you

Thanks
Amit Chaudhary
Gaurav AgnihotriGaurav Agnihotri
Hi Amit, 
If I update QuantityProduct to 0. it does not reflect the value in VF page. I need to so something in the VF page.

Regards, 
Gaurav
SaranSaran
Hi Gaurav,

I think there must be something to do with reRender. On the button click you have reRendered only the block2 try adding the criteria2 so that even that block will be reRendered.

Also as amit said you can also make that value to null on the processSelected method.

Hope this might help you.

Thanks!