0

I am using Knowledge Base and Sites.

Basic overview of the setup: A Controller used across a VF page and a number of VF Components. A VF page which calls the VF Component. A Component which I'd like to set a Controller property after a selectList onchange event.

Attached are some code snippets: ----Component (MyComp)----

    <apex:component allowDML="true">
      <apex:attribute name="pkController" type="MyController" required="true" description="handle" />
      <header>
       <div id="container">
        <apex:form id="dropdown-form">
         <apex:actionRegion>
          <apex:selectList size="1" value="{!pkController.selectedCategory}">
              <apex:selectOptions id="dropdown-content" value="{!pkController.dynamiclist}"></apex:selectOptions>
              <apex:actionSupport event="onchange" reRender="container" action="{!pkController.topTierCategorySelection}"/>
            </apex:selectList>
          </apex:actionRegion>
       </apex:form>
      </div>
     </header>

----MyController code----

public class MyController {
  public String selectedCategory { get; set; }
  public String topTierCategorySelection { get; set;}
  public transient List<SelectOption> options = new List<SelectOption>();
  public transient List<DescribeDataCategoryGroupResult> ddcgr;
  public transient List<DescribeDataCategoryGroupStructureResult> ddcgrStructureResult;
  public transient List<String> grpNames = new List<String>();
  public transient DataCategory [] toplevelCategories;

  public List<SelectOption> getdynamiclist() {
  if(ddcgrStructureResult != null){
    for(DescribeDataCategoryGroupStructureResult singleResult : ddcgrStructureResult){

      ////Get the top level categories
      if(singleResult.getName() == categoryGroup1){
        toplevelCategories = singleResult.getTopCategories();
      }
    }
    //Recursively get all the categories
    List<DataCategory> allCategories = getAllCategories(toplevelCategories);
    for(DataCategory category : toplevelCategories) {
      DataCategory [] childCategories = category.getChildCategories();
      for(DataCategory catRow : childCategories){
        options.add(new selectOption(catRow.getName(),catRow.getName()));
      }
    }
  }
 return options;
 }
 private static DataCategory[] getAllCategories(DataCategory [] categories){
  if(categories.isEmpty()){
   return new DataCategory[]{};
  } else {
   DataCategory [] categoriesClone = categories.clone();
   DataCategory category = categoriesClone[0];
   DataCategory[] allCategories = new DataCategory[]{category};
   categoriesClone.remove(0);
   categoriesClone.addAll(category.getChildCategories());
   allCategories.addAll(getAllCategories(categoriesClone));
   return allCategories;
  }
 }
}

And last but not least ---- Visualforce page (MyPage)---

<apex:page cache="false"
  standardStylesheets="false"
  showHeader="false"
  sidebar="false"
  standardController="KnowledgeArticle"
  extensions="MyController"
  language="{!pkController.selectedLanguage}"
  applyBodyTag="false"
  applyHtmlTag="false"
  docType="html-5.0"
>
 <body>
  <c:MyComp pkController="{!MyController}" />
 </body>
<site:previewAsAdmin/>
</apex:page>

I'm getting this error message when attempting to compile the component in MavensMate: MyComp: Unknown method 'MyController.topTierCategorySelection()'

I edited the code for this post so some of the braces might be jacked! Any insight would be kindly appreciated.

  • After additional investigation found out a few things. 1) you don't need the ActionSupport tags to make the selectList postback the selected value in a VF page to controller interaction. 2) if you're using a component the selectList sends a null value to the controller when invoking the set. I've created an idea to change that. Please vote for it. success.salesforce.com/ideaView?id=0873A000000E4DTQA0 – jangiz Nov 3 '16 at 16:50
0

topTierCategorySelection is a String in your controller, not a method.

You would need to turn topTierCategorySelection into a method in order to call it from an action={!}.

That should get you past the error you are seeing.

  • Hmm. I see your point. Changing it to a method leaves me with a null value in the controller variable. Any suggestions for how I might draw a picklist and store the selected value in a way that is accessible to all page and components which load MyController. – jangiz Nov 1 '16 at 18:35
  • You appear to be using the action support to rerender the individual component. Is there a reason you are trying to use the action attribute? If you are just trying to reRender the section then remove the action={!} portion from the actionSupport. Currently, whenever you do change the pick list value it will assign to selectedCategory in your controller without that action. – Tyager Nov 1 '16 at 18:48
  • yeah. I was using the actionsupport element to simply set the variable in my controller after an 'onchange' event. At that point, the user can select a link (i.e. FAQ, ContactUs, etc...) and I would load a series of other components based on the parameters in the generated url for the link. The new link will reference the same controller. – jangiz Nov 1 '16 at 19:47
  • I was saying that you don't need the actionSupport to set the variable in your controller, the apex:selectList is doing that for you. Whenever you select one of the options in the select list it will assign it to pkController.selectedCategory. You can then use the actionSupport to rerender some section on the page on the onChange event. – Tyager Nov 1 '16 at 21:04
  • Understood. The outstanding issue is I can't redirect to a different page (with same controller) and access that selected value... I think it's because I'm updating a String and I need to update an object (passed by reference) to be able to maintain the changed value. – jangiz Nov 1 '16 at 21:20

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.