0

I have a dropdown list of Client__c object's records and a data table list of Question___c objects record with checkboxes on each record. when, I select a client from dropdown and select the questions by checking the checkboxes and save them, I need it to be saved as a record in another object ClientTeamQuestion__c

this is my code
Vf Page:

<apex:page standardController="Question__c" extensions="WrapperClassController" recordSetVar="questions" showHeader="false">
    <script>
        function clientName() {
            var temp = document.getElementById("ClientList");
            var selectedClients = temp.value();
        }
    </script>
    <apex:form>
        <apex:pageBlock title="ClientTeamQuestions">
            <apex:pageBlockSection title="Select The Client">
                <apex:selectList size="1" onselect="clientName()">
                    <apex:selectOptions value="{!ClientList}"></apex:selectOptions>
                </apex:selectList>
            </apex:pageBlockSection>

            <apex:pageBlockSection title="List Of Questions">
                <apex:pageBlockTable value="{!questions}" var="q">
                    <apex:column>
                        <apex:facet name="header">Select</apex:facet>
                        <apex:inputCheckbox value="{!q.Id}" />
                    </apex:column>
                    <apex:column value="{!q.name}" />
                    <apex:column value="{!q.Category__c}" />
                    <apex:column value="{!q.Questions__c}" />
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Save" action="{!saveQuestionList}" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Controller:

public with sharing class WrapperClassController {
    public WrapperClassController(ApexPages.StandardSetController controller) {}
    public List<Question__c> questionTemp {get;set;}
    public void questions() {
        questionTemp = new List<Question__c>();
        questionTemp = [SELECT Id, Name, Category__c, Questions__c FROM Question__c];
    }
    public List<Client__c> ClientTemp = new List<Client__c>();
    public List<SelectOption> ClientList {
        get {
            ClientTemp = [Select Name, Id From Client__c];
            ClientList = new List<SelectOption>();
            for (Client__c temp: ClientTemp) {
                ClientList.add(new SelectOption(temp.Id, temp.Name));
            }
            return ClientList;
        }
        set;
    }
    public void saveQuestionList() {}
}

pls help me with this code to save it

Thanks in advance.

enter image description here

1
  • I have attached my vf page here:
    – Sathesh
    Jul 6, 2016 at 10:45

1 Answer 1

1

You will need some new functions for that.

First I would define a merge variable for the selected client to be able to access it in controller:

public String selectedClient { get; set; }

Then use the value attribute of the selectList tag to "link" this variable with the selectList:

<apex:selectList size="1" value="{!selectedClient}">
    <apex:selectOptions value="{!ClientList}" />
</apex:selectList>

Now you can access selected client in the controller. Next we will need a wrapper class for the Question__c object to be able to get only selected questions while saving a new record:

public class Question{
    // Selected variable will be linked with a checkbox
    public Boolean selected { get; set; }
    // This is an sObject 
    public Question__c question { get; set; }
    // constructor
    public Question(Boolean selected, Question__c question){
        this.selected = selected;
        this.question = question;
    }
}

Creating a new list variable where the questions are stored:

public list<Question> questions { get; set; }

The method that reads questions could look like this:

public void readQuestions() {
    // new questions list
    questions = new list<Question>();
    // reading questions and adding it to the list to be displayed on the page
    for(Question__c q : [SELECT Id, Name, Category__c, Questions__c FROM Question__c]){
        questions.add( new Question(false,q) );
    }
}

And the last step is to implement the save method:

// Depending on the structure of the ClientTeamQuestion__c object you
// will have to customize the method
public void saveQuestionList() {
    // The object to be saved
    ClientTeamQuestion__c ctq = new ClientTeamQuestion__c();
    // adding selected client
    ctq.Client__c = selectedClient;
    // Getting only SELECTED questions
    for(Question q : questions){
        if(q.selected){
            // do your logic with selected question here
            ...
        }
    }
    // Inserting a new record
    insert ctq;
}

And do not forget to initialize variables in the constructor of the main class:

public WrapperClassController(ApexPages.StandardSetController controller) {
    selectedClient = null;
    questions = new list<Question>();
}

Finally the page block table should be rewritten (adding wrapper variables):

<apex:pageBlockTable value="{!questions}" var="q">
    <apex:column headerValue="Select">
        <apex:inputCheckbox value="{!q.selected}" />
    </apex:column>
    <apex:column value="{!q.question.name}" />
    <apex:column value="{!q.question.Category__c}" />
    <apex:column value="{!q.question.Questions__c}" />
</apex:pageBlockTable>

Putting it all together.

Page:

<apex:page standardController="Question__c" extensions="WrapperClassController" recordSetVar="questions" showHeader="false">
    <apex:form>
        <apex:pageBlock title="ClientTeamQuestions">
            <apex:pageBlockSection title="Select The Client">
                <apex:selectList multiselect="false" size="1" value="{!selectedClient}">
                    <apex:selectOptions value="{!ClientList}" />
                    <apex:actionSupport event="onchange"
                                        action="{!readQuestions}"
                                        rerender="questionsPanel"/>
                </apex:selectList>
            </apex:pageBlockSection>

            <apex:pageBlockSection title="List Of Questions" id="questionsPanel">
                <apex:pageBlockTable value="{!questions}" var="q">
                    <apex:column headerValue="Select">
                        <apex:inputCheckbox value="{!q.selected}" />
                    </apex:column>
                    <apex:column value="{!q.question.name}" />
                    <apex:column value="{!q.question.Category__c}" />
                    <apex:column value="{!q.question.Questions__c}" />
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Save" action="{!saveQuestionList}" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Class:

public with sharing class WrapperClassController {

    public String selectedClient { get; set; }
    public list<Question> questions { get; set; }

    public WrapperClassController(ApexPages.StandardSetController controller) {
        selectedClient = null;
        questions = new list<Question>();    
    }

    public class Question{
        public Boolean selected { get; set; }
        public Question__c question { get; set; }
        public Question(Boolean selected, Question__c question){
            this.selected = selected;
            this.question = question;
        }
    }

    public void readQuestions() {
        questions = new list<Question>();
        for(Question__c q : [SELECT Id, Name, Category__c, Questions__c FROM Question__c]){
            questions.add( new Question(false,q) );
        }
    }

    public List<Client__c> ClientTemp = new List<Client__c>();

    public List<SelectOption> ClientList {
        get {
            ClientTemp = [Select Name, Id From Client__c];
            ClientList = new List<SelectOption>();
            for (Client__c temp: ClientTemp) {
                ClientList.add(new SelectOption(temp.Id, temp.Name));
            }
            return ClientList;
        }
        set;
    }

    public void saveQuestionList() {
        ClientTeamQuestion__c ctq = new ClientTeamQuestion__c();

        ctq.Client__c = selectedClient;

        // Get all selected questions
        for(Quaetion q : questions){
            if(q.selected){
                // do your logic with selected question
                ...
            }
        }

        insert ctq;
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .