0

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 Answer 1

0

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>
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your response. I see you have used<target>lightning__RecordPage</target> . I need the value to be displayed in <target>lightningCommunity__Page</target>. I even corrected the <targetConfigs>, but i encountered an error The 'objects' tag isn't supported for lightningCommunity__Page. Can you help me here.
Read the bit about "experience builder" in here: developer.salesforce.com/docs/component-library/documentation/…. if for some reason it still doesn't work poke me again, as last resort we can try to read account id from the page URL in a "connected callback" function (acts bit like constructor or aura's doInit function)
No luck. It seems like the LWC is not getting the response from controller class. Is there any other approach we can go with here? Every time the value for contacts is "undefined " for me.
Permissions? Does community user's profile / permset has right to execute this class? Any errors in browser's JS console or network traffic tab? Can that community user manually view that account (if it's his own accoun he should but different acc not neccessarily, you may need to read up on "sharing sets" or in extreme cases - mark the class as "without sharing")
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.