I had an interesting question pop up in my feed recently. The asker had a question about taking fields populated with a particular value and adding them to a description field, so users who view the record can get an easy 'at a glance' look at things that need repair:

We have an object with 128 picklist items that all use the same global picklist values. It is used in an inspection form. The 3 possible values are OK, Repair Needed, N/A. I have a flow that then creates a record in a ticketing object. However, i would like to list ONLY the items that have Repair Needed. I can't figure out how to do this or if it can even be done. I'm seeking guidance on how to resolve this.

So, if the picklist values for right mirror and windshield wiper were both 'Repair Needed', a technician would be able to take a look at the description field as a one-stop-shop for all elements needing fixes.

There were some suggestions referencing out-of-the-box Salesforce Flow, but solutions involved countless repeated decision elements to coax the data out. These can be especially problematic to maintain and make edits to as time goes on. As such, we are diving into an Apex Invokable-based approach. Here is the code:

public with sharing class countPicklist {
    @InvocableMethod(label='Count Repairs' description='Returns a string containing the names of fields marked Repair Needed' category='Account')
    public static List<String> countPicklist(List<Vehicle_Inspection__c> myVehicleInspectionList) {
        Map<String,Object> myObjectMap = myVehicleInspectionList[0].getPopulatedFieldsAsMap();
        Map<String,Schema.SObjectField> vIFields = Schema.Vehicle_Inspection__c.SObjectType.getDescribe().fields.getMap();
        String retString = 'Needing Repair Fields: \r\n';
        List<String> retStringList = new List<String>();
        for(String fieldName : myObjectMap.keySet()){
            System.debug(fieldName);
            if(myObjectMap.get(fieldName) == 'Repair Needed'){
                Schema.DescribeFieldResult fieldResult = vIFields.get(fieldName).getDescribe();
                retString += fieldResult.getLabel() + '\r\n';
            }
        }
        retStringList.add(retString);
        System.debug(retString);
        return retStringList;
    }
}

The code works as follows:

  1. Accepts a single Vehicle Inspection record (the record with the values to evaluate)
  2. Gets populated fields from the object, which should hopefully cut down on processing time since we can ignore null and blank fields
  3. Gets a map of the object's field metadata
  4. Declares our string variable to act as our list's starter
    1. This shows regardless if fields are found in Repair Needed or not
  5. Checks if the field's value is Repair Needed
    1. If it is, we need to pull the label for the field from the metadata map we retrieved. We then append it to our string, add some characters to generate a new line, and move on to the next field
    2. If it isn't, we move on to the next field
  6. We finally add the end string to a List of Strings and return it to the Flow

The solution output would look as follows:

fields.png

This type of solution can be great when you have larger sets of fields you need to analyze and distill down. I could see this type of solution being useful in some of the following circumstances

  1. (Current poster's usage) You want to provide quick and easy-to-view flags on fields within records that require action, like required fixes on a vehicle
  2. You want to highlight things that need immediate attention in a separate field, such as specific values from a prospective blood donor's pre-donation questionnaire
  3. You have a survey where if any response is 'Highly Dissatisfied', you want to prioritize it at a glance on a record

The usage of Apex Invokables for these types of business problems will likely continue to happen until more nuanced looping is added to Flow, along with the ability to dynamically pull information from fields! Until then, solutions like the one outlined above are likely your best bet at solving a similar business problem.

Displaying all fields with a certain value in Flow in Salesforce