0

I have an object called Customer_Experience__c that is the child of a master-detail relationship to the Contact. I'm able to view the entire record in the console log but I need to assign the field values to variables in js. It keeps returning 'undefined' for introOnboSp in the console log. Any ideas?

Apex:

public with sharing class getConsultantTrackerInfo {
@AuraEnabled(cacheable=true)
public static List<Customer_Experience__c> getConsultantTrackerInfo(List<Id> recordId){
    List<Customer_Experience__c> expTracker = [SELECT Id, Intro_Onboarding_Specialist__c 
                                               FROM Customer_Experience__c 
                                               WHERE Candidate__c =:recordId];
    return expTracker;
   }
}

JS:

import { LightningElement, api, wire } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import getConsultantTrackerInfo from '@salesforce/apex/getConsultantTrackerInfo.getConsultantTrackerInfo';

export default class ConsultantTracker extends NavigationMixin(LightningElement) {
@api recordId;
tracker;
introOnboSp;

@wire(getConsultantTrackerInfo, {recordId : '$recordId'})
wiredTracker({data, error}){
if(data){
    console.log('data: ', data);
    this.tracker = data;
    this.introOnboSp = this.tracker.Intro_Onboarding_Specialist__c;
    console.log('introOnboSp = '+this.introOnboSp);
}
else if(error){
    console.log('error');
    console.log(error);
    }
  }
}

2 Answers 2

1

You are returning a list of Customer_Experience__c objects but in the javascript you are attempting to use dot notation to access a field

this.introOnboSp = this.tracker.Intro_Onboarding_Specialist__c;

My advice would be to console log this.tracker and see if you are getting back a list and if you are you should use the first value like so

this.introOnboSp = this.tracker[0].Intro_Onboarding_Specialist__c

(if you are not I would check the query itself in developer console's query editor to see if the query brings back a value).

Alternatively in the apex you could return just the first value

    public with sharing class getConsultantTrackerInfo {
@AuraEnabled(cacheable=true)
public static Customer_Experience__c getConsultantTrackerInfo(Id recordId){
    Customer_Experience__c expTracker = [SELECT Id, Intro_Onboarding_Specialist__c 
                                               FROM Customer_Experience__c 
                                               WHERE Candidate__c =:recordId LIMIT 1];
    return expTracker;
   }
}
1
  • 1
    Ahh that makes sense. Thanks for clearing that up for me Calvin! Commented Jun 21, 2021 at 19:11
0
public static List<Customer_Experience__c> getConsultantTrackerInfo(List<Id> recordId){

The Apex is expecting a List of Id, but you're passing in an Id (so it can't be automatically converted to a list). This is easy to fix, though:

public static List<Customer_Experience__c> getConsultantTrackerInfo(Id recordId){
2
  • I have unfortunately tried that already and it still comes out as "introOnboSp = undefined " Commented Jun 21, 2021 at 18:34
  • @LukeHiggins is this being placed on a Record Page? Is it a top-level component? The thing is, wire will fire twice if it loads data, once with a null parameter, and once with the loaded data. If it's not loading, make sure you're in the right context.
    – sfdcfox
    Commented Jun 21, 2021 at 18:43

You must log in to answer this question.

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