0

I trying to extract the values from a JSON object. This is the code to console log the retrieved record data -

console.log(JSON.stringify(data));

And here is the output in the JSON viewer

enter image description here

And this is the code I wrote to extract just the value of the Segment_ID__c field

console.log(JSON.stringify(data.fields.Segment_ID__c.value));

Below is the snip of the error I get when I load the record page

enter image description here

Reference for Matthew Souther

Matthew here is full code -

import { LightningElement, api, wire, track } from 'lwc';
import { getRecord, getFieldValue  } from 'lightning/uiRecordApi';
import loadPersonalizedOffers from "@salesforce/apex/PersonalizedOffersController.loadPersonalizedOffers";

let FIELDS = ['Account.Segment_ID__c'];

export default class Personalized_offers extends LightningElement {
    @api recordId;
    @api objectApiName;
    @track segment;
    
    @wire(getRecord, {
        recordId: '$recordId',
        fields: FIELDS
      }) segmentId( data, error ) {
          if(data) {
            //console.log(JSON.stringify(data));
            console.log(JSON.stringify(data.fields.Segment_ID__c.value));
          }
          else if(error) {
           
          }
      };
} 

EDIT - Raw Output

  1. console.log(JSON.stringify(data)); {"data":{"apiName":"Account","childRelationships":{},"id":"0011700001IAt7EAAT","lastModifiedById":"00517000007IbsaAAC","lastModifiedDate":"2021-02-06T18:26:54.000Z","recordTypeId":"01241000001AMf6AAG","recordTypeInfo":{"available":true,"defaultRecordTypeMapping":false,"master":false,"name":"Customer","recordTypeId":"01241000001AMf6AAG"},"systemModstamp":"2021-02-06T18:26:54.000Z","fields":{"Segment_ID__c":{"displayValue":null,"value":"a2n17000000EhO7AAK"}}}}

  2. console.log(JSON.Stringify(data.fields)); enter image description here

13
  • It's not clear to me whether you're getting this error on the console log line you shared, or if there are other references to Segment_ID__c in your JavaScript code that might have an issue. Can you share the full contents of the .js file? Feb 8, 2021 at 23:03
  • Matthew Souther, I have added the full js code to the post. The first console.log I am getting the data on the console log without any errors, the second one I don't see anything on console.log, but I get that error snip when I load a record.
    – sumchans
    Feb 8, 2021 at 23:10
  • 1
    Have you tried actually debugging your JS? developer.salesforce.com/docs/component-library/documentation/…
    – Nick Cook
    Feb 8, 2021 at 23:21
  • 1
    That code should work, based on what I'm able to see. Something isn't adding up. Can you provide the raw JSON output for console.log(JSON.stringify(data)) and console.log(JSON.stringify(data.fields))? Feb 8, 2021 at 23:37
  • 2
    instead of segmentId( data, error ) { can you try segmentId( {data, error} ) {
    – Nick Cook
    Feb 9, 2021 at 1:22

1 Answer 1

1

The error you're receiving is telling you that data.fields is undefined.

Looking at your code, you have segmentId( data, error ) { which is essentially declaring two objects to be passed into your function.

As per the docs (https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.data_wire_service_about), you need a single object with a data and error property.

Therefore you need to replace segmentId( data, error ) { with segmentId( { data, error } ) {

1
  • Thanks Nick. Appreciate it.
    – sumchans
    Feb 9, 2021 at 1:36

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.