All Ideas

Idea Details

Post an Idea
400  Points
Open
Idea has been posted. Give it an upvote or downvote.

Easier way to iterate over fields in an SObject in Apex

Salesforce Platform, Apex & Visualforce, Customization

 As an Apex developer writing code with layers of code abstraction, I would like to be able to efficiently iterate over all the fields in an SObject so that I can convert SObjects into JSON.

The problem I'm running into is that I have a class that is converting SObjects into JSON and thus needs to iterate over the fields present in the SObject to create a consumable JSON map. The only efficient way I have found to do this is the following:

 

Map<String, String> resultMap = new Map<String,String>();
// Iterate over all possible fields on the object to find the ones in the sObject passed in

for (String fieldName : Utils.getSObjectFields(sObj.getSObjectType()).keySet()) {

    // Passed in object has the field

    try {

        resultMap.put(fieldName, String.valueOf(sObj.get(fieldName)));

    } catch (System.SObjectException except) { /* field not found in sObj*/}

}
 

public with sharing class Utils {

    private static Map<String, Map<String,Schema.Sobjectfield>> sObjectFieldCache =new Map<String, Map<String,Schema.Sobjectfield>>();

    

    public static Map<String,Schema.Sobjectfield> getSObjectFields(Schema.SObjectType sObjectType) {

     String sObjectTypeAsString = String.valueOf(sObjectType);

     // If it's not already in the cache, add the sObject's field map to the cache

     if (!sObjectFieldCache.containsKey(sObjectTypeAsString)) {

        sObjectFieldCache.put(sObjectTypeAsString, sObjectType.getDescribe().fields.getMap());

     }

     return sObjectFieldCache.get(sObjectTypeAsString);

    }

}

Unfortunately, this is really slow (presumably because it has to iterate over all possible fields and throws and catches an exception every time a field is encountered that isn't in the sObject). A simple i to check if an SObject has a field instead of having to catch the exception each time.  If a "hasField(String fieldName)" method was implemented, I could change the code to:
 

Map<String, String> resultMap = new Map<String,String>();
// Iterate over all possible fields on the object to find the ones in the sObject passed in

for (String fieldName : Utils.getSObjectFields(sObj.getSObjectType()).keySet()) {

    // Passed in object has the field
    if (sObj.hasField(fieldName)) {

        resultMap.put(fieldName, String.valueOf(sObj.get(fieldName)));
    }

}


A preferable alternative, would be to able to iterate over the fields in an SObject so that I wouldn't have to iterate over all possible fields, particularly if the org has hundreds of custom fields. That would allow:

 

 

Map<String, String> resultMap = new Map<String,String>();
// Iterate over all possible fields on the object to find the ones in the sObject passed in

for (String fieldName : sObj.getAllFields()) {

    // Passed in object has the field

    resultMap.put(fieldName, String.valueOf(sObj.get(fieldName)));
}

Merge Idea · Flag

  • Upvotes
  • Downvotes

Ideas

Apps

from AppExchange

Questions

Help us to keep IdeaExchange clean by pointing out overlapping ideas. We'll investigate your suggestion and merge the ideas if it makes sense.



 

 

Thanks for your merge suggestion. We will review it shortly and merge the ideas if applicable.

Salesforce takes abuse situations very seriously. Examples of abuse include but are not limited to posting of offensive language or fraudulent statements. To help us process your request as quickly as possible, please fill out the form below describing the situation. For privacy and security reasons, the final outcome of an abuse case may not be revealed to the person who reported it.


 

Thank you for your feedback. We take abuse seriously and will investigate this issue and take appropriate action.