+ Start a Discussion
Renu KyalRenu Kyal 

Calling a controller function from Javascript action function, but not getting required output.

Hi,

 

following are the code requirements:

1. If value entered in product name is not present in Products, then it should enable Save button.

2. If value entered in product name is present in Products, then it should display all the contents in a table.

 

VF Code:

<apex:page StandardController="Product__c" extensions="ExistingController"  >
   <apex:form id="fm" >
   <apex:actionFunction name="invoke" action="{!search}" reRender="pb"/>

       <apex:pageBlock id="pb" title="Products" mode="edit" >
           <apex:pageBlockSection id="pbs" title="Search/Create a Product record" columns="2">
               <apex:inputField id="ifname" value="{!Product__c.name}"  />
               <apex:inputField value="{!Product__c.Description__c}" />
               <apex:inputField value="{!Product__c.Total_Inventory__c}"  />
               <apex:inputField value="{!Product__c.Price__c}" />
               <apex:inputField value="{!Product__c.Stock_Quantity__c}" />
           </apex:pageBlockSection>
               <apex:pageBlockButtons >
               <apex:commandButton value="reset"  action="{!Reset}"/>
               <apex:commandButton value="Save" action="{!Save}" rendered="{!flag}"/>
               <apex:commandButton value="Search" OnClick="nameNull()"/>
           </apex:pageBlockButtons>
          
           <apex:pageBlockTable id="pbt" var="pb" value="{!prodRecordList}">
               <apex:column value="{!pb.Name}" />
               <apex:column value="{!pb.Description__c}"/>
               <apex:column value="{!pb.Total_Inventory__c}"/>
               <apex:column value="{!pb.Price__c}"/>
               <apex:column value="{!pb.Stock_Quantity__c}"/>
           </apex:pageBlockTable>
           </apex:pageBlock>
      
      
   </apex:form>
   <script>
       function nameNull(){
           var text= document.getElementById("{!$Component.fm.pb.pbs.ifname}").value;
           if(text == '')
           {alert('Please enter value in Product Name field');
           }
           else
                {           invoke();
                                                   }        
                                           }
   </script>
</apex:page>

 

 

Controller Code:

 

public class ExistingController {

   public string nameStr{ get; private set; }
   Public List<product__c> prodRecordList {get; set;}
   Public product__c prod {get; set;}
   public boolean flag {get; set;}
  
   public ExistingController(ApexPages.StandardController stdController) {
       this.prod= (product__c)stdController.getRecord();
   }
  
   public pageReference search () {

       prodRecordList =  [SELECT id, Name, Description__c, Total_Inventory__c, Price__c, Stock_Quantity__c from product__c where name=:prod.name];
       System.debug('@@prodRecordList'+prodRecordList);
       if(prodRecordList.size()==0 )
      
           flag = true;
           return null;
   }
   public pageReference Reset() {

       PageReference newpage = new PageReference(System.currentPageReference().getURL());
       newpage.setRedirect(true);
       return newpage;  
          }
      
  public pageReference Save()  {
  INSERT prod;
  pageReference newpage = new PageReference('/apex/recordsaved');
  return newpage;
  }
}

 

When calling directly it works, but when called from Javascript action function it does not. Please help.

Nantha_WipNantha_Wip

<apex:page StandardController="Product__c" extensions="ExistingController"  >
   <apex:form id="fm" >
   <apex:actionFunction name="invoke" action="{!search}" reRender="pb">
<apex:param name="firstParam" assignTo="{!state}" value="" />
</apex:actionFunction>
       <apex:pageBlock id="pb" title="Products" mode="edit" >
           <apex:pageBlockSection id="pbs" title="Search/Create a Product record" columns="2">
               <apex:inputField id="ifname" value="{!Product__c.name}"  />
               <apex:inputField value="{!Product__c.Description__c}" />
               <apex:inputField value="{!Product__c.Total_Inventory__c}"  />
               <apex:inputField value="{!Product__c.Price__c}" />
               <apex:inputField value="{!Product__c.Stock_Quantity__c}" />
           </apex:pageBlockSection>
               <apex:pageBlockButtons >
               <apex:commandButton value="reset"  action="{!Reset}"/>
               <apex:commandButton value="Save" action="{!Save}" rendered="{!flag}"/>
               <apex:commandButton value="Search" OnClick="return nameNull()"/>
           </apex:pageBlockButtons>
          
           <apex:pageBlockTable id="pbt" var="pb" value="{!prodRecordList}">
               <apex:column value="{!pb.Name}" />
               <apex:column value="{!pb.Description__c}"/>
               <apex:column value="{!pb.Total_Inventory__c}"/>
               <apex:column value="{!pb.Price__c}"/>
               <apex:column value="{!pb.Stock_Quantity__c}"/>
           </apex:pageBlockTable>
           </apex:pageBlock>
      
      
   </apex:form>
   <script>
       function nameNull(){
           var text= document.getElementById("{!$Component.fm.pb.pbs.ifname}").value;
           if(text == '')
           {alert('Please enter value in Product Name field');
           }
           else
                {           
        invoke(text);
                                   }
        return false;
        
                                           }
   </script>
</apex:page>

 

 

Controller Code:

 

public class ExistingController {

   public string nameStr{ get; private set; }
   Public List<product__c> prodRecordList {get; set;}
   Public product__c prod {get; set;}
   public boolean flag {get; set;}
   public string state{get;set;}
 
   public ExistingController(ApexPages.StandardController stdController) {
       this.prod= (product__c)stdController.getRecord();
   }
 
   public pageReference search () {

       prodRecordList =  [SELECT id, Name, Description__c, Total_Inventory__c, Price__c, Stock_Quantity__c from product__c where name=:state];
       System.debug('@@prodRecordList'+prodRecordList);
       if(prodRecordList.size()==0 )
      
           flag = true;
           return null;
   }
   public pageReference Reset() {

       PageReference newpage = new PageReference(System.currentPageReference().getURL());
       newpage.setRedirect(true);
       return newpage;  
          }
      
  public pageReference Save()  {
  INSERT prod;
  pageReference newpage = new PageReference('/apex/recordsaved');
  return newpage;
  }
}



Correction1:You need to put "return" keyword when calling jscript code in commandbutton. otherwise use HTML input button

Correction 2: Use apex:param in action function for passing parameter

Please let me know if you have any doubts

Amol SolankiAmol Solanki
I tried this and it worked for me.

VF page :
<apex:commandButton value="Activate/Lock" id="activeIA"  styleClass="btn btn-success activeIA "  disabled="{!activeIA}" style="margin-right:10px;" html-data-toggle="tooltip" html-data-placement="bottom" html-data-original-title="Please click activate for activating Integration." />
                        <apex:actionFunction name="activateInterface" action="{!submitActiveIA}"  /> 


Javascript :
 $('body').on('click', '.activeIA', function(e) {
         var r = confirm("Once Interface is activated, you cannot make configuration changes until it is unlocked. Do you like to proceed?");
         if ( r == false){
               return false;
      }else{
          activateInterface();
          return false;
          }
      });