Select id, Account.Type from opportunity__c
I create a custom object opportunity and make relational on Account__c and account data type lookup but above query does not run and give below error
"Select id, Account.Type from opportunity__c
^ ERROR at Row:1:Column:12 Didn't understand relationship 'Account' in field path. If you are attempting to use a custom
relationship, be sure to append the '__r' after the custom
relationship name. Please reference your WSDL or the describe call for
the appropriate names."
Sounds like account is a custom field, in which case the field is called account__c, and the relationship is called account__r so your query should be select id, account__r.type from opportunity__c
When you try to reach in a SOQL a field linked by a lookup or a Master-detail relationship you must put the suffix '__r' not the '__c'.
In your case, try with:
Select id, Account__r.Type from opportunity__c
Related
Is it possible to write a query to access parent-child and child-parent objects, in one SOQL query?
I have a scenario where, I need to access Account Objects from child and child of Account too, in the same query.
Example:
Select Id,
(Select Id,Name, (Select Id, Address from Addresses__r) from x__r.Account),
x__r.Account.Name
From x
.
(Pardon me, if I use any wrong terms. I am pretty new to Salesforce)
Yes, it is possible to do so.
In the example below, we are using sub-query to retrieve Ids of all Account Territories (parent-to-child relationship) and, at the same time, we are using child-to-parent relationship to retrieve TimeZoneSidKey of the User who has created the record.
SELECT Id, (SELECT Id FROM Accounts_Territories__r), CreatedBy.TimeZoneSidKey FROM Account
Documentation on the topic
I have 2 custom objects in Salesforce.com
One is PersonAccount and one is Accounts.
Within the default "Account" object I have a field called user_id
PersonAccount acts as a junction table to link "Account" to Accounts
PersonAccount does a lookup in Person for user_id Lookup(Account)
How can I build a query to check something in Account to find all the matching items in Accounts?
Currently, Salesforce only permits a single level of nested queries. It cane be done like the following:
[SELECT ID, Name, Field1 from Object__c WHERE Id IN ( SELECT Id FROM Object2__c WHERE Field2 = 'SomeValue')]
However, with the junction object you don't actually need to use a nested query.
Unfortunately, your description isn't clear enough to understand your specific object set-up, so I am going to make some assumptions.
You have three objects, Accounts__c (your custom Accounts Objct), PersonAccount__c (your junction object), and Account (the default Account objects).
The PersonAccount__c object contains two lookup fields (for a true Junction, they should be Master-Detail). The first is to Accounts__c (we will call that lup_cust_accounts__c). The second is to Account (we will call that lup_account__c). [As an aside it is a really bad idea to have an Accounts and Account object. It is going to screw you up because Salesforce will automatically pluralize words and then you will be confused as to which is which.]
Salesforce allows dot relationship lookups in SOQL queries. So if you want to get the ID and Name from custom Accounts Objects when the associated Account object's Name is like "Test", you could do the following:
[SELECT lup_cust_accounts__r.Id, lup_cust_accounts__r.Name FROM PersonAccount__c WHERE lup_account__r.Name LIKE 'Test%'];
Notice the double underscore r instead of double underscore c? That is how you indicate a reference (lookup) rather than the specific field value.
I am running the following query in salesforce to get the country
SELECt name, owner.country from lead
This gives the following error:
No such column 'country' on entity 'Name'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.
However this works just fine if I do
SELECt name, owner.id from lead
SELECt name, owner.username from lead
SELECt name, owner.name from lead
It gives the data
This happens due to the polymorphic relationship. If you have carefully read the error you are getting, you may have noted that the entity is Name, it's not User (as I also expected sometime back).
The Name object is used to retrieve information from related records where the related record may be from more than one object type.
For example, the owner of a case can be either a user or a group
(queue)
Who, What and Owner relationship fields will be used to access this Name object and it can't be accessed directly. And the error you are getting is due to this Name entity doesn't have a field called country as you can see in the documentation.
I have the following three custom objects:
Order__c
Design__c (has a lookup to Order and a lookup to Location, so the design ojbect is the junction object)
Location__c
On the order page layout I want to add a blank section that contains a VF page in order to display the Location records for all the design records for an order.
An order can have many designs and a design can have many locations. I need a way to group and display each design/locations in the VF page on the Order.
How can I build this query and display the results on the VF page?
I was trying a query like this: Select Location_r.Name, Location_r.Mockup From Design_c where Order_c = 'xxxxxxxxxxxxxx'
Also, is there a better way to display the results besides a VF page section in a related list?
Thanks for any help!
Regards.
First things, first. since design is related to the other 2 objects via lookup, it is not a junction object. junction objects are only when it's 2 parents have a master-detail relationship with the child.
I think what you're trying to achieve is to traverse a parent-child-parent relationship. You'll need to use subqueries for the last half. From the documentation- here you go: http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_relationships.htm
Query **child-to-parent relationships**, which are often many-to-one. Specify these relationships directly in the SELECT, FROM, or WHERE clauses using the dot (.) operator.
For example:
SELECT Id, Name, Account.Name
FROM Contact
WHERE Account.Industry = 'media'
This query returns the ID and name for only the contacts whose related account industry is media, and for each contact returned, the account name.
Query **parent-to-child**, which are almost always one-to-many. Specify these relationships using a subquery (enclosed in parentheses), where the initial member of the FROM clause in the subquery is related to the initial member of the outer query FROM clause. Note that for subqueries, you should specify the plural name of the object as that is the name of the relationship for each object.
For example:
SELECT Name,
(
SELECT LastName
FROM Contacts
)
FROM Account
The query returns the name for all the accounts, and for each account, the last name of each contact.
I need to get a list of users whose role has the first 5 char as 'Sales'
I want to use this on a apex class
My first thought was to use something like this
select id from user where UserRoleId__r like 'Sales%'
But this throws an error
No such column 'UserRoleId__r' on entity 'User'.
Do i need to query the role object to get all ids and then query the user object using those ids?
is there a better way of doing this?
Thanks
You need to fix the name of the User Role column in your search. Here's is the correct syntax:
select id from user where userrole.name like 'Sales%'
You only need to append the __r suffix to relationships in queries when the object is custom. For built in objects, you only need the name of the object (no __r).