Write Metadata-Aware Code

When you have a certain level of dynamic-ness in your code in almost every case let it be metadata-aware. Let’s imagine you’re converting standard Contact to a custom Contact__c object and you have a field mapping somewhere in your code:

//non-metadata-aware approach 
private static Map<String, String> NON_METADATA_AWARE_FIELD_MAPPING = new Map<String, String> {
'FirstName' => 'First_Name__c',
'LastName' => 'Last_Name__c',
'Email' => 'Email__c'
};
//metadata-aware approach
private static Map<SObjectField, SObjectField> METADATA_AWARE_FIELD_MAPPING = new Map<SObjectField, SObjectField> {
Contact.FirstName => Contact__c.First_Name__c,
Contact.LastName => Contact__c.Last_Name__c,
Contact.Email => Contact__c.Email__c
};

As a pros metadata-aware approach at least minimizes typo errors, allows to leverage “where is this used?” feature and prevent’s accidental deletion of critical elements (fields, objects, etc.).

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