Wednesday 30 November 2016

Dynamically Assigning Fields in Salesforce With Example

Introduction
        Salesforce provides sObject methods and all of these methods are instance methods such as addError(), get(), etc. This article regarding the real time usage of put() method, It can be 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 (Custom Object)

Problem
        OpportunityStatusHistory object having the fields and that are named like Prospecting__c, Qualification__c, etc. The field names are similar to the StageNames in Opportunity object.

        Whenever the Opportunity record created or updated every time with the particular stage means we need to count the stage history details in “OpportunityStatusHistory” object. For example if I create Opportunity with Stage “Prospecting” means, OpportunityStatusHistory object record need to be created and the corresponding field Prospecting__c value will be set as 1.

        If I changed above opportunity Stage to “Qualification” means, the corresponding OpportunityStatusHistory object field Qualification__c value to be set as 1. If I went to backward stage i.e. ‘Prospecting” means the related Prospecting__c field in OpportunityStatusHistory record will get updated as 2.

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

This above code each and every time the field value is checked and assigned to the related OpportunityStatusHistory object. Suppose if you have 50 – 100 stages means the code goes complex. 

Solution
To overcome this issue, we can utilize the Dynamically getting field and assigning.

  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 : loanStatusFieldsDescribe.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(loanStatusFieldsMap.get(currentLoan.Stage__c)1); // This put method used to dynamically assigning field values
  12. }

Conclusion
By using Dynamic DML we can easily get the field names dynamically at run time and insert them into the database using DML. This approach is efficient and easy to use than checking each and every time and assigning to the corresponding fields.

No comments:

Post a Comment

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...