1

i wrote a function which uses set.my function is

public static Set<Schema.DisplayType> giveSchemaSet(Set<String> x){
  Set<Schema.DisplayType> displayCompatibleSet = new Set<Schema.DisplayType>();
  Map<String,Schema.DisplayType> displayTypeMap = new Map<String,Schema.DisplayType>{
            'boolean' => Schema.DisplayType.Boolean,
            'currency' => Schema.DisplayType.Currency,
            'date' => Schema.DisplayType.Date,
            'datetime' => Schema.DisplayType.DateTime,
            'double' => Schema.DisplayType.Double,
            'email' => Schema.DisplayType.Email,
            'encryptedstring' => Schema.DisplayType.EncryptedString,
            'integer' => Schema.DisplayType.Integer,
            'multipicklist' => Schema.DisplayType.MultiPickList,
            'percent' => Schema.DisplayType.Percent,
            'phone' => Schema.DisplayType.Phone,
            'picklist'=> Schema.DisplayType.Picklist,
            'reference'=>Schema.DisplayType.Reference,
            'string' =>Schema.DisplayType.String,
            'textarea' =>Schema.DisplayType.TextArea,
            'time' => Schema.DisplayType.Time,
            'url' => Schema.DisplayType.URL
            };
  System.debug('x is '+x);
  for(String displayType:x)
  {
   System.debug('displayType is:' + displayTypeMap.get(displayType));
   displayCompatibleSet.add(displayTypeMap.get(displayType));
  }
  System.debug('displayCompatibleSet is'+displayCompatibleSet);
  return displayCompatibleSet;
}

in debug log

x is {boolean, picklist, string, textarea}
displayCompatibleSet is{BOOLEAN}

in argument i am passing 4 elements but in displayCompatibleSet.it should also be of 4 length but it contains only one element .can anyone please explain this behaviour.

3
  • 1
    Do you see your "displayType is:" debug output in the debug log?
    – Keith C
    Jun 5, 2014 at 16:41
  • yes for loop is traversing 4 times and giving correct output. but set is not getting added 4 times.Please help Jun 5, 2014 at 16:47
  • and every time instead of boolean there is some random value among these four Jun 5, 2014 at 16:50

1 Answer 1

1

With exactly the implementation you have posted this test passes so I think the code is good:

@isTest
private class ATest {
    @isTest
    static void test() {
        Set<String> x = new Set<String>{
                'boolean',
                'picklist',
                'string',
                'textarea'
                };
        Set<Schema.DisplayType> expected = new Set<Schema.DisplayType>{
                Schema.DisplayType.Boolean,
                Schema.DisplayType.Picklist,
                Schema.DisplayType.String,
                Schema.DisplayType.TextArea
                };
        System.assertEquals(expected, giveSchemaSet(x));
    }
    public static Set<Schema.DisplayType> giveSchemaSet(Set<String> x) {
        ...
    }
}

PS The issue is with the debug output of sets of enumerations: the sets themselves are fine.

2
  • Brilliant. Absolutely correct about the fact that the Set contains the correct values. Iterating over the result indeed produced the full list of elements. Thanks Keith!! Something new is learned every day.
    – Mark Pond
    Jun 5, 2014 at 17:25
  • @MarkPond Think I saw the same problem a few months ago on this site where the debug output for sets was misleading someone but to be honest had forgotten about that when first looking at this...
    – Keith C
    Jun 5, 2014 at 17:29

You must log in to answer this question.

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