We have a Segment picklist field that separates our accounts by platinum, strategic, premium, ect. We want to have the parent account display the highest segment of all the child accounts which could be a mix of all five. I've already got a set of the parent account Ids and a list of all the child and parent accounts.

I have this method that is looping through them but I don't know how to figure out what child value is the highest without doing if statements in the right order.

Once I have the highest value I want to compare that with the parent, so if it's already equal I don't have to update it.

public static List<Account> updateParentAcc(Set<Id> accSet, List<Account> accList)
    List<Account> accToUpdate = new List<Account>();
    String a = 'P - Platinum';
    String b = '1 - Strategic';
    String c = '2 - Premium';
    String d = '4 - Select';
    String e = 'NC - Not Covered';

    for(Id parentId : accSet)
        String parentSegment;
        String childSegment;

        for(Account acc : accList)
            if(acc.Id == parentId)
                parentSegment = acc.Segment__c;

        for(Account ac : accList)
            if(ac.ParentId == parentId)
                if(ac.Segment__c == a)
                    childSegment = a;
                if(ac.Segment__c == b)
                    childSegment = b;
                if(ac.Segment__c == c)
                    childSegment = c;
                if(ac.Segment__c == d)
                    childSegment = d;
                if(ac.Segment__c == e)
                    childSegment = e;

        if(parentSegment != childSegment)
            Account accP = new Account();
                accP.Id = parentId;
                accP.Segment__c = childSegment;
    return accToUpdate;

I think I want to have a break because once I have the value I need I don't need to loop through the records to see the same value, but with this code it will not work if the second record is higher than the first.

Where am I going wrong?

  • this problem is conceptually easier to solve if you pass the method a Map<Id,ChildAccount[]> childAccountsbyParentAccountId where ChildAccount is a wrapper on Account that implements Comparable. Then you can offload the sorting to the Comparable implementation
    – cropredy
    Sep 17, 2019 at 18:57

1 Answer 1


Here is one option... you could use maps to convert the values into something you can actually compare (integers).

This example uses the standard Rating field on the Account... but hopefully it illustrates the idea. Let me know if the code comments aren't enough to make the intent clear.

    public static void checkRatings() {

        // Create a map that allows us to assign integer values to each picklist value.
        Map<String,Integer> ratingToScoreMap = new Map<String,Integer>();

        // Create a map that allows us to convert an integer value back to the given picklist value.
        Map<Integer,String> scoreToRatingMap = new Map<Integer,String>();

        // Get the children of a specific account for testing.
        Account[] children = [SELECT Id,
                              FROM Account
                              WHERE Parent.Name = 'Parent Account'];

        // Assume that the max is 0... which is below all of the "real" ratings.
        Integer maxRating = 0;

        // Loop through the children...
        for (Account child : children) {

            // Get the current rating for this child account.
            Integer currentRating = ratingToScoreMap.get(child.Rating);

            // If the current rating is greater than the max,
            // let's keep the current rating.
            if (currentRating > maxRating) {
                maxRating = currentRating;

            // Optional: one could choose to exit here if the max
            // value is 3... since that's the highest it could possibly be.


        // After we've processed all of the children, convert the max rating
        // back to the actual picklist value.
        system.debug('Highest Rating: ' + scoreToRatingMap.get(maxRating));

  • This was what I needed. I converted the children list to a map and got the parent as well to compare if the parent score is different than the child score. Sep 18, 2019 at 14:18
  • Great. Glad I could help. Sep 18, 2019 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.