0
votes

I have a free account in salesforce and learning to program in APEX. I was looking back at sample data and have a question regarding how to write a query in which the field name has a space in it. Unlike in a traditional SQL statement, SOQL does NOT allow the use of square brackets to delimit a field with a space in the field name. Using one example from the list of standard tables that comes with the Sales tables, I am hoping that the explanation here will apply to any situation in which the field name contains a space.

For my example, the "Account" table contains the field label of "Parent Field" and a field name of "Parent", yet all of the attempts below fail to achieve a functional query. Thanks, in advance, for any insight you can bring to this problem.

Select Name, Parent from Account
Select Name, [Parent Field] from Account
1

1 Answers

1
votes

Fields can't have spaces in their names in Salesforce.

You'll write your queries using the API name of the field, which looks like ParentId, or My_Custom_Field__c. The field label is never used in SOQL.

The Parent relationship on Account has an actual field name of ParentId. Parent is the relationship traversed in a query that selects fields across that relationship, such as

SELECT Name, Parent.Name FROM Account