0

tl; dr - How do I access a custom field value on the Event object via lwc?

Update at bottom of page

Overview

I am attempting to create a lightning web component that displays the field values of a record related to an Event record. Initial testing with a hard-coded record Id was successful, but attempts to dynamically acquire the record Id from a lookup field on the Event record have not been.

In my Html file, I created two lines for testing purposes to show the values I am looking for.

MeetingId : --|{meetingId}|--
MeetingNoteId : --|{meetingNoteId}|--

When the {meetingNoteId} is commented out, {meetingId} populates fine. But when {meetingNoteId} is not commented out, neither field populates. The field that {meetingNoteId} is capturing is a custom field called Meeting_Notes__c, a lookup field to a custom object. Because it is a custom field, it is technically on the Activity object, but "@salesforce/schema/Activity.Meeting_Notes__c" causes an error, whereas "@salesforce/schema/Event.Meeting_Notes__c" does not.

Question

What changes need to be made to my @wire or import code to allow the component to access the custom field value on the Event object?

Code

displayMeetingNotes.html

<template>
    <lightning-card title="Meeting Notes">
            <p class="slds-p-horizontal_small">
                <lightning-record-edit-form record-id="a1k1Y00000FB6xUQAT" object-api-name="Meeting_Note__c">
                    <lightning-messages></lightning-messages>
                    MeetingId : --|{meetingId}|--
                    MeetingNoteId : --|{meetingNoteId}|--
                    <lightning-input-field field-name="Running_Log_Coaching__c"></lightning-input-field>
                    <lightning-input-field field-name="Running_Log_Financial_Services__c"></lightning-input-field>
                    <lightning-button
                        class="slds-m-top_small"
                        variant="brand"
                        type="submit"
                        name="update"
                        label="Update">
                    </lightning-button>
                </lightning-record-edit-form>
            </p>
    </lightning-card>
</template>

displayMeetingNotes.js

import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';

// const fields = ['Event.Meeting_Notes__c'];
import EVENT_ID from '@salesforce/schema/Event.Id';
import MEETING_NOTE_ID from '@salesforce/schema/Event.Meeting_Notes__c';

export default class DisplayMeetingNotes extends LightningElement {
    @api recordId;

    @wire(getRecord, { recordId: '$recordId', fields:[MEETING_NOTE_ID, EVENT_ID]})
    meeting;

    get meetingNoteId(){
        return getFieldValue(this.meeting.data, MEETING_NOTE_ID);
    }

    get meetingId(){
        return getFieldValue(this.meeting.data, EVENT_ID);
    }

}

----- Updated Code here ------

Switching to an apex based data call is still not yielding the field values.

apex

@AuraEnabled(cacheable=true)
public static Event getEvent(Id eventId) {
    return [
        SELECT Id, Meeting_Notes__c
        FROM Event
        WHERE Id = :eventId
    ];
}

html

<template>
    <lightning-card title="Meeting Notes">
            <p class="slds-p-horizontal_small">
                <lightning-record-edit-form record-id="a1k1Y00000FB6xUQAT" object-api-name="Meeting_Note__c">
                    <lightning-messages></lightning-messages>
                    MeetingId : --|{meetingId}|--
                    <!-- MeetingNoteId : --|{meetingNoteId}|-- -->
                    <lightning-input-field field-name="Running_Log_Coaching__c"></lightning-input-field>
                    <lightning-input-field field-name="Running_Log_Financial_Services__c"></lightning-input-field>
                    <lightning-button
                        class="slds-m-top_small"
                        variant="brand"
                        type="submit"
                        name="update"
                        label="Update">
                    </lightning-button>
                </lightning-record-edit-form>
            </p>
    </lightning-card>
</template>

javascript

import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import getEvent from '@salesforce/apex/TaskEventUtility.getEvent';
import { getSObjectValue } from '@salesforce/apex';

export default class DisplayMeetingNotes extends LightningElement {
    @api recordId;

    @wire(getEvent, { eventId: '$recordId'})
    meeting;

    get meetingId() {
        return this.meeting.data ? getSObjectValue(this.meeting.data, 'Id') : 'No Data';
    }

}
1

You cannot get the record of Event object using the "User Interface API". Please use the apex controller to query the Event record.

Please find the supported standard objects by "User Interface API" here:- https://developer.salesforce.com/docs/atlas.en-us.uiapi.meta/uiapi/ui_api_get_started_supported_objects.htm

| improve this answer | |
  • Do you know where I can find information on how to use an apex controller in lwc for this purpose? – Jwok Mar 4 at 18:42
  • You just need to create an apex method that takes the record Id and return the event record. And then call it from javascript either using the wire method or imperative one. Find the full information here - developer.salesforce.com/docs/component-library/documentation/… – Avi Rai Mar 4 at 18:46
  • I updated my question to show the new apex based code, but unfortunately, the desired values are still not showing. The page also raises an error reading [Value does not include an object API name.] – Jwok Mar 4 at 19:47
0

I've marked Avi Rai's response correct, but for future readers, I wanted to include the code that was ultimately successful in achieving my goal.

html

<template>
    <lightning-card title="Meeting Notes">
            <p class="slds-p-horizontal_small">
                <lightning-record-edit-form record-id={meetingNotesId} object-api-name="Meeting_Note__c">
                    <template for:each={fieldAPIs.data} for:item="rec">
                        <lightning-input-field field-name={rec} key={rec.Id}></lightning-input-field>
                    </template>
                    <lightning-button
                        class="slds-m-top_small"
                        variant="brand"
                        type="submit"
                        name="update"
                        label="Update">
                    </lightning-button>
                </lightning-record-edit-form>
            </p>
    </lightning-card>
</template>

Javascript

import { LightningElement, api, wire } from 'lwc';
import { getSObjectValue } from '@salesforce/apex';
import getMeetingNote from '@salesforce/apex/TaskEventUtility.getMeetingNote';
import getEvent from '@salesforce/apex/TaskEventUtility.getEvent';
import getMeetingNoteFields from '@salesforce/apex/TaskEventUtility.getMeetingNoteFields';

import ID_FIELD from '@salesforce/schema/Meeting_Note__c.Id';
import MEETING_TYPE from '@salesforce/schema/Event.Meeting_Type__c';

export default class DisplayMeetingNotes extends LightningElement {
    @api recordId;

    @wire(getMeetingNote, { eventId: '$recordId'})
    meeting;

    @wire(getEvent, { eventId: '$recordId'})
    eventRecord;

    @wire(getMeetingNoteFields, { eventId: '$recordId'})
    fieldAPIs;

    get meetingNotesId(){
        return this.meeting.data ? getSObjectValue(this.meeting.data, ID_FIELD) : '';
    }

    get meetingType(){
        return this.eventRecord.data ? getSObjectValue(this.eventRecord.data, MEETING_TYPE) : ''
    }



}

Apex

@AuraEnabled(cacheable=true)
  public static Event getEvent(Id eventId) {
      return [
          SELECT Id, Meeting_Type__c
          FROM Event
          WHERE Id = :eventId
      ];
  }

@AuraEnabled(cacheable=true)
public static List<String> getMeetingNoteFields(Id eventId){

    Event e = [SELECT Id, Meeting_Type__c, Meeting_Notes__c FROM Event WHERE Id = :eventId LIMIT 1];

    List<Meeting_Type_Fields__c> fields = [SELECT Order__c, Meeting_Template_Fields__c, Meeting_Notes_Update_Fields__c, Meeting_Template_Name__c
                                            FROM Meeting_Type_Fields__c 
                                            WHERE Meeting_Template_Name__c =: e.Meeting_Type__c
                                            AND  Meeting_Template_Name__c != null
                                            ORDER BY Order__c ASC];

    List<String> fieldAPINames = new List<String>();

    for(Integer i = 0, j = fields.size(); i < j; i++){
        fieldAPINames.add(fields[i].Meeting_Notes_Update_Fields__c);
    }

    return fieldAPINames;

}

@AuraEnabled(cacheable=true)
public static Meeting_Note__c getMeetingNote(Id eventId){
    Event e = [SELECT Id, Meeting_Type__c, Meeting_Notes__c FROM Event WHERE Id = :eventId LIMIT 1];
    Id meetingNoteId = e.Meeting_Notes__c;

    List<Meeting_Type_Fields__c> fields = [SELECT Order__c, Meeting_Template_Fields__c, Meeting_Notes_Update_Fields__c, Meeting_Template_Name__c
                                            FROM Meeting_Type_Fields__c 
                                            WHERE Meeting_Template_Name__c =: e.Meeting_Type__c
                                            AND  Meeting_Template_Name__c != null
                                            ORDER BY Order__c ASC];

    String strQuery = 'SELECT Id ';

    for(Integer i = 0, j = fields.size(); i < j; i++) {
        strQuery += ', ' + fields[i].Meeting_Notes_Update_Fields__c;
    }
    strQuery += ' FROM Meeting_Note__c WHERE Id = \''+meetingNoteId+ '\'';
    return Database.query(strQuery);
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.