+ Start a Discussion
NadulaNadula 

Retrieve all the data by object ID

Hi Guys,

Need some help here,

I want to retrieve all the field data of a particular object.

I see standard retrieve core call (http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_retrieve.htm" target="_blank) has three arguments (fieldList, sObjectType, ids).

How can I retrieve all the data in all the fields without passing in full field list?


Thanks in advance

Nadula
Best Answer chosen by Nadula
Deepak Kumar ShyoranDeepak Kumar Shyoran
Yes you have too but you don't need to write the API name for all these 100 fields rather you can use For loop and can create string of such fields and then can pass them to SOQL.

See below code it will fetch all Fields from Account and then will fetch data from Account Object related to those fields.

Map<String, Schema.SObjectField> fieldsMap;
fieldsMap = Account.sObjectType.getDescribe().fields.getMap();

String fieldApiName = 'id' ;

// Creating String of fields for all fields on Account
for(Schema.SObjectField fieldName:fieldsMap.Values()) { 
	if(fieldname.getDescribe().getName() != 'id')
    fieldApiName += ',' +fieldname.getDescribe().getName() ;
}
System.debug('--'+Database.query('Select '+fieldApiName +' from Account Limit 2'));




All Answers

Deepak Kumar ShyoranDeepak Kumar Shyoran
First you have to find the list of all fields of a particular Object using Describe call and then with the  help of those and Dynamic SOQL you can retrieve data for all those fields for a particular Object.
NadulaNadula
Thanks Deepak,

Is that mean I can't retreive data wihtout passsing in field names?

Is there any way I can pass some sort of wild card like "all" or something to the "retrieve" method to get all the field data returned?


Thanks
Nadula
Deepak Kumar ShyoranDeepak Kumar Shyoran
Wild card are not allowed in Salesforce rather you have to pass field API name to query them. As I suggested above you can retrieve only field Api name with querying (by using Describe) but to retrieve data yes you should pass Field API name in SOQL.
NadulaNadula
Hi Deepak,

Thanks for the info.

Well, since the object I'm querying has more than 100 fields, that means I have to pass in all the field names if I want all the data retrieved of a particular object ID?


Thanks
Nadula
Deepak Kumar ShyoranDeepak Kumar Shyoran
Yes you have too but you don't need to write the API name for all these 100 fields rather you can use For loop and can create string of such fields and then can pass them to SOQL.

See below code it will fetch all Fields from Account and then will fetch data from Account Object related to those fields.

Map<String, Schema.SObjectField> fieldsMap;
fieldsMap = Account.sObjectType.getDescribe().fields.getMap();

String fieldApiName = 'id' ;

// Creating String of fields for all fields on Account
for(Schema.SObjectField fieldName:fieldsMap.Values()) { 
	if(fieldname.getDescribe().getName() != 'id')
    fieldApiName += ',' +fieldname.getDescribe().getName() ;
}
System.debug('--'+Database.query('Select '+fieldApiName +' from Account Limit 2'));




This was selected as the best answer
NadulaNadula
Hi Deepak,

It an awesome way to do it you pointed out.

I tried this, using a foreach to generate a string of fields returned by describeSObject(). It had 84 fields in total.

I passed that string to retrieve() and it only returned only 23 fileds of data.

Is there any limitation to the number of fields we can pass in to the function in one time?


Thanks
Nadula

Deepak Kumar ShyoranDeepak Kumar Shyoran
Try this :

Database.query('Select '+fieldApiName +' from ObjectName ') ;
fieldApiName -- > Comma separated field API name like  id,Name,createdByid ...

ObjectName --> Your SObject API name like Account or MyCustomObj__c .

This will return you all field data which you will pass to it.

NadulaNadula
Hi Mate, im back. Thanks for you your continuing support

I'm coding the script using php. 
Following is my code to get fields and construct a string to pass in to retrieve function. Any chance you can check it out?

$response = $mySforceConnection->describeSObject('Lead');
$allFields = array();
foreach($response->fields as $key => $value){
  if(!empty($value->name)) $allFields[] = $value->name;

sort($allFields);

$allFieldString = "";
foreach($allFields as $field)
  $allFieldString .= $field.", ";

$allFieldString = rtrim($allFieldString,", ");
$response1=$mySforceConnection->retrieve($allFieldString,"Lead",array("00Q9000000WP7LG"),"false");

NadulaNadula
Ok, the reason to not get all the fields data is "retrieve" and "query" only returns fields with data. No empty fields returned.

And following is php script for "query" call:

$response = $mySforceConnection->describeSObject('Lead');
$allFields = array();
foreach($response->fields as $key => $value){
	if(!empty($value->name)) $allFields[] = $value->name;
}		
sort($allFields);	

$allFieldString = "";
foreach($allFields as $field)
	$allFieldString .= $field.", ";

$allFieldString = rtrim($allFieldString,", ");

print $query = "SELECT $allFieldString FROM Lead WHERE IsDeleted = false";
$response = $mySforceConnection->query(($query));

foreach ($response->records as $record) {
	print "<pre>"; print_r($record); print "</pre>";
}

This is my first Salesforce project.
Thanks heaps Deepak, much appreciated your help.


Cheers
Nadula