I am trying to get records from another object using the field values from the record page.
Here I have the Account object for which I am getting the recordId using the
@api recordId
declaration. I want to send the value of the segment_Id
field which is also in the account object. How do I pass along the value of the segment_id field with @wire
declaration to retrieve data from a different object.
In the below code I have hardcoded the Id value to test if it is pulling any data, which it does. I want to pass the Account.Segment_id
value there inside of hardcoding
Here is the full code I have so far -
import { LightningElement, api,wire,track } from 'lwc';
import SEGMENT_FIELD from '@salesforce/schema/Account.Segment_ID__c';
import { getRecord } from 'lightning/uiRecordApi';
import loadPersonalizedOffers from "@salesforce/apex/PersonalizedOffersController.loadPersonalizedOffers";
export default class Personalized_offers extends LightningElement {
@api recordId;
@api objectApiName;
fields=['Name','Segment_ID__c'];
@wire(loadPersonalizedOffers, {Id: 'a2n17000000EhO7AAK'}) personalizedOffers({
error, data
}) {
if (data) {
console.log(JSON.stringify(data));
}
}
}
EDIT after the below trials-
I have tried all the below answers, still not reaching anywhere, now I am getting this error on record page load. As of now what I am trying is to get the segmentId track variable to be populated with the actual field value so that I can pass it to my next apex call.
Here is the new code -
import { LightningElement, api, wire, track } from 'lwc';
import SEGMENT_FIELD from '@salesforce/schema/Account.Segment_ID__c';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import loadPersonalizedOffers from "@salesforce/apex/PersonalizedOffersController.loadPersonalizedOffers";
const SEGMENT_ID_FIELD = [SEGMENT_FIELD];
export default class Personalized_offers extends LightningElement {
@api recordId;
@api objectApiName;
@track segmentId;
@wire(getRecord, {
recordId: '$recordId',
SEGMENT_ID_FIELD
}) segmentId( data, error ) {
if(data) {
this.segmentId = getFieldValue(this.data, SEGMENT_ID_FIELD);
console.log(this.segmentId);
};
};
}
****EDIT - 2-- Not having any luck so far. Here is the code so far, after the mentioned changes
import { LightningElement, api, wire, track } from 'lwc';
import SEGMENT_FIELD from '@salesforce/schema/Account.Segment_ID__c';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import loadPersonalizedOffers from "@salesforce/apex/PersonalizedOffersController.loadPersonalizedOffers";
const SEGMENT_ID_FIELD = [SEGMENT_FIELD];
export default class Personalized_offers extends LightningElement {
@api recordId;
@api objectApiName;
@track segmentId;
@wire(getRecord, {
recordId: '$recordId',
SEGMENT_ID_FIELD
}) segmentId( data, error ) {
if(data) {
this.segmentId = getFieldValue(this.data, SEGMENT_FIELD);
console.log(this.segmentId);
};
};
}
EDIT 3 - After several trials I am getting the data console logged, a few questions on the data.
In the log below I have the console log firing even when I have it specified inside if(data)
. It is on the second log I see the actual data.
Questions
- In the snip below I have the console log method firing twice even when I have it specified inside
if(data)
. How do I make fire once when we have the data available? - Why is it firing all the other values when I have just asked for the value of just one field which Segment_ID__c?
- How can I extract the Segment_ID__c value from the output in the snip below?
Here is my final code -
import { LightningElement, api, wire, track } from 'lwc';
import SEGMENT_FIELD from '@salesforce/schema/Account.Segment_ID__c';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import loadPersonalizedOffers from "@salesforce/apex/PersonalizedOffersController.loadPersonalizedOffers";
export default class Personalized_offers extends LightningElement {
@api recordId;
@api objectApiName;
@track segmentId;
@wire(getRecord, {
recordId: '$recordId',
fields: [SEGMENT_FIELD]
}) segmentId( data, error ) {
if(data != undefined) {
this.segmentId = getFieldValue(data, 'Account.Segment_ID__c');
console.log(this.segmentId);
}
else if(error) {
}
};
}