0

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.

enter image description here

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

  1. 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?
  2. Why is it firing all the other values when I have just asked for the value of just one field which Segment_ID__c?
  3. How can I extract the Segment_ID__c value from the output in the snip below?

enter image description here

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) {
           
          }
      };
}   

2 Answers 2

1

This we can do in three steps:

Step 1 Get Account record data for that we need recordId which we have.

Step 2 Now get Segment_ID__c field value from account record.

Step 3 Finally get the loadPersonalizedOffers with the Segment_ID__c value.

Code will look something like this:

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

/* ... */

import loadPersonalizedOffers from "@salesforce/apex/PersonalizedOffersController.loadPersonalizedOffers";

FIELDS = ['Segment_ID__c'];

export default class Personalized_offers extends LightningElement {
    @api recordId;
    @track account;
    segmentId;
    
    /* ... */
    
    @wire(getRecord, { recordId: '$recordId', fields: FIELDS }) /* Step 1 */
    getAccount({error, data}){
        if (error) {            
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Error loading account',
                    message: error.body.message,
                    variant: 'error',
                })
            );
        } else if (data) {
            this.account = data;
            this.segmentId = this.account.fields.Segment_ID__c.value; /* Step 2 */
            /* Now we can call the getPersonalizedOffers */
            loadPersonalizedOffers({Id: this.segmentId}).then((data)=> {
                if (data) {
                    console.log(JSON.stringify(data));
                }
            }).catch((error)=> {
               console.log(JSON.stringify(error));
            });
        }
    }

    /* ... */

}
14
  • thanks for the detailed steps, I get an error on the second @wire, it says "Leading decorators must be attached to a class declaration"
    – sumchans
    Feb 8, 2021 at 2:08
  • Added missing track decorator in the first import. Feb 8, 2021 at 2:25
  • Give it a try now, it should work, didn't tried this so if there is still any error try to debug that. Feb 8, 2021 at 2:28
  • I have updated my original post, with the edits. Still I was not able to get to an end to this. Please advise!
    – sumchans
    Feb 8, 2021 at 3:15
  • in the new code replace this.segmentId = getFieldValue(this.data, SEGMENT_ID_FIELD) with this.segmentId = getFieldValue(this.data, SEGMENT_FIELD), getFieldValue takes 2nd parameter as a single field not a list of fields. Feb 8, 2021 at 3:49
1

Pass the accrount recordId to the getFieldValue() method, from lightning/uirecordapi, and get the segment_id. Pass this segment_id into the personalizedOffers wire adaptor.

Refer to the above link for an example of how the method works.

1
  • Thanks Naga, You guided me in the right direction, but my code still; needs some fixing.
    – sumchans
    Feb 8, 2021 at 21:50

You must log in to answer this question.

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