You need to sign in to do that
Don't have an account?
Raghav T
How to display the return data of different types of objects separately in html.
This is Controller Class:
public with sharing class DataController {
@AuraEnabled(cacheable=true)
public static List<List<sObject>> getData(String keyword){
String searchquery = 'FIND \'' + keyword + '*\' IN ALL FIELDS RETURNING Account(Id, Name), Contact(Id, Name)';
List<List< SObject>> searchList = search.query(searchquery);
system.debug('Search List is ' + searchList);
Account[] searchAccounts = (Account[])searchList[0];
System.debug('searchAccounts'+searchAccounts);
Contact[] searchContacts = (Contact[])searchList[1];
System.debug('searchContacts'+searchContacts);
return searchList;
}
}
Thanks.
public with sharing class DataController {
@AuraEnabled(cacheable=true)
public static List<List<sObject>> getData(String keyword){
String searchquery = 'FIND \'' + keyword + '*\' IN ALL FIELDS RETURNING Account(Id, Name), Contact(Id, Name)';
List<List< SObject>> searchList = search.query(searchquery);
system.debug('Search List is ' + searchList);
Account[] searchAccounts = (Account[])searchList[0];
System.debug('searchAccounts'+searchAccounts);
Contact[] searchContacts = (Contact[])searchList[1];
System.debug('searchContacts'+searchContacts);
return searchList;
}
}
Thanks.
Please follow below code:-
Apex class:- DataController
Controller.js
Lightning component :-
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
Please follw below code:-
Component:- Controller.js:-
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
This is My DataController.cls:
public with sharing class DataController {
@AuraEnabled(cacheable=true)
public static List<List<sObject>> getData(String keyword){
WraperForStorage wrperObj = new WraperForStorage();
String searchquery = 'FIND \'' + keyword + '*\' IN ALL FIELDS RETURNING Account(Id, Name, Phone, Industry, Website), Contact(Id, Name, Phone, Email)';
List<List< SObject>> searchList = search.query(searchquery);
system.debug('Search List is ' + searchList);
Account[] searchAccounts = (Account[])searchList[0];
System.debug('searchAccounts'+searchAccounts);
Contact[] searchContacts = (Contact[])searchList[1];
System.debug('searchContacts'+searchContacts);
wrperObj.acntsLst = searchAccounts;
wrperObj.cntsLst = searchContacts;
return wrperObj;
}
Public class WraperForStorage{
@AuraEnabled Public List<Account> acntsLst {get;set;}
@AuraEnabled Public List<Contact> cntsLst {get;set;}
// Public WraperForStorage(){}
}
}
And This is My soslComponenetSearch.js:
import { LightningElement, track } from 'lwc';
import getData from '@salesforce/apex/DataController.getData';
export default class SoslComponentSearch extends LightningElement {
key;
@track allData;
@track error;
searchVal(event) {
this.key = event.target.value;
console.log('The Key value : '+this.key);
}
Acc_cols = [
{ label: 'Account Name', fieldName: 'Name', type: 'text' },
{ label: 'Phone', fieldName: 'Phone', type: 'phone' },
{ label: 'Industry', fieldName: 'Industry', type: 'text' },
{ label: 'Website', fieldName: 'Website', type: 'URL' }
];
Con_cols = [
{ label: 'Contact Name', fieldName: 'Name', type: 'text' },
{ label: 'Phone', fieldName: 'Phone', type: 'Phone' },
{ label: 'Email', fieldName: 'Email', type: 'Email' }
];
handleSearch(event){
if (this.key) {
console.log('Key '+this.key);
getData({keyword : this.key})
.then(result => {
console.log('inside out result...');
this.allData = result;
console.log('I am here',this.allData);
})
.catch(error => {
console.log('Time for error...');
this.error = error;
});
} else {
console.log('Undefined data...');
this.allData = undefined;
}
}
}
when i try to console.log(allData) it show Undefined in console.
And also in html i am unable to get results. Please help me out.
And This is My HTML code:
<template>
<lightning-card title='Search Data'>
<lightning-layout multiple-rows="true" vertical-align="end">
<lightning-layout-item size="4" padding="around-small">
<lightning-input type="search" label="Enter Account Name" value={key} onchange={searchVal} ></lightning-input>
</lightning-layout-item>
<lightning-layout-item size="3" padding="around-small">
<lightning-button label="Search" onclick={handleSearch} variant="brand"></lightning-button>
</lightning-layout-item>
</lightning-layout>
</lightning-card>
<lightning-card>
<lightning-layout>
<lightning-layout-item size="12" padding="around-small">
<lightning-datatable
key-field="id"
data={allData.acntsLst}
columns={Acc_cols}
hide-checkbox-column="true">
</lightning-datatable>
</lightning-layout-item>
</lightning-layout>
<lightning-layout>
<lightning-layout-item size="12" padding="around-small">
<lightning-datatable
key-field="id"
data={allData.cntsLst}
columns={Con_cols}
hide-checkbox-column="true">
</lightning-datatable>
</lightning-layout-item>
</lightning-layout>
</lightning-card>
</template>
Thanks.
You need use @track key;
and you can use
this.allAccData = result.acnLst ;
this.allConData = result.cntsLst;
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh