function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sampath Reddy 15Sampath Reddy 15 

How to get the picklist values from wrapper (Fieldset) to lightning component controller

Apex:
public class JobRequisitionFields {  
    private static String WhereClause;
    public class SearchResult
    {
        @AuraEnabled public List<Job_Requisition__c> jobRequisitionList;
        @AuraEnabled public List<FieldDetails> fieldSetdetails;
        @AuraEnabled public List<JobSearchWrapper> jobSearchWrapper;
    }
    public class FieldDetails
    {
        @AuraEnabled public string fieldType{ get; set; }
        @AuraEnabled public string fieldAPIName{ get; set; }
        @AuraEnabled public string  fieldLabel{ get; set; }
        @AuraEnabled List<PickListOption> pickListOptionList {get;set;}
    }
    public class PickListOption
    {
        @AuraEnabled public string label {get; set; }
        @AuraEnabled public string value {get; set; }
    }
    public class JobSearchWrapper 
    {
        @AuraEnabled public String jobRecordField{set;get;}
        @AuraEnabled public String operator{set;get;}
        @AuraEnabled public String value{set;get;}
        @AuraEnabled public String fielddatatype{set;get;}
    }   
    @AuraEnabled
    public static List<FieldDetails> getJobRequisitionFields(String jobrecordField)
    { 
        system.debug('Field value inside getJobRequisitionFields() is: '+jobrecordField);
        Schema.DescribeSObjectResult a_desc = Job_Requisition__c.sObjectType.getDescribe(); 
        List<FieldDetails> fieldDetailsList = new List<FieldDetails>(); 
        for(String jobField : a_desc.fields.getMap().keySet()){ 
             PickListOption pickListOption = new PickListOption();
            if(jobField.contains('__c') || jobField == 'Name' ){
                Schema.SObjectField field = a_desc.fields.getMap().get( jobField );
                Schema.DescribeFieldResult fieldResult = field.getDescribe();
                FieldDetails fieldDetails = new FieldDetails();
                fieldDetails.fieldAPIName =fieldResult.getName();
                fieldDetails.fieldLabel = fieldResult.getLabel();
                fieldDetails.fieldType = String.valueOf(fieldResult.getType());
                fieldDetailsList.add(fieldDetails); 
                system.debug('Fieldtype inside getJobRequisitionFields is: '+fieldDetails.fieldType);
                system.debug(fieldDetails.fieldAPIName);
                if(fieldDetails.fieldType == 'PICKLIST'){
                system.debug('Inside if '+fieldResult.getPicklistValues());
                    list<PickListOption> pickListOptionList = new list<PickListOption>();
                    list <Schema.PicklistEntry> values = fieldResult.getPickListValues();
                   for (Schema.PicklistEntry picklist: values) {
                           pickListOption.label = picklist.getLabel();
                            pickListOption.value = picklist.getValue();
                            system.debug('@@@@@@@@   '+pickListOption.value);
                            pickListOptionList.add(pickListOption);
                        }
                    system.debug('*****pickListOptionList '+pickListOptionList);  
                }else{
                    pickListOption.value =' ';
                }
            
            }
        }
        System.debug('22222222222222222222222222  '+fieldDetailsList);
        return fieldDetailsList;
    }
    
    @AuraEnabled  
    public static List<PickListOption> findPicklistOptions(string pickListVal) {
        list<PickListOption> pickListOptionList = new list<PickListOption>();
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Schema.sObjectType objType = schemaMap.get(pickListVal);
        Schema.DescribeSObjectResult objDescribe = objType.getDescribe();
        map < String, Schema.SObjectField > fieldMap = objDescribe.fields.getMap();
        list < Schema.PicklistEntry > values = fieldMap.get(pickListVal).getDescribe().getPickListValues();
        PickListOption picklistDetails = new PickListOption();
        for (Schema.PicklistEntry picklist: values) {
            PickListOption pickListOption = new PickListOption();
            pickListOption.label = picklist.getLabel();
            pickListOption.value = picklist.getValue();
            pickListOptionList.add(pickListOption);
        }
        system.debug('*****pickListOptionList'+pickListOptionList);
        return pickListOptionList;    
    }
    
    @AuraEnabled
    public static List<FieldDetails> getFields() 
    {
        system.debug('inside the field sets ');
        List<FieldDetails> fieldDetailList = new List<FieldDetails>();
        for(Schema.FieldSetMember fieldMember : SObjectType.Job_Requisition__c.FieldSets.JobSearchFieldset.getFields()){ 
            system.debug(fieldMember);
            FieldDetails fieldDetails= new FieldDetails();
            fieldDetails.fieldType = String.valueOf(fieldMember.getType());
            fieldDetails.fieldAPIName = fieldMember.getFieldPath();
            fieldDetails.fieldLabel = fieldMember.getLabel();
            fieldDetailList.add(fieldDetails);
        }
        system.debug(fieldDetailList);
        return fieldDetailList;
    }
    //Wrapper class to hold Columns with headers
    public class DataTableColumns 
    {
        @AuraEnabled
        public String label {get;set;}
        @AuraEnabled       
        public String fieldName {get;set;}
        @AuraEnabled
        public String type {get;set;}
        
        //Create and set three variables label, fieldname and type as required by the lightning:datatable
        public DataTableColumns(String label, String fieldName, String type){
            this.label = label;
            this.fieldName = fieldName;
            this.type = type;            
        }
    }
    //Wrapper calss to hold response - This response is used in the lightning:datatable component
    public class DataTableResponse 
    {
        @AuraEnabled
        public List<DataTableColumns> lstDataTableColumns {get;set;}
        @AuraEnabled
        public List<sObject> lstDataTableData {get;set;} 
        @AuraEnabled 
        public List<FieldDetails> fieldSetdetails;
        public DataTableResponse(){
            lstDataTableColumns = new List<DataTableColumns>();
            lstDataTableData = new List<sObject>();
            fieldSetdetails = new List<FieldDetails>();
        }
    }
    
    @AuraEnabled
    public static DataTableResponse SearchQueryMethod(List<JobSearchWrapper> jobSearchWrapperList)
    {
        System.debug('Inside SearchQueryMethod'+jobSearchWrapperList);
        List<string> conditions = new List<string>();
        Schema.SObjectType SObjectTypeObj = Schema.getGlobalDescribe().get('Job_Requisition__c');
        Schema.DescribeSObjectResult DescribeSObjectResultObj = SObjectTypeObj.getDescribe();
        Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get('JobSearchFieldSet');
        /* Schema.DescribeSObjectResult res=globalDescribe.get('Job_Requisition__c').getDescribe();  
Map<String, Schema.FieldSet> fieldSetMap= res.fieldSets.getMap();  
Schema.FieldSet jobFieldSet = fieldSetMap.get('JobSearchFieldSet');*/
        
        FieldDetails fieldDetails= new FieldDetails();
        //string selectfields = 'select Id, Name';
        WhereClause = 'select Id, Name,Skill_Set__c,Work_Experience__c,State__c,Start_Date__c,Job_Type__c,Industry__c,Country__c,City__c from Job_Requisition__c where Id != null'; 
        // WhereClause = 'select :jobSearchFields from Job_Requisition__c where Id != null';
        //To hold the table hearders 
        List<DataTableColumns> lstDataColumns = new List<DataTableColumns>();
        Map<string,DataTableColumns> DataColumnsMap = new Map<string,DataTableColumns>();
        //Field to be queried - fetched from fieldset
        List<String> lstFieldsToQuery = new List<String>();
        //The final wrapper response to return to component
        DataTableResponse response = new DataTableResponse();
        for( Schema.FieldSetMember eachFieldSetMember : fieldSetObj.getFields())
        {
            String dataType = String.valueOf(eachFieldSetMember.getType()).toLowerCase();
            system.debug('The datatype of field inside searchQuery() is : '+dataType);
            //This way we can set the type of a column
            //We do not get the exact type from schema object which matches to lightning:datatable component structure
            if(dataType == 'datetime'){
                dataType = 'date';
            }
            //Create a wrapper instance and store label, fieldname and type.
            DataTableColumns datacolumns = new DataTableColumns( String.valueOf(eachFieldSetMember.getLabel()) , 
                                                                String.valueOf(eachFieldSetMember.getFieldPath()), 
                                                                String.valueOf(eachFieldSetMember.getType()).toLowerCase() );
            lstDataColumns.add(datacolumns);
            //DataColumnsMap.put(String.valueOf(eachFieldSetMember.getFieldPath()),datacolumns);
        }
       for(JobSearchWrapper jobWrapper:jobSearchWrapperList)
        {
            System.debug('Inside for loop of SearchQueryMethod '+jobWrapper);
            if(jobWrapper.jobRecordField!=null && jobWrapper.jobRecordField!= '' && jobWrapper.operator!=null && jobWrapper.operator!= '' 
               && jobWrapper.value!=null && jobWrapper.value!= ''){
                   if(jobWrapper.jobRecordField == 'Start_Date__c' ||jobWrapper.jobRecordField ==     'End_Date__c' ){
                       conditions.add(jobWrapper.jobRecordField  + jobWrapper.operator + jobWrapper.value);
                   }else
                       conditions.add(jobWrapper.jobRecordField  + jobWrapper.operator + '\'' + jobWrapper.value + '\'');                  
               }
      }
        system.debug('size is: '+conditions.size());
        if (conditions.size() > 0) {
            for (Integer i = 0; i < conditions.size(); i++){
                WhereClause +=  '  AND '  + conditions[i] ;                                     
            }       
        }
        system.debug('WhereClause-----------'+WhereClause);    
        List<Job_Requisition__c> whereclauseDemo = Database.query(WhereClause);
        System.debug('Result of Query is '+whereclauseDemo);
        for(Job_Requisition__c newrecord : whereclauseDemo){
            system.debug(newrecord.Name);
        }
        system.debug('WhereClause-----------'+WhereClause);        
        response.lstDataTableColumns = lstDataColumns;
        response.lstDataTableData = Database.query(WhereClause);
        return response ;
    }
    @AuraEnabled
    public static Map<String, String> fieldPickListValues(String jobField)
    {
        system.debug('We are in getFieldType method'+jobField);
        //String dataType = '';
        system.debug('field inside getFieldType method is: '+ jobField);
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Schema.SObjectType leadSchema = schemaMap.get('Job_Requisition__c');
        Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();
        system.debug(fieldMap);
        Map<String, String> options = new Map<String, String>();
        List<Schema.PicklistEntry> pick_list_values = New List<Schema.PicklistEntry>();
          pick_list_values = fieldMap.get(jobField).getDescribe().getPickListValues(); //grab the list of picklist values for the passed field on the sobject
        for (Schema.PicklistEntry a : pick_list_values) { //for all values in the picklist list
            options.put(a.getLabel(), a.getValue()); 
        }
        return options;
    }    
}
Sampath Reddy 15Sampath Reddy 15
Component:
<aura:component controller="JobRequisitionFields" implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global">
    <aura:attribute name="rowobject" type="object"/>
    <aura:attribute name="rowObjectList" type="List[]" />
    <aura:attribute name="fieldDetailList" type="object[]" />
    <aura:attribute name="jobreqfield" type="String" default="jobreqfield"/>
    <aura:attribute name="fieldDataType" type="String" />
    <aura:attribute name="picklistvalues" type="Map" />
    <aura:attribute name="options" type="List"/>
    <aura:attribute name="rowIndex" type="String" />
    <aura:registerEvent name="AddRowName" type="c:AddRow"/>
    <lightning:layout horizontalAlign="space" verticalAlign="center" multipleRows="true">
        <lightning:layoutItem size="3" padding="horizontal-medium">
            <lightning:select aura:id="selectedfield" label="" name="JobReqFields" 
                              onchange="{!c.handleChange}" value ='{!v.rowobject.jobRecordField}' class="slds-m-bottom_medium ">
                <option value="">-- Job Requisition Fields --</option>
                <!--attribute list-->
                <aura:iteration items="{!v.fieldDetailList}" var="jobrecfield">
                    <option value="{!jobrecfield.fieldAPIName}">  {!jobrecfield.fieldLabel} </option>                    
                </aura:iteration>
            </lightning:select>    
        </lightning:layoutItem>
        <lightning:layoutItem size="3" padding="horizontal-medium" >
            <lightning:select aura:id="selectedoperator" label="" name="OperatorFields" value ='{!v.rowobject.operator}'
                              class="slds-m-bottom_medium">
                <option value="">-- Operator Feilds --</option>
                <option value="=">Equal</option>
                <option value="!=">Not Equal</option>        
            </lightning:select>
        </lightning:layoutItem>
        <lightning:layoutItem size="3" padding="horizontal-medium" >
            <aura:if isTrue="{!v.fieldDataType == 'STRING'}">
                <lightning:input aura:id="EnteredText" name="EnteredText" type="text" class="slds-m-bottom_medium" value ='{!v.rowobject.value}' >
                </lightning:input>
            </aura:if>
            <aura:if isTrue="{!v.fieldDataType == 'DATE'}">
                <lightning:input aura:id="date" type="date" value="{!v.rowobject.value}"  />
            </aura:if>
            <aura:if isTrue="{!v.fieldDataType == 'PICKLIST'}">
                <lightning:select aura:id="picklist"  class="slds-m-bottom_medium" >
                    <option value="">--None--</option>
                    <aura:iteration items="{!v.picklistvalues}" var="picklistval" indexVar="key">
                        <option text="{!picklistval.value}" value="{!picklistval.key}" />
                    </aura:iteration>
                </lightning:select>
            </aura:if> 
        </lightning:layoutItem >
        <lightning:layoutItem size="3" padding="horizontal-medium">
        <div class="slds-show" aura:id="addButton">
            <lightning:button aura:id="addRowid" variant="brand" label="Add" title="Brand action" onclick="{!c.addRow}" />
        </div>
        </lightning:layoutItem>
    </lightning:layout>        
</aura:component>
 
Sampath Reddy 15Sampath Reddy 15
({
    handleChange: function (component, event, helper) {
        
        console.log('------------------------inside handle change function');
       
        var jobrecordfieldlist = component.get('v.fieldDetailList');
        console.log('jobrecordfieldlist values are',jobrecordfieldlist);
        
        var pickListValues = component.get('v.picklistvalues');
          console.log('pickListValues values are',pickListValues);
        
        var jobreqfield = event.getSource().get('v.value');
        console.log('jobreqfield value inside handleChange ',jobreqfield);
        component.set("v.jobreqfield",jobreqfield);
        for(var i=0;i<jobrecordfieldlist.length;i++){
                       
            jobrecordfieldlist.fieldAPIName = jobreqfield;
            if((jobrecordfieldlist[i].fieldAPIName) == jobreqfield){
                console.log('we are in if ',jobrecordfieldlist[i].fieldType);
                component.set("v.fieldDataType",jobrecordfieldlist[i].fieldType);
                
                console.log('=========== ',component.get('v.fieldDataType'));
               
            }
        }
        
        //Need to get the picklist values from wrapper no need to call the apex class need to get those values on fieldset wrapper
       /* var action = component.get("c.fieldPickListValues");
        console.log('value of jobrecfield is: ',jobreqfield);
        action.setParams({
            "jobField": jobreqfield   
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var industryMap = [];
                var result = response.getReturnValue();
                
                for(var key in result){
                    industryMap.push({key: key, value: result[key]});
                }
                component.set("v.picklistvalues",industryMap);
                
                var picklistvalues = component.get('v.picklistvalues');
                console.log('--------picklistvalues----'+picklistvalues);
            } 
        });
        $A.enqueueAction(action);*/
        //helper.getfieldDatatypes(component, event);
        //var typeOfField = component.get('v.fieldDataType');
        //console.log('field type inside handle change is: ',typeOfField);
        //from the jobrecordfieldlist we have to get the jobreqfield datatype
    },
    
    addRow:function(component, event, helper){
        let button = event.getSource();
        console.log('Inside addRow function');
        var jobRecordFields = component.get('v.jobrecordfieldList');
        console.log(component.get('v.jobrecordfieldList'));
        //console.log(jobRecordFields);
        var operatorfields = ["Equal", "Not-Equal"];
        console.log(operatorfields);
        
        //console.log('row index value in addrow() ',component.get("v.rowIndex"));
        var rowObjectList = component.get("v.rowObjectList",rowObjectList);  
        var rowObj =  {
            jobRecordField:'',
            operator:'',
            value :'',
            fielddatatype:''
        }
        // console.log('type of field is : '+typeOfField);
        console.log('Values in rowObj is : ',rowObj);
        
        rowObjectList.push(rowObj);
        component.set("v.rowObjectList",rowObjectList);
        
        var cmpEvent = component.getEvent("AddRowName");
        cmpEvent.setParams({"row": component.get("v.rowObjectList")});
        console.log('Before Event Fire');
        var addBut = component.find("addButton");
        $A.util.toggleClass(addBut, "slds-hide");
        cmpEvent.fire();
        console.log('After Event Fired');
       //button.set('v.disabled',true);   
        
        }
   
})