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
PeacePeace 

Display only field values from every record of an object

Hi

Please help me in implementing following :

I dynamically create 'Select' query to fetch data from an object, as shown below

Public PageReference FetchObjectContents() {
 
     
      String query = 'SELECT...';

    
       ObjectContents = database.query(query);
    
 system.debug('----ObjectContents-------' +ObjectContents);
 
 
 //Converting Query result to String
 String str = '';

for (SObject a : ObjectContents) {
    
    str += String.valueOf(a) + '\n';
}
}

  OUTPUT  something like :

Account:{Name=Burlington Coat Factory, Id=001T000000pSG6rIAF}
Account:{Name=General Motors, Id=001T000000pSf4cAIR}
...

 I want OUTPUT as follows,  ie. without Object name and Field name  ( only field values )
 
 'Burlington Coat Factory', 001T000000pSG6rIAF  
 'General Motors', 001T000000pSf4cAIR



OR is there any other way to fetch only field values dynamically.

 

Thanks in advance!!

Best Answer chosen by Admin (Salesforce Developers) 
Dhaval PanchalDhaval Panchal

Try this,

 

        //Converting Query result to String
        String str = '';
        //For fatching name and id only
        for (SObject a : ObjectContents) {
            str += a.get('name') + ', ' + a.get('Id') + '\n';
        }
        system.debug('###### str >>>> ' + str);

This will generate below result

Burlington Coat Factory,001T000000pSG6rIAF
General Motors,001T000000pSf4cAIR ...

for all field try this.

        //For fatching all values dynamically
        String str2 = '';
        for (SObject a : ObjectContents) {
            for(String field:fieldApis){
                str2 += field + ' = ' + a.get(field) + '\n';
            }
        }
        system.debug('###### str2 >>>> ' + str2);


This will generate below result

Name = Burlington Coat Factory
Id = 001T000000pSG6rIAF
Phone = 45532 ...

 

 

All Answers

Yoganand GadekarYoganand Gadekar

try like this,

 

for(objectAPIName obj: objList){

 System.debug(obj.name+obj.id   );

 

}

PeacePeace

Hi,

 

I have a Generic Sobject type.

And the solution which u provided requires me to provide objectAPIName statically in my code, which is not the requirement.

 

Can u please provide a solution by modifying the code which i provided.

 

Thanks !!

Dhaval PanchalDhaval Panchal

Try this,

 

        //Converting Query result to String
        String str = '';
        //For fatching name and id only
        for (SObject a : ObjectContents) {
            str += a.get('name') + ', ' + a.get('Id') + '\n';
        }
        system.debug('###### str >>>> ' + str);

This will generate below result

Burlington Coat Factory,001T000000pSG6rIAF
General Motors,001T000000pSf4cAIR ...

for all field try this.

        //For fatching all values dynamically
        String str2 = '';
        for (SObject a : ObjectContents) {
            for(String field:fieldApis){
                str2 += field + ' = ' + a.get(field) + '\n';
            }
        }
        system.debug('###### str2 >>>> ' + str2);


This will generate below result

Name = Burlington Coat Factory
Id = 001T000000pSG6rIAF
Phone = 45532 ...

 

 

This was selected as the best answer