0

Hi can anyone help on this,

My requirement after users selection of field, operator and value on each row on click of button it will able to call a dynamic search query with all these selected fields, operators and values. Am pasting the code and screenshot please go through once and provide the best solution.

Component:

<aura:iteration items="{!v.rowObjectList}" var="item">
    <lightning:layout horizontalAlign="space" verticalAlign="center" multipleRows="true">
        <lightning:layoutItem padding="horizontal-medium" >
            <lightning:select aura:id="selectedfield" label="" name="JobReqFields" 
                              onchange="{!c.handleChange}" value ='{!item.jobRecordField}' class="slds-m-bottom_medium">
                <option value="">-- Job Requisition Fields --</option>
                <aura:iteration items="{!v.jobrecordfield}" var="jobRecordField">
                    <option value="{!jobRecordField}">  {!jobRecordField} </option>   
                </aura:iteration>
            </lightning:select>        
        </lightning:layoutItem>
        <lightning:layoutItem padding="horizontal-medium" >
            <lightning:select aura:id="selectedoperator" label="" name="OperatorFields" value ='{!item.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 padding="horizontal-medium" >
            <lightning:input aura:id="EnteredText" name="EnteredText" type="text" class="slds-m-bottom_medium" value ='{!item.value}' >
            </lightning:input>                 
        </lightning:layoutItem>
        <lightning:layoutItem padding="horizontal-medium" >
            <div class="slds-p-left_medium slds-p-top_medium slds-p-bottom_medium">
                <lightning:button variant="brand" label="Add" title="Brand action" onclick="{!c.addRow}" />
            </div>     
        </lightning:layoutItem>
    </lightning:layout>
</aura:iteration>
<div>
    <lightning:button variant="brand" label="JobRequisitionSearch" title="Brand action" onclick="{!c.JobRequisitionSearch }"/>
</div>   

Controller.js:

({
    init: function(component, event, helper) {
        helper.createObjectData(component, event);
 },

handleChange: function (component, event) {
    console.log('inside handle change function')
    var jobrecordfield = component.get('v.jobrecordfield');
    var jobreqfield = event.getSource().get('v.value');
    component.set("v.jobreqfield",jobreqfield);
    console.log(jobreqfield);
},

addRow:function(component, event, helper){
    let button = event.getSource();
    console.log('Inside addRow function');
    var jobRecordFields = component.get('v.jobrecordfield');
    console.log(jobRecordFields);
    var operatorfields = ["Equal", "Not-Equal"];
    console.log(operatorfields);
    var valueFields = "";

    var rowObjectList = component.get("v.rowObjectList",rowObjectList);  
    var rowObj =  {
        jobRecordField:'',
        operator:'',
        value :''
    }
    rowObjectList.push(rowObj);

    component.set("v.rowObjectList",rowObjectList);
    button.set('v.disabled',true);
},

JobRequisitionSearch: function (component, event, helper) { 
    //console.log('Inside JobRequisitionSearch function');
    var rowObjectList= component.get("v.rowObjectList");
    console.log('rowObjectList are : ',rowObjectList);

    for(var i=0;i<rowObjectList.length;i++){
    var jobrecordfieldvalue = rowObjectList[i].jobRecordField;
    console.log(jobrecordfieldvalue);
    var operatorfieldvalue = rowObjectList[i].operator;
    console.log(operatorfieldvalue);
    var valuefield = rowObjectList[i].value;
    console.log(valuefield);
    }
    var action = component.get("c.SearchQueryMethod");
    //alert('after calling search criteria method');
    //console.log(component.get('v.jobRecordField'));
    action.setParams({ "jobRecordField":jobrecordfieldvalue,
                      "operator":operatorfieldvalue,
                      "value":valuefield
                     });
    //console.log('Jobrecord fields are: ',jobRecordField);
    //console.log(operator);
    //console.log(value);
    action.setCallback(this, function(response) {
        console.log(response);
        var state = response.getState();
        console.log(state);
        if (state === "SUCCESS") {
            if(response != null){

                var result = response.getReturnValue();
                console.log(result);

                component.set("v.SearchedRecords",result);     
                console.log( component.get("v.SearchedRecords"));
            }
        }

    });
    $A.enqueueAction(action);
}

});

Helper.js:

({
  createObjectData: function(component, event) {
    var action = component.get('c.getJobRequisitionFields');

    action.setParams({        });
    action.setCallback(this, function(response){ 
       var state = response.getState();

        if(state === 'SUCCESS' || state === 'DRAFT'){
            var rowObjList = [];
            var rowObj =  {
                jobRecordField:'',
                operator:'',
                value :''
            };
            //console.log(rowObj.length);
            console.log(Object.keys(rowObj).length);
            rowObjList.push(rowObj);
            console.log(rowObjList.length);
            console.log(rowObjList);
            component.set("v.rowObjectList",rowObjList);  
            var responseValue = response.getReturnValue();
            console.log('responseValue', responseValue);
            component.set("v.jobrecordfield",responseValue);
        }    
    });
    $A.enqueueAction(action);     
}

})

Apex Class:

public class JobRequisitionFields {  
@AuraEnabled
public static List<String> getJobRequisitionFields(){

     Schema.DescribeSObjectResult a_desc = Job_Requisition__c.sObjectType.getDescribe(); 
    List<String> jobReqFields =  new list<String>();
    for(String jobField : a_desc.fields.getMap().keySet()){         
         Schema.SObjectField field = a_desc.fields.getMap().get( jobField );
            Schema.DescribeFieldResult fieldLabel = field.getDescribe();
        //To get Label names of fields insead of Api Names
        jobReqFields.add(fieldLabel.getLabel());
        //jobReqFields.add(jobField);
    }
    System.debug(jobReqFields);
    return jobReqFields;
} 

@AuraEnabled
public static List<Job_Requisition__c> SearchQueryMethod(String jobRecordField,String operator,String value ){
    System.debug('Inside SearchQueryMethod');
    System.debug(jobRecordField);
    System.debug(operator);
    System.debug(value);

    String soqlQuery = 'SELECT Name FROM Job_Requisition__c where '+jobRecordField+'__c'+operator+':'+'value';
    List<Job_Requisition__c> jobreqList= database.query(soqlQuery);           
     System.debug('The result of query is: '+jobreqList);

     return jobreqList ;                     
 }

}

enter image description here

  • Please edit your question to describe the specific issue you are struggling with, and limit the code you include to only that which is directly related to the issue. SFSE cannot implement requirements on your behalf or provide complete solutions. – David Reed Sep 4 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.