3

I am looking to make dynamic queries, but I want to avoid compound fields in the query. Is there any way to check if the fields is a compound or overall not updateable?

    public List<Sobject> DynamicSOQL(String objectName, Set<Object> IdsToQuery) 
    {

        String SobjectApiName = objectName;
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();

        List<String>apiNames =  new list<String>();

        for(String apiName : fieldMap.keyset())
        {
            apiNames.add(apiName);
        }

        string allstring = string.join(apiNames,',');

        query = 'SELECT ' + allstring + ' FROM ' + SobjectApiName + ' WHERE Id in' :IdsToQuery + ' Limit 3';

        Account[] accList = Database.query(query);

        return accList;
}

1 Answer 1

3

You are almost there

You can utilise the DescribeFieldResult object to determine if the running user is able to update the field based on the running users FLS.

Compound fields such as BillingAddress on the account object will return false, the fields which make it up will return true.

Hope this helps

public List<Sobject> DynamicSOQL(String objectName, Set<Object> IdsToQuery) 
    {

        String SobjectApiName = objectName;
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();

        List<String>apiNames =  new list<String>();

        for(String apiName : fieldMap.keyset())
        {
           if(fieldMap.get(apiName).getDescribe().isUpdateable()) {
              apiNames.add(apiName);
           }
        }
2
  • what about things like compound fields? or does isUpdateable capture this? Commented Feb 27, 2020 at 0:26
  • I updated the answer to give a little more clarity
    – Crispy
    Commented Feb 27, 2020 at 0:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .