I have this method getFarmData being called from LWC
@AuraEnabled(cacheable=true)
public static List<RecordWrapper> getFarmData(String listView, Integer Count) {
if (listView== 'OWNER_FARMS') {
return getFarms();
}
if (listView== 'ALL_FARMS') {
return getAllFarms();
}
if (listView== 'VERIFIER_VISITS') {
return getVerifierVisits();
}
return null;
}
//this method is to get the list of farms owned by running user.
@TestVisible
private static List<RecordWrapper> getFarms() {
String user = UserInfo.getUserId();
String query = 'SELECT Id, Name, Farm_Owner__c, Verifier_Visits__c, CreatedDate FROM Farm__c' +
' WHERE Farm_Owner__c= :user' +
' ORDER BY CreatedDate DESC LIMIT 100';
List<Farm__c> farmData = Database.query(query);
List<RecordWrapper> results = new List<RecordWrapper>();
for (Farm__c f: farmData ) {
results.add(new RecordWrapper(f));
}
return results;
}
I also have a date range selection in the same lwc, by selecting the from & to dates, user should see the data for that particular range.
String query = 'SELECT Id, Name, Farm_Owner__c, Verifier_Visits__c, CreatedDate FROM Farm__c' +
' WHERE Farm_Owner__c= :user' AND (CreatedDate >+ :fromDate AND CreatedDate >+ :toDate +
' ORDER BY CreatedDate DESC LIMIT 100';
Can someone help me on how to make this happen on click of "Set Range" button??
Thank you
Update (LWC Code Snippet)
multiple listviews can be selected:
<lightning-combobox style="width: fit-content;" name="listview" value={listValue} options={listOptions} onchange={handleViewChange} ></lightning-combobox>
custom component with sorting and filtering:
<c-reusable-grid fields-list={fieldsList} grid-data={gridData.data} id-field-name="Id" records-per-page="20"
show-select-checkbox="false" >
</c-reusable-grid>
JS:
@track listValue = 'ALL_FARMS';
@track fromDate = null;
@track toDate = null;
@track fieldsList = [
{ name: "Farm_Owner", title: "Farm Owner", type: "text", filtering: true, sorting: true},
{ name: "Verifier_Visits__c", title: "Visits", type: "text", filtering: true, sorting: true},
{ name: "CreatedDate", title: "Created Date", type: "date", filtering: true, sorting: true}
]
@wire(getFarmData, { listView: '$listValue', Count: '$refreshCount' }) gridData;
get listOptions(){
return [
{label: 'All Farms', value: 'ALL_FARMS'},
{label: 'My Farms', value: 'MY_FARMS'},
{label: 'Verifier Visits', value: 'VERIFIER_VISITS'}
];
}
handleViewChange(event) {
this.listValue = event.detail.value;
console.log(event.detail.value);
}
Best Answer
There's no reason for dynamic queries here; Salesforce is smart enough to ignore criteria that doesn't make sense. You can simply use the following instead:
If you only plan on returning values on a certain "list view", don't call the server unnecessarily.
Pass in the start and end date values; if the values are null, they will be ignored in the query.
I included LIMIT 100, but is that really necessary? Does a user have more than 50,000 farms to deal with? Also, is the "wrapper" really necessary? It seems like a lot of overhead for no real reason; you can directly transform the results in your wire method or just use the field names as-is. Consider simplifying your code design.