Hey HH Community,
I am currently using the following fields on one line for a “Description” field on a card:
details: profile.c_addressLine1 + ", " + profile.c_addressLine2 + ", " + profile.c_addressCity + " " + profile.c_postalCode, // The text in the body of the card
I need to create a null check to:
-
List item to ensure that only the fields that ARE populated show
-
List item ensure that the delimiter “,” does not show when one of the fields is empty
Can anyone help me as I am not sure what the correct syntax here would be?
Thanks in advance
Asaki
You can get the result you are looking for by:
- Adding a new formatter to the formatters-custom.js file:
export function addressCustom(profile) {
const line1 = profile.c_addressLine1 ? profile.c_addressLine1 : '';
const line2 = profile.c_addressLine2 ? profile.c_addressLine2 : '';
const city = profile.c_addressCity ? profile.c_addressCity : '';
const postalCode = profile.c_postalCode ? profile.c_postalCode : '',;
let text = [line1, line2, city].filter(Boolean).join(", ");
let text2 = [text, postalCode].filter(Boolean).join(" ");
if (profile.c_addressLine1 || profile.c_addressLine2 || profile.c_addressCity || postalCode) {
return text2;
}
}
The first portion of the code (e.g., const line1 = profile.c_addressLine1 ? profile.c_addressLine1 : '';
) runs a check to see if the fields you want to pull (addressLine1, addressLine2, city, and postalCode) exist within the KG. If the field data exists, the equation returns that field. Otherwise, nothing/blank is returned.
The let text
line puts the fields into a string with ‘,’ as a delimiter. However, it only returns the comma delimiter if data exists for each field through the use of .filter(Boolean)
. The let text2
line joins the previous string and any postal code data with a space as a delimiter.
The last line looks to see if any of the custom fields exist in the kg and if so, the text 2
string is returned as the final result.
- Adding the following to the component.js file for the card you want to show this information on. It should look something like this:
details: Formatter.addressCustom(profile)