+ Start a Discussion
cmarzillicmarzilli 

Loop through fields?

I want to loop through all the fields on an opportunity and if they are a multi select pick list, add their name to a list.  here is my non-working code:

 

        Schema.DescribeSObjectResult dsr = Opportunity.sObjectType.getDescribe();
        List<String>  MPL= new List<String>();
        for (Integer i=0; i < dsr.fields.Length; i++) {
                if( dsr.fields[i].getType == 'MultiPicklist'  )
                {
                    MPL.Items.Add( dsr.fields[i].getLabel  );
                }
            }

On the MPL.Items.Add( dsr.fields[i].getLabel  ) line I get the following error:

Error: Compile Error: Expression must be a list type: Schema.SObjectTypeFields at line 13 column 36 

 

From the documentation "getLabel" should return a string.  I don't know why it's asking for a list type. 

cmarzillicmarzilli
Ok looks like I have more problems than just that one line. I recently did basically the same thing through the web services API, does anyone know how to accomplish this through APEX?
rtuttlertuttle
I ran into this.  Try using the fields.getMap() member.  It returns a Map<String, SObjectField>