LWC @wire decorator not getting response from controller class. how to verify response from controller class using @wire decorator in LWC?

2.8k views Asked by At

I am new to LWC. Kindly help me.

I am trying to fetch value from controller class in LWC component. But every time I try to fetch value from controller class, it comes as undefined.

Here is my code: controller class

public without sharing class contactController {
    @AuraEnabled (cacheable= true)
    public static  String  getAccountExtId(String contactId){
        String accExtId;
        List <Contact> contactRecord = [select Account.CodeClientLEMS__c , AccountId from Contact where Id= :contactId];
        accExtId = contactRecord[0].Account.CodeClientLEMS__c;
        return accExtId;
    }
}

.Js

import { LightningElement, wire, api } from 'lwc';
import LOGO from "@salesforce/resourceUrl/CustomPortal_Logo";
import getAccountExtId from '@salesforce/apex/contactController.getAccountExtId';
export default class Logo_CustPotal extends LightningElement {
  @api  recordId;
  contacts;
  @wire(getAccountExtId,{contactId:'$recordId'})
  wiredAccount({ data, error }) 
  {
      if (data) {
          console.log('checking the data', data);
          this.contacts = data;
      } else if (error) {
          console.log('Something went wrong:', error);
      }
  }
coustomPortalLogo = LOGO + '/Logos/'+this.contacts+'.png';
}

html

    <template>
<lightning-card >
<div style="width: 15%; display: block;margin-left: auto;margin-right: auto;">
   <img src={coustomPortalLogo}>
</div>
</lightning-card>
</template>
1

There are 1 answers

6
eyescream On

Works for me. You sure you don't have any errors? Running as admin? Maybe you need to grant access to that class in profile/permission sets? Maybe you're running it on contact without account or one that doesn't have this field populated...

enter image description here

public with sharing class Stack73842936 {
    @AuraEnabled (cacheable= true)
    public static  String  getAccountExtId(String contactId){
        String accExtId;
        if(String.isNotBlank(contactId)){
            List <Contact> contactRecord = [select Account.Name from Contact where Id= :contactId];
            accExtId = contactRecord[0].Account.Name;
        }
        return accExtId;
    }
}
<template>
    <lightning-card>{contacts}</lightning-card>
</template>
import { LightningElement, wire, api } from 'lwc';
import getAccountExtId from '@salesforce/apex/Stack73842936.getAccountExtId';

export default class Stack73842936 extends LightningElement {
  @api  recordId;
  contacts;
  @wire(getAccountExtId,{contactId:'$recordId'})
  wiredAccount({ data, error }) 
  {
      if (data) {
          console.log('checking the data', data);
          this.contacts = data;
      } else if (error) {
          console.log('Something went wrong:', error);
      }
  }
}
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>55.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__RecordPage</target>
    </targets>
    <targetConfigs>
        <targetConfig targets="lightning__RecordPage">
            <objects>
                <object>Contact</object>
            </objects>
        </targetConfig>
    </targetConfigs>
</LightningComponentBundle>