1

I'm just trying to see some Event's data in the Console window but unable to figure out where am going wrong. I'm sure its a silly mistake, please help!

import { LightningElement, wire } from 'lwc';
import { getRecords } from 'lightning/uiRecordApi';
import SUBJECT_FIELD from '@salesforce/schema/Event.Subject';

export default class TestLWC extends LightningElement {
    @wire(getRecords, { 
        recordIds: ['00UHr00001KSgP9MAL'], //brand new record created
        fields: [SUBJECT_FIELD]
    })
    loadEvents({ error, data }) {
        if (data) {
            console.log('data: '+data); //never reaches here
        } else if (error) {
            console.log('error: '+error); //never reaches here
        }
        console.log('data: '+data); //undefined
    }
}

2 Answers 2

2

The documentation is somewhat misleading if you're not paying attention. getRecords receives an array of objects that specify a record Id and the fields to retrieve. If you scroll down, you'll see this in the example. To fix this, change your code as follows:

    @wire(getRecords, { records: [
        { recordIds: ['00UHr00001KSgP9MAL'], //brand new record created
          fields: [SUBJECT_FIELD]
        }
      ]
    })

Notice how we add a records property that's an array, and in the array, we have objects that specify fields and record Ids. This allows us to retrieve multiple records of multiple data types in a single call.

2
  • Even ChatGPT doesn't know this, you're awesome!! I'm getting this error now, so I'm assuming my only option is to get the Events using Apex? data: {"results":[{"statusCode":400,"result":[{"errorCode":"INVALID_TYPE","message":"Object Event is not supported in UI API"}]}]}
    – apz
    Commented May 8 at 14:44
  • 1
    @apz Yes, I hadn't thought about that, but not all object types can be retrieved through the wire service. You'll have to check out the docs for specific unsupported objects.
    – sfdcfox
    Commented May 8 at 15:10
2

You are trying to wire a function to getRecords in LWC. The issue seems to be with the getRecords wire adapter usage. The getRecords method is used to retrieve multiple records.

  • Changed getRecords to getRecord because you are retrieving a single record.
  • Changed recordIds to recordId because getRecord takes a single ID, not an array.
import { LightningElement, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi'; 
import SUBJECT_FIELD from '@salesforce/schema/Event.Subject';

export default class TestLWC extends LightningElement {
        @wire(getRecord, { 
                recordId: '00UHr00001KSgP9MAL', 
                fields: [SUBJECT_FIELD]
        })
        loadEvents({ error, data }) {
                if (data) {
                        console.log('data: ', data); 
                } else if (error) {
                        console.log('error: ', error);
                }
        }
}
1
  • Are you saying the getRecords doesn't support one recordId in a string array? Ideally, I want to get multiple records without using Apex. The code in the question was just an example.
    – apz
    Commented May 8 at 13:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .