1

I've written a vf page and try to send value from <apex:actionFunction> vf page to controller but in controller, i think searchjob() function is not executing. and i'm not getting any debug statement in debug log.

In controller i've to take the value from vf page and add to list.

Please help

<apex:page sidebar="true" controller="Student_Controller">
<apex:form >
<script type="text/javascript">
    function doSearch(a) 
        { 
              alert(a);
              UpdateUser(a);
        }  
   </script>

<apex:pageBlock >
     <apex:actionFunction name="UpdateUser" action="{!SearchJob}" reRender="" >
        <apex:param name="param1" value="" />
     </apex:actionFunction>

     <div id="category" style="">Category: <br/>
        <apex:selectcheckboxes layout="pagedirection" value="{!ProductName}" onclick="doSearch(this.value);" style="">                   
            <apex:selectoptions value="{!ProductNames}" />  
            <apex:actionsupport event="onchange" oncomplete="" reRender="" />
        </apex:selectcheckboxes> 
     </div>  

</apex:pageBlock>
</apex:form>

Controler;

public with sharing class Student_Controller {

public List<string> ProductName{get;set;}
public list<string> lst{get;set;}

public List<selectoption> getProductNames() 
{           
    list<selectoption> options = new list<selectoption>();            

    //Product Name is a MultiSelect Picklist               
    Schema.DescribeFieldResult fieldResult = student__c.Approval_Status__c.getDescribe();

    list<schema.picklistentry> values = fieldResult.getPickListValues();               
    for (Schema.PicklistEntry a : values) 
    {                  
    options.add(new SelectOption(a.getLabel(), a.getValue()));
    }           
    return options; 
}

 public PageReference SearchJob() {

     String SearchCompany = Apexpages.currentPage().getParameters().get('param1');
      system.debug('^^^^^^^^^^^^^^^^^^^^^^^^^^'+SearchCompany );
      lst = new list<string>();
      lst.add(SearchCompany );
       system.debug('^^^^^^^^^^^^^^^^^^^^^^^^^^'+lst);
     return null;
     }
 }

Please suggest what is the problem here;

  • never tried rerender="" so not sure this is issue but can you try rerender="temp". Also you are getting alert? – Tushar Sharma Nov 30 '16 at 7:00
  • 1
    @Tushar Sharma. I've tried it but, not working. – Amol Nov 30 '16 at 7:06
  • 1
    @TusharSharma rerender="" works just fine if you want to prevent a full page refresh – Eric Nov 30 '16 at 7:44
1

You never initialized you String[] ProductNames. The fact that it does not throw an error about being unable to set the value when using an action function could be considered a platform bug I guess. I got the error message when using the command button.

you can use the example below or add the constructor to your existing code and it will work. When passing the list of strings you need to initialize it before you can add to is via component. At least that is what I found. Simply adding the constructor produced the desired results. but using the onclick like you did with this.value and the param only passes the value of the clicked checkbox (regardless of state)

Also change your JS event from onclick to onchange

Class

public with sharing class Student_Controller {

public List<string> ProductName{get;set;}
public list<string> lst{get;set;}

public Student_Controller(){
    ProductName = New String[]{};
}

public List<selectoption> getProductNames() 
{           
    list<selectoption> options = new list<selectoption>{New SelectOption('','Select')};            

    //Product Name is a MultiSelect Picklist               
    options.add(new SelectOption('1', '1'));

    return options; 
}

 public PageReference SearchJob() {

     String SearchCompany = Apexpages.currentPage().getParameters().get('param1');
      system.debug('^^^^^^^^^^^^^^^^^^^^^^^^^^'+SearchCompany );
      lst = new list<string>();
      lst.add(SearchCompany );
       system.debug('^^^^^^^^^^^^^^^^^^^^^^^^^^'+lst);
     return null;
     }
 }

Page

<apex:page sidebar="true" controller="Student_Controller">
<apex:form >
<script type="text/javascript">
    function doSearch(a) 
        { 
              alert(a);
              UpdateUser(a);
        }  
   </script>

<apex:pageBlock >
     <apex:actionFunction name="UpdateUser" action="{!SearchJob}" reRender="" >
        <apex:param name="param1" value="" />
     </apex:actionFunction>

     <div id="category" style="">Category: <br/>
        <apex:selectcheckboxes layout="pagedirection" value="{!ProductName}" onchange="doSearch(this.value);" style="">                   
            <apex:selectoptions value="{!ProductNames}" />  
            <apex:actionsupport event="onchange" oncomplete="" reRender="" />
        </apex:selectcheckboxes> 
     </div>  

</apex:pageBlock>
</apex:form>
</apex:page>

Since you are assigning the values to ProductName just use ProductName in your code instead of the param to ensure only the checked values are used

To ensure that only the selected Items are used use the standard functionality and use ProductName.

Example Class

public with sharing class Student_Controller {

public string[] ProductName {get;set;}

public Student_Controller(){
    ProductName = New String[]{};
}
public List<selectoption> getProductNames() 
{           
    list<selectoption> options = new list<selectoption>{New SelectOption('','Select')};            

    //Product Name is a MultiSelect Picklist               
    options.add(new SelectOption('1', '1'));

    return options; 
}



 public void SearchJob() {
      system.debug(ProductName);
     }
 }

Example Page

<apex:page sidebar="true" controller="Student_Controller">
<apex:form >
<apex:Messages id="msgs"/>
<apex:pageBlock >

     <div id="category" style="">Category: <br/>
        <apex:selectcheckboxes layout="pagedirection" value="{!ProductName}" >                   
            <apex:selectoptions value="{!ProductNames}" />  
        </apex:selectcheckboxes> 
     </div>  



</apex:pageBlock>
<apex:commandbutton action="{!SearchJob}" rerender="" value="click me"/>
</apex:form>

</apex:page>

I would be remiss if I did not point you to the Manual which has excellent examples

  • As i've to pass value to controller by using actionfuction,i'm not getting . Can you please give me code. – Amol Nov 30 '16 at 7:14
  • @Amol - See updated answer – Eric Nov 30 '16 at 7:33
  • I dont want to use a button, as i 'm running it as a dynamic filter on vf page. Thats why i'm passinig value from action function by javascript. – Amol Nov 30 '16 at 7:33
  • Then don't use the dang button. Add the constructor to your class to initialize the String[] like I mentioned and it will work. But you will need to work on the value you pass because all you are passing is the option value of the checkbox regardless of if it is checked or not – Eric Nov 30 '16 at 7:35
  • @Amol - And change your event from on click to onchange – Eric Nov 30 '16 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.