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
Dev RaulDev Raul 

Pass query parameters from component and display results

Hi all,
I would like to make a query through a lightning component.
This component should have some input parameters like fields to choose/insert (SELECT), object selection/input (FROM), and maybe a where condition.

Then, I would like to display the query result in another page.

I don't know how to insert parameters in a lightning component, pass them to a controller and make a query.

Is that possible? Could you give me some advise?

Thank you very much, any help would be very appreciated.

Raul

 
ravi soniravi soni
hi Dev,
try below code and fatch dynmic records of object by passing some parameters from Component and make query. you need to pass objectApiName and some fields name that you want to pass.
<aura:component controller="fetchAccount">
    <aura:handler name="init" value="{!this}" action="{!c.doInIt}"/>
</aura:component>
----------------------------------------------
//JS
({
	doInIt : function(component, event, helper) {
	    var action = component.get("c.getAccRecords");
            action.setParams({ objectApiName : 'Account',
                          fieldsApiName : 'Name,AccountNumber,AccountSource'});
        
        action.setCallback(this, function(response) {
            var state = response.getState();
            alert(state);
            if (state == "SUCCESS") {
                var result = response.getReturnValue();
                alert(JSON.stringify(result));
            }
            
                else if (state === "ERROR") {
                    var errors = response.getError();
                    if (errors) {
                        if (errors[0] && errors[0].message) {
                            console.log("Error message: " + 
                                        errors[0].message);
                        }
                    } else {
                        console.log("Unknown error");
                    }
                   }
        });
        $A.enqueueAction(action);
        
    
	}
})

------------------------------------------

//Here is apex class
public with sharing class fetchAccount {
   
    public static List<sObject> getAccRecords(string objectApiName,string fieldsApiName){
        string query = 'Select ' + fieldsApiName + ' FROM ' + objectApiName;
        system.debug('query---->'+query);
        /* If you Need where Condition then use below Line*/
        //query+= ' Where Name = \'abc\' ';
        List<sObject> lstSObject = database.query(query);
       
        system.debug('lstSObject---->'+lstSObject);
        return lstSObject;
    }
    
}
take reference from above code and don't forget to mark it as best answer.
Thank you
​​​​​​​
Dev RaulDev Raul
Hi Veer,
Thank you very much for your reply. I saw your code, but this does not allow me to input parameters and to have search results after, in another page. Could you help me also with that?
Thank you again.
Suraj Tripathi 47Suraj Tripathi 47
Hi Dev,

use my code to understand the flow between Component,JS,Class.

Que--> Create an aura component that will use to create a new account with five fields values.

Ans:

Step 1: Create a Lightning Component

<aura:component controller="AccountInsertController">
    <aura:attribute name="acc" type="Account" default="{'sObjectType': 'Account'}"/>
    <div>
        <div>
            <lightning:input label="Account Name" value="{!v.acc.Name}"/>
        </div>
        <div>
            <lightning:input label="Account Description" value="{!v.acc.Description}"/>
        </div>
        <div>
            <lightning:input label="Account Year Started" value="{!v.acc.YearStarted}"/>
        </div>
        
        <lightning:button label="Save" onclick="{!c.Save}"/>
        
    </div>
</aura:component>

Step 2: Create a Lightning Controller for the component

({
    Save : function(component, event, helper) {
        helper.help(component)
    }
})


step3: Create Helper Controller
({
    help : function(cmp){
        var action=cmp.get("c.insertAccount");
        action.setParams({acc : cmp.get("v.acc")});
        action.setCallback(this,function(response){
                var state=response.getState();
            if(state=="SUCCESS"){
                alert("From Server: " +response.getReturnValue());
                
            }
            else if(state=="ERROR"){
                var errors=response.getError();
                alert(JSON.stringify(errors));
            }
        });
        $A.enqueueAction(action);
    }
})


step 4 Create an Apex class and define the method to create the record

public class AccountInsertController {
      @AuraEnabled
    public static id insertAccount(Account acc){
        insert acc;
        return acc.id;
        
    }
    
}


Step 5: Create a Lightning application which will host the component in the App


<aura:application  extends="force:slds">
    <c:InsertNewAcc/>        
</aura:application>

If you find your Solution then mark this as the best answer.

Thank you!
Regards,
Suraj Tripathi