0

I am fetching contact address in js and setting the address in a variable which I am using in html to print the address.

Now suppose if contact mailing state is empty then in html its printing 'null'. How can I handle this situation?

JS::

import STATE_FIELD from "@salesforce/schema/Contact.MailingState";
import COUNTRY_FIELD from "@salesforce/schema/Contact.MailingCountry";
export default class CustomerContact extends LightningElement {
    MailingAddress;
    @wire(getRecord, {
        recordId: "$recordId",
        fields: [COUNTRY_FIELD, STATE_FIELD],
    })
    wiredData({ error, data }) {
        if (data) {
            const street = getFieldValue(this.contactRecord, STREET_FIELD);
            const country = getFieldValue(this.contactRecord, COUNTRY_FIELD);
            this.MailingAddress = street + "," + country;
        }
    }
}

HTML::

<div>{MailingAddress}<div> 
//this is printing null Country Name if street is blank.

2 Answers 2

1

let street = '' // assign a default value is a good practice, we use an empty string here

if(getFieldValue(this.contactRecord, STREET_FIELD)) { street = getFieldValue(this.contactRecord, STREET_FIELD) //if it is not null reassign }

Or you can do it in one line

let street = getFieldValue(this.contactRecord, STREET_FIELD) ?getFieldValue(this.contactRecord, STREET_FIELD) : ''

0

Looking at your sample code, the problem is that you're not reading from the data variable but from this.contactRecord (which isn't assigned):

const street = getFieldValue(this.contactRecord, STREET_FIELD);
const country = getFieldValue(this.contactRecord, COUNTRY_FIELD);

Should be:

const street = getFieldValue(data, STREET_FIELD);
const country = getFieldValue(data, COUNTRY_FIELD);
4
  • Thanks! could you pls help me with the correct syntax.
    – Raj SF
    Jan 11, 2021 at 13:29
  • here you go, I've added the correct syntax
    – POZ
    Jan 11, 2021 at 15:04
  • Hi, its still showing null.
    – Raj SF
    Jan 11, 2021 at 17:02
  • whenever there is no value in UI means if street is blank then its returning null in UI. How can i avoid this
    – Raj SF
    Jan 11, 2021 at 17:38

You must log in to answer this question.

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