You need to sign in to do that
Don't have an account?
Austen Robinson
Retrieve all the data by object ID in salesforce object using apex sql
How can you show all the data (not just the Id's) in a salesforce object using APEX SQL?
I tried this:
but get the error: The query has to start with find or select?
Where exactly does that go?
Getting very frustrated. In my object I have 3 fields I want to extract. Name, Account_Owner and Wholesale_Name. Name and Accout_Owner are strings but Wholesale_Name is a reference. So I want to return:
Name Account Holder Wholesale_name
ABC John Doe ABC Company
I tried this:
// without an ID, simply specify the object to then derive the sobject type DescribeSObjectResult describeResult = Account.getSObjectType().getDescribe(); List<String> fieldNames = new List<String>( describeResult.fields.getMap().keySet() ); String query = ' SELECT ' + String.join( fieldNames, ',' ) + ' FROM ' + describeResult.getName() ; // return generic list of sobjects or typecast to expected type List<SObject> records = Database.query( query ); System.debug( records );
but get the error: The query has to start with find or select?
Where exactly does that go?
Getting very frustrated. In my object I have 3 fields I want to extract. Name, Account_Owner and Wholesale_Name. Name and Accout_Owner are strings but Wholesale_Name is a reference. So I want to return:
Name Account Holder Wholesale_name
ABC John Doe ABC Company
Select Id, Wholesale_Account__r.Name From ACNA__c thanks so much!!!!!
All Answers
I've created something similar in the past. You will find the code below. it will return a query string with all the fields of the object, except the fields listed in the fieldSet variable. Add all fields you want to exclude to this set. Please keep in mind that this code will produce errors if the API version of the class that contains this code is different from the API version of the class calling it.
For example, let's say that Salesforce introduced a new field in API v35. If the code listed above runs in version 34, that new field will not be retrieved. On the other hand, if the above code runs in v35 it will return the new field. But if the class that is calling this code is in v34, it will get the new field, but will not recognize it and produce an error.
So if you want to use this functionality, you will have to keep the API versions in sync.
Also, this is NOT a best practice. There is a reason that Salesforce does not support Select * From Object. Since your Salesforce org is part of a multi-tentant architecture, you share resources with other tenants. Querying fields you will not use is only using system resources that could otherwise be used by other tenants.
I would advise you to write SOQL Queries for each individual task, and reuse them whereever possible.
Hope this helps!
Best regards,
Martijn Schwärzer
Ps. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
This will return the Name of the Account in the Primary_Account__c field. The opposite of this would be looking up things from the Account. I would be looking for the list of Orders for the Account. I can do this a couple of ways. First if I want to do it directly from Orders I can do this:
If I am doing an account query I can do a subquery to reference the relationship like this:
This one is a little weird. Notice the name of the relationship in this dircection is PrimaryAccountOrders__r. If you look at the definition of the lookup field, you can see the Relationship Name. In this case the Primary_Account__c field on my Order__c object has a relationship name of PrimaryAccountOrders. This name is set when the lookup field is createds. I hope this helps. For more info on SOQL Relationships see:
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships.htm
but I get an error message saying that it doesnt understand the relationship Wholesale_Account__r.
If so, how about this:
The first query: Select Id, Name,Wholesale_Account__c from ACNA__c Where ID = 'Account ID'
throws an error of: Invalid ID field: Account ID
The second query: Select Id, Name from Wholesale_Account__c
works but what is represented in the Wholesale_Account__c field is the unique Id for that field. Is there some sort of transformation of going up or down a level(s) with __c to __r or vice versa? Im so new to this (3rd day) Im so lost.
If that and the second query works,
should work. The __r tells SOQL that you want to reference the object related to the ID stored in that field.
Select Id, Wholesale_Account__r.Name From ACNA__c thanks so much!!!!!