Formula-driven Configuration

Illia Leshchuk
1 min readDec 3, 2024

When you have a condition-based logic to do something with a record, e.g. apply color-coding for a record card, process it in a certain way, etc. — use a formula-field to give you something (a flag, a JSON, etc.) that you can use right away in place where it is needed. Basically — move the logic into a formula-field and let SF do the heavy lifting, to give you the answer, which you can just take and use where it is needed

For example — apply color-coding for an Opportunity card used in a custom LWC Kanban component, based on a certain condition for Close Date. Instead of hardcoding those colors, or creating CMDT entries, or, God forbid, use Custom Labels for this purpose, and use a bunch of if-else statements inside LWC component, a new formula field Kanban_Configuration__c has been introduced for Opportunity which returns JSON:

"{"
/*7 days before closing - yellow, closed day passed - red, rest - green*/
& "\"borderColor\":\"" + IF(CloseDate < TODAY(), '#ffded5', IF(TODAY() + 7 > CloseDate, '#f9e3b6', '#91db8b')) + "\""

& "}"

and then inside LWC just use this config directly:

const config = JSON.parse(this.opportunity?.Kanban_Configuration__c ?? '{}');
Object.assign(this.template.querySelector('.opportunity-card').style, config);

This approach gives us the following benefits:

  • administrator has now full support over the configuration w/o need to change the code
  • not only the colors from the initial requirements can be adjusted and new colors added, but the whole logic can be easily altered

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Illia Leshchuk
Illia Leshchuk

Written by Illia Leshchuk

A salesforce developer/architect

No responses yet

Write a response