Hi community,
For a recent Answers experience, I needed to include headshots on my entities’ result cards. The challenge was that some of the entities in my Knowledge Graph were missing headshot images:
To add to the challenge, I needed a different placeholder image depending on the value of an unrelated field - in my case Gender
.
Here is the logic I wanted to achieve:
-
If the
Headshot
field is populated, use that image. -
If the
Headshot
field is empty andGender
is set toFemale
, use a specific placeholder image. -
Otherwise, fall back to a different placeholder image.
→ Learn more about nullchecking here.
To achieve my desired result, I added the following code to my forked card:
return {
title: profile.name, // The header text of the card
subtitle: profile.c_area, // The sub-header text of the card
image: profile.headshot ? Formatter.image(profile.headshot).url : (profile.gender === 'Female' ? 'https://a.mktgcdn.com/p-sandbox/y0iJHYFeCERihQg0qv7j88TBeEnpHq-v_BLyHiDXOb8/142x200.png' : 'https://a.mktgcdn.com/p-sandbox/6DD2AHZA1kbe8M9ZQGW_JOwXFyWNnRTMDJXIGxvQK-4/321x450.jpg'),
...
}
Here is the result:
You can use this same conditional logic to populate any kind of field. Hope this helps someone