3

I have an sObject custom_obj__c with a multi-select picklist field items_list__c and its values (items) are (item1 list, item2 list, item3 list,..), and checkbox fields (item1_list__c, item2_list__c, item3_list__c,..) for each item in the picklist. I need do display the checkboxes on my visualforce page which have the same values selected in the picklist.

In my controller I've got the selected items from the picklist and stored them in a list of strings and I updated the values to replace spaces with underscore _ and append __c at the end of the string. This list I made to build the query to get the checkboxes.

String selectedItemsQuery = 'item1_list__c,item2_list__c';
String finalQuery = 'SELECT '+ selectedItemsQuery +' FROM custom_obj__c WHERE Id=\''+ObjId+'\';

and then I execute a select query statement to get record value

custom_obj__c targetObj = Dabase.query(finalQuery);

So now I've got the values of selected checkboxes, how can I display only these checkboxes and not the whole list of checkboxes on the visualforce page??

<apex:pageBlock>
     <apex:pageBlockSection title="List of checkboxes selected from list">
         <!-- Checkboxes inputFields should display here -->

     </apex:pageBlockSection>
</apex:pageBlock>
3

You can create a list of the selectedItemsQuery and then use it in the vf page, Something like this :

public list<String> selectedCheckboxFieldApiNames{get;set;}

In your method then,

selectedCheckboxFieldApiNames = new list<String>();

String selectedItemsQuery = 'item1_list__c,item2_list__c';
String finalQuery = 'SELECT '+ selectedItemsQuery +' FROM custom_obj__c WHERE Id=:'+ObjId;
custom_obj__c targetObj = Dabase.query(finalQuery);

selectedCheckboxFieldApiNames.addAll(selectedItemsQuery.split(','));

Your vf page code would like this:

<apex:pageBlock>
     <apex:pageBlockSection title="List of checkboxes selected from list">
         <!-- Checkboxes inputFields should display here -->
         <apex:repeat value="{!selectedCheckboxFieldApiNames}" var="fielName">
             <Apex:inputField value="{!targetObj[fielName]}"/>
         </apex:repeat>
    </apex:pageBlockSection>
</apex:pageBlock>
| improve this answer | |
  • Humm, I got error I don't think related to my issue it may cause of dynamic soql, System.QueryException: Variable does not exist: a0Z25000001Vq02EAC , when I changed query to 'SELECT '+ selectedItemsQuery +' FROM custom_obj__c limit 1' it works ! – user3003810 Jan 27 '16 at 11:23
  • I got it, it was because the string variable inside the query statement, it may need to set the value of it inside method scope, but i but it like this Id=\''+ObjId+'\' – user3003810 Jan 27 '16 at 11:34

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.