0

I have an LWC component that has lightning-input-address with show-address-lookup, I have kept all the fields read-only except the address search. When I search address and press the save button then get an error bad value picklist for country/territory. I have selected the United States and it shows bad value. Can anyone tell me what actually it has to be passed? I know it wants value like Ind, US but how I can pass this?

You can review below component:

HTML:

 <lightning-input-address if:true={showShippingAdd} required class="slds-size_6-of-12" style="margin-left: 5%;"
      address-label="Address Test"
      read-only
      street-label="Street Test "
      city-label="City Test"
      country-label="Country Test"
      province-label="Province Test"
      postal-code-label="PostalCode Test" 
      street={shipToStreet}
      city={shipToCity}
      province={shipToState}
      postal-code={shipToPostalCode}
      country={shipToCountry}     
      onchange={handleShippingAddChg} 
      show-address-lookup ></lightning-input-address> 

Js:

handleShippingAddChg(event) {
    console.log("event.detail--- >> ", event.detail);
    console.log("event.target--- >> ", event.target);
    console.log("street >> ", event.detail.street);
    console.log("city >> ", event.detail.city);
    console.log("state >> ", event.detail.province);
    console.log("postalcode >> ", event.detail.postalCode);
    console.log("country >> ", event.detail.country);
    console.log("country2 >> ", event.target.country);
    this.shipToStreet = event.detail.street;
    this.shipToCity = event.detail.city;
    this.shipToState = event.detail.province;
    this.shipToCountry = event.detail.country;
here country >> is coming as United States but i want to pass the value

1 Answer 1

0

The lightning-input-address component expects the country value in a specific format (like "US" for the United States) rather than the full country name. You can handle this by mapping the full country name to its corresponding country code.

Create a Country Code Mapping:

const countryCodeMapping = {
'United States': 'US',
'India': 'IN',
// Add other countries as needed
};

Update the handleShippingAddChg Method:

handleShippingAddChg(event) {
        console.log("event.detail--- >> ", event.detail);
        console.log("event.target--- >> ", event.target);
        console.log("street >> ", event.detail.street);
        console.log("city >> ", event.detail.city);
        console.log("state >> ", event.detail.province);
        console.log("postalcode >> ", event.detail.postalCode);
        console.log("country >> ", event.detail.country);
        console.log("country2 >> ", event.target.country);

        this.shipToStreet = event.detail.street;
        this.shipToCity = event.detail.city;
        this.shipToState = event.detail.province;
        this.shipToPostalCode = event.detail.postalCode;
        this.shipToCountry = countryCodeMapping[event.detail.country] || event.detail.country;
}       

Full Example:

HTML:

<template>
        <lightning-input-address if:true={showShippingAdd} required class="slds-size_6-of-12" style="margin-left: 5%;"
                address-label="Address Test"
                read-only
                street-label="Street Test"
                city-label="City Test"
                country-label="Country Test"
                province-label="Province Test"
                postal-code-label="PostalCode Test"
                street={shipToStreet}
                city={shipToCity}
                province={shipToState}
                postal-code={shipToPostalCode}
                country={shipToCountry}
                onchange={handleShippingAddChg}
                show-address-lookup>
        </lightning-input-address>
</template>

JS:

import { LightningElement, track } from 'lwc';

const countryCodeMapping = {
        'United States': 'US',
        'India': 'IN',
        // Add other countries as needed
};

export default class AddressComponent extends LightningElement {
        @track shipToStreet;
        @track shipToCity;
        @track shipToState;
        @track shipToPostalCode;
        @track shipToCountry;

        handleShippingAddChg(event) {
                console.log("event.detail--- >> ", event.detail);
                console.log("event.target--- >> ", event.target);
                console.log("street >> ", event.detail.street);
                console.log("city >> ", event.detail.city);
                console.log("state >> ", event.detail.province);
                console.log("postalcode >> ", event.detail.postalCode);
                console.log("country >> ", event.detail.country);
                console.log("country2 >> ", event.target.country);

                this.shipToStreet = event.detail.street;
                this.shipToCity = event.detail.city;
                this.shipToState = event.detail.province;
                this.shipToPostalCode = event.detail.postalCode;
                this.shipToCountry = countryCodeMapping[event.detail.country] || event.detail.country;
        }
}
5
  • Still getting an error, after console 'this.shipToCountry>> United States' is coming Commented Aug 9, 2024 at 7:48
  • @NileshWarade, Can you please provide the full code of your lwc? Commented Aug 9, 2024 at 8:47
  • above code is the code on which I am working. I mean whole code Commented Aug 9, 2024 at 8:49
  • I have tried this code in my org and it is working fine. no error throw. See this: awesomescreenshot.com/image/… Commented Aug 9, 2024 at 9:00
  • 1
    const countryCodeMapping = { 'United States': 'US', 'India': 'IN', }; by doing this change I am able to save the record. Thank you @Tushar Jadhav Commented Aug 9, 2024 at 9:40

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.