Real time Use Case of Dynamically Assigning Field in Salesforce

Salesforce provides sObject methods and all of these methods are instance methods such as addError(), get(), etc. This article explains one of the real-time usages of the put() method.


Salesforce provides sObject methods and all of these methods are instance methods such as addError(), get(), etc. This article explains one of the real-time usages of the put() method. It can reduce code and make the code efficient instead of hard coding Field or Object Names. Recently we had a scenario like field names should be assigned dynamically.

Consider the below two objects,

  1. Opportunity (Standard Object)
  2. OpportunityStatusHistory__c (Custom Object)


The OpportunityStatusHistory__c custom object fields are named like Prospecting__c, Qualification__c, etc. The field names are equal to the Stage Name’s in Opportunity object.

Whenever a new Opportunity record is created or updated with a particular stage, then we need to count the stage history details in “OpportunityStatusHistory” object. For example, if we create an opportunity with Stage “Prospecting”, then OpportunityStatusHistory object record should be created with the corresponding Prospecting__c field value set as 1.

If we changed the above opportunity stage to “Qualification”, then corresponding OpportunityStatusHistory object field Qualification__c value to be set as 1. If I go to backward stage i.e. “Prospecting”, then related Prospecting__c field in OpportunityStatusHistory record should get updated as 2.

Example Code Snippet:  

In the below code snippet, I am checking the Opportunity stage field values and assigning the value to the related OpportunityStatusHistory object fields. Suppose if I have 100 stages, then code become complex.

  1. for(Opportunity currentOpp : Trigger.New)
  2. {
  3.      OpportunityStatusHistory__c statusHistory = new OpportunityStatusHistory__c();
  4.      If(currentOpp.StageName == ‘Prospecting’)
  5.      {
  6.            statusHistory.Prospecting__c = ‘1’;
  7.      }
  8.      else if(currentOpp.StageName == ‘Qualification’)
  9.      {
  10.            statusHistory.Qualification__c = ‘1’;
  11.      }
  12. }


To overcome this we can utilize the Dynamic Apex. It can help you to write flexible code.

  1. Map<String, String> opportunityStatusFieldMap = new Map<String, String>();
  2. Map<String, Schema.SObjectField> opportunityStatusDescribe = OpportunityStatusHistory__c.sObjectType.getDescribe().fields.getMap();
  3. for(Schema.SObjectField currentField : opportunityStatusDescribe.Values())
  4. {
  5.     Schema.DescribeFieldResult fieldResult = currentField.getDescribe();
  6.     opportunityStatusFieldMap.put(fieldResult.getLabel(), fieldResult.getName());
  7. }
  8. for(Opportunity currentOpp : Trigger.New)
  9. {
  10.     OpportunityStatusHistory__c statusHistory = new OpportunityStatusHistory__c();
  11.     statusHistory.put(opportunityStatusFieldMap.get(currentOpp.Stage__c), 1); // This put method used to dynamically assign field values
  12. }


This approach is efficient and easier to use than checking conditions every time and assigning to the corresponding fields, and we can easily get and assign the field values dynamically at run time.


Dynamic Apex

sObject Class

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Field Trip App

Introduction:  Field Trip App helps the Salesforce users to calculate the custom field’s health. It can be used to run the inbuilt report on objects

Read Article »

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.