function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
GailGail 

How can I find a value of a field in a field set?

I have code that returns a fieldset based on a field's value (e.g. if Field is X, returns fieldset X but if Field is Y, returns fieldset Y. It then cycles through and adds up the number of fields in the fieldset. Next, I'd like to be able to cycle through each field and if its value is "Yes", then add a count. Below is a code snippet. Obviously, the if(lead[f.getFieldPath()] == 'Yes') line does not work. How can I find the value of the fieldsetmember? I also tried constructing a database.query (string query = 'Select ' + f.getFieldPath() + ' from lead where id =: lead.id'), but then I can't figure out how to query the result just for the field value.

if (fsMap.get(strProgram) != null){
                for(Schema.FieldSetMember f : fsMap.get(strProgram).getFields()) {
                    TotalPreReqs += 1;
               
                if(lead[f.getFieldPath()]== 'Yes'){              
                    PreReqCount += 1;          
             
                }
            }//end cycle through fieldset
            }
Best Answer chosen by Gail
sfdcfoxsfdcfox
Array syntax only works on List objects. An SObject uses the function get(field), where field is either a string or SOBjectField token.
if((String)Lead.get(f.getFieldPath()) == 'Yes') ...

All Answers

sfdcfoxsfdcfox
Array syntax only works on List objects. An SObject uses the function get(field), where field is either a string or SOBjectField token.
if((String)Lead.get(f.getFieldPath()) == 'Yes') ...
This was selected as the best answer
GailGail
Thank you so much!! That did the trick. Now have a new problem but will try to resolve on my own first and then post a new thread...