0

My form has a parent drop down first and based on the selected dropdown a set of questions get displayed and iam trying to display this questions dynamically by creating parent and child objects in salesforce. Here is how the child record looks like enter image description here

There can be multiple picklist data type questions like above under the main question. I need to be able to get the picklist values based on the specific question displayed. I got the list of child records and was able to display question label but not sure how to get the picklist values. Sorry new to lightning. Any help is appreciated.

I need to be able to retrieve the question and the picklist values associated to it on the record like shown in below image. Note: Its not an actual picktlist data type field but a Text area field with values separated by semi colon. So i need to get that text area field and make a list of dropdown values for that child question.

[![enter image description here][2]][2]

Lightning component:

<aura:iteration items="{!v.childQuestionValues}" var="child">
                  <aura:if isTrue="{!child.Data_Type__c == 'Picklist'}">
           <lightning:select aura:id="{!child.id}" name="picklistQuestionType" label="{!child.Question_Label__c}" 
                             required="{!child.Required__c}" value ="{!v.selChildPicklistValue}"
                               onchange="{!c.handleChange}" messageWhenValueMissing="You must select one of the option to create a case">
               <option label = "Please select.." value=""/>  
       **<need the dropdown options here associated to each child question>**
               </aura:iteration>
                   </lightning:select>

Controller:

showChildQuestions : function(component, event, helper) {
   console.log('The selected question type is: ' +component.get("v.selQuestionType"));
    var selQuestion = component.get("v.selQuestionType");

    if(selQuestion != ""){
        component.set("v.displayDropdown1", true);
       helper.getChildQuestions(component);
    helper.getSortedPicklistValues(component);

    }
}

Helper:

getChildQuestions : function(component){
       var selMainQuestion = component.get("v.selQuestionType");
    console.log(selMainQuestion);
    var action = component.get("c.getChildQuestions");
    action.setParams({mainQuestionValue : selMainQuestion});
    action.setCallback(this, $A.getCallback(function (response) {
     var state = response.getState();

    if(state === "SUCCESS"){

        var childOptions = response.getReturnValue();
        console.log('getChildQuestions returned: ' +childOptions);                
        component.set("v.childQuestionValues", response.getReturnValue());

    } else if (state === "ERROR") {
        //otherwise write errors to console for debugging
        alert('Problem with connection. Please try again.');

           }

}));

 $A.enqueueAction(action);
 }

apex controller method:

  @AuraEnabled
public static List<Child_Webform_Questions__c> getChildQuestions(String mainQuestionValue) {
    List<Child_Webform_Questions__c> childquestions = [Select id, Data_Type__c,Field_Value__c,Picklist_Values__c,Main_Question_Value__c,
                                                       Question_Label__c,Webform_Main_Question__c,Place_Holder__c,Order__c from Child_Webform_Questions__c 
                                                       where Main_Question_Value__c = :mainQuestionValue Order by Order__c];
      return childquestions;
}
  • Is childQuestionValues declared in your component a List? – Jayant Das Nov 1 '18 at 19:47
  • Yes <aura:attribute name="childQuestionValues" Type="List" access="global"/> <aura:attribute name="mapvalues" Type="List" access="global"/> – Keerthi Nov 1 '18 at 19:52
  • Can you specify, where exactly are you facing issue? As what is not working? – Jayant Das Nov 1 '18 at 19:55
  • So once i select the parent question , the list of child questions show up . child questions are records in salesforce. The picklist child question is created as shown above in my question. In that child questions i need be able to populate picklist values if the data type of the child question is picklist. iam unable to populate those picklist values for that particular child question that populated .I can add an image of the form but not sure how to do it here.. – Keerthi Nov 1 '18 at 20:01
  • Updated the question with the Image of the form. I need to be able to populate the dependent dropdown list for Select address type and for other similar dependent child questions. – Keerthi Nov 1 '18 at 20:26
0

You need to parse out the individual values from the semicolon-delimited string in Picklist_Values__c. You could do this in your javascript controller, but I think it's simpler to do it in Apex.

Use a wrapper class so you can return the picklist values as a list of strings, along with the Child_Webform_Question records.

Apex controller:

@AuraEnabled
public static List<QuestionWrapper> getChildQuestions(String mainQuestionValue) {

    List<Child_Webform_Questions__c> childquestions = [Select id,       
                                                              Data_Type__c,
                                                              Field_Value__c,
                                                              Picklist_Values__c,
                                                              Main_Question_Value__c,
                                                              Question_Label__c,
                                                              Webform_Main_Question__c,
                                                              Place_Holder__c,
                                                              Order__c 
                                                         From Child_Webform_Questions__c
                                                        Where Main_Question_Value__c = :mainQuestionValue
                                                     Order by Order__c];

    List<QuestionWrapper> questions = new List<QuestionWrapper>();

    for(Child_Webform_Questions__c q : childquestions){
        questions.add(new QuestionWrapper(q));
    }

    return questions;

}

public class QuestionWrapper {
    public List<Child_Webform_Questions__c> question;
    public List<String> picklistValues;

    public questionWrapper(Child_Webform_Questions__c cq){
        this.question = cq;
        this.picklistValues = new List<String>();

        if(cq.Data_Type__c = 'Picklist'){
            picklistValues = parsePicklistVals(cq.Picklist_Values__c);

        }
    }

    private String parsePicklistVals(String vals){
        List<String> picklistValues = vals.split(';');

        return picklistValues;

    }

}

In your component, iterate over the picklistValues to create select options when the QuestionType is 'Picklist'.

<aura:iteration items="{!v.questionWrappers}" var="child">
    <aura:if isTrue="{!child.question.Data_Type__c == 'Picklist'}">
    <lightning:select aura:id="{!child.question.id}" 
                      name="picklistQuestionType" 
                      label="{!child.question.Question_Label__c}"
                      required="{!child.question.Required__c}" 
                      value ="{!v.selChildPicklistValue}"
                      onchange="{!c.handleChange}" 
                      messageWhenValueMissing="You must select one of the option to create a case">
        <option label = "Please select.." value=""/>  

        <aura:iteration items="{!child.picklistValues}" var="value">
            <option label="{!value}" value="{!value}"/>
        </aura:iteration>

    </lightning:select>
</aura:iteration>

Note that you'll need to update your javascript and change the attribute name "childQuestionValues" to "questionWrappers" to account for the changes here.

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.