+ Start a Discussion
JagadeshJagadesh 

Fieldsets in apex

Hi,

I have a requirement where in im using Fieldsets.Here using fieldsets i have constructed the query and the database.query(query) working (conRec in the given code) fine,Now i would want to know the data that is returned,(i.e.,the data in each field of the fieldset ).
Could someone help me here. My code is as follows:


Global class DataValidationNew{
    Webservice static boolean dataValidate(Id conId){
    String Query='SELECT ';
    List<Schema.FieldSetMember> fieldList=SObjectType.Contact.FieldSets.test_field_set.getFields();
     for(Schema.FieldSetMember f : fieldList) {
       Query = Query+f.getFieldPath() +','; 
         }
        Query=Query+'Id from Contact where id=:conId';
        Contact conRec=Database.query(Query);
        System.debug('contact record'+conRec);
        return true;
}
}
Best Answer chosen by Jagadesh
KapilCKapilC
Hello Jagedesh,

Please give a try to below code, this will work for you.

Global class DataValidationNew{
    Webservice static boolean dataValidate(Id conId){
    String Query='SELECT ';
    List<Schema.FieldSetMember> fieldList=SObjectType.Contact.FieldSets.test_field_set.getFields();
     for(Schema.FieldSetMember f : fieldList) {
       Query = Query+f.getFieldPath() +','; 
         }
        Query=Query+'Id from Contact where id=:conId';
        Contact conRec=Database.query(Query);
        System.debug('contact record'+conRec);
          
        sobject sobj = conRec;
        for(Schema.FieldSetMember f : fieldList) {
             System.debug('::::Your Field Result::::::'+sobj.get(f.getFieldPath()));
         }
        return true;
}
}

Thanks,
Kapil
 

All Answers

sandeep sankhlasandeep sankhla
Jagedesh,

Data should be visible from system.debug whihc you have already used... Let me know if your looking for something else..

Thanks,
Sandeep
KapilCKapilC
Hello Jagedesh,

Please give a try to below code, this will work for you.

Global class DataValidationNew{
    Webservice static boolean dataValidate(Id conId){
    String Query='SELECT ';
    List<Schema.FieldSetMember> fieldList=SObjectType.Contact.FieldSets.test_field_set.getFields();
     for(Schema.FieldSetMember f : fieldList) {
       Query = Query+f.getFieldPath() +','; 
         }
        Query=Query+'Id from Contact where id=:conId';
        Contact conRec=Database.query(Query);
        System.debug('contact record'+conRec);
          
        sobject sobj = conRec;
        for(Schema.FieldSetMember f : fieldList) {
             System.debug('::::Your Field Result::::::'+sobj.get(f.getFieldPath()));
         }
        return true;
}
}

Thanks,
Kapil
 
This was selected as the best answer
JagadeshJagadesh
Thanks @KapilC,It worked.