Questions about requirements or objectives should demonstrate thework or research you’ve done so far and ask aspecific question. Providing complete implementations based on a list of requirements is not a goal of this community. This question can be reopened when it is edited to include the needed information.
Closed3 years ago.
Can I write SOQL query in js file of LWC Comp. I don't want to do query in apex class. Is there any way for doing that?
- 2I suspect the answer to this is "no", and that Apex is required. You should re-frame your question to focus on the overall problem that you're trying to solve here (executing a query is a possible solution, but it isn't the overall problem you're trying to solve).Edit your question to add more details.Derek F– Derek F2022-08-10 13:27:34 +00:00CommentedAug 10, 2022 at 13:27
1 Answer1
No, you can't write a SOQL in the js file, but if the SOQL would have been something like "SELECTfields FROMobject WHERE Id = 'xxx'" or "SELECTfields FROMobject WHERE Id INlistOfIds"you can leverage eithergetRecord orgetRecords.
The first will retrieve data from a single records, while the latter will let you query multiple records at once, even from different objects.
Example 1: Retrieve the name of an Account and its owner's Name viagetRecord
import { LightningElement, wire, api } from 'lwc';import { getRecord, getFieldValue } from 'lightning/uiRecordApi';import NAME_FIELD from '@salesforce/schema/Account.Name';import OWNER_NAME_FIELD from '@salesforce/schema/Account.Owner.Name';export default class Example extends LightningElement { @api recordId; accountName; ownerName; @wire(getRecord, { recordId: '$recordId', fields: [NAME_FIELD, OWNER_NAME_FIELD]}) wiredAccount({data, error}) { if (data) { this.accountName = getFieldValue(data, NAME_FIELD); this.ownerName = getFieldValue(data, OWNER_NAME_FIELD); } else if (error) { // do something } }Example 2: Retrieve the name of an Account and First and Last Name of a Contact viagetRecords
import { LightningElement, wire, api } from 'lwc';import { getRecord, getFieldValue } from 'lightning/uiRecordApi';import ACCOUNT_NAME_FIELD from '@salesforce/schema/Account.Name';import FIRST_NAME_FIELD from '@salesforce/schema/Contact.FirstName';import LAST_NAME_FIELD from '@salesforce/schema/Contact.LastName';export default class Example extends LightningElement { accountIds = ['001xxx']; contactIds = ['003xxx']; accountName; contactFirstName; contactLastName; @wire(getRecords, { records: [ { recordIds: '$accountIds', fields: [ACCOUNT_NAME_FIELD] }, { recordIds: '$contactIds', fields: [FIRST_NAME_FIELD, LAST_NAME_FIELD] } ] }) wiredRecords({data, error}) { if (data) { this.accountName = getFieldValue(data.results[0], ACCOUNT_NAME_FIELD); this.contactFirstName = getFieldValue(data.results[1], FIRST_NAME_FIELD); this.contactLastName = getFieldValue(data.results[1], LAST_NAME_FIELD); } else if (error) { // do something } }For complex SOQL you need tocall an Apex method exposed via@AuraEnabled annotation.
- Thanks for sharing this! I need to created a super simple LWC that display's the user's account name. Having the query in the JS file did the trick without having to create an Apex class and an apex test. One thing I noticed though is that is slower to query the record than a SOQL query would be. With the query in the JS file, it takes about a second for the account name to populate in the LWC after the page loads. Just a heads up for anyone taking this approach in the future.Miguel Diaz– Miguel Diaz2023-12-06 23:38:06 +00:00CommentedDec 6, 2023 at 23:38
Explore related questions
See similar questions with these tags.