2

I want to return the account name for each task in a SOQL query, am I correct in saying the relationship is that the Account is the parent object and the Task is the child object? I am not sure who to create my SOQL syntax to carry out this query, here is an example of what I have but I get a fail because the relationship Accounts__r doesn't seems to exist:

SELECT id, activitydate, accountid, (SELECT Name from Account__r) 
FROM task 
WHERE isrecurrence = false 
ORDER BY activity date ASC

3 Answers 3

4

The name of relationship for the standard object and for the custom object is different.

  1. Standard object (List of all standard objects)

    For standard objects like Account or Task the form of the relationship name is depending on the direction of the relationship:

    • Child-to-Parent

      The relationship name to the parent is the name of the foreign key:
      SELECT Contact.FirstName, Contact.Account.Name from Contact

    • Parent-to-Child

      The parent object has a name for the child relationship that is unique to the parent, the plural of the child object name

  2. Custom object

When you use a relationship Name in a query, you must use the relationship names without the __c. Instead, append an __r (underscore underscore r)

So in your case you don't need to use __r because Account is a standard object.

To understand it better you can read more about relationships: Understanding Relationship Names

2

It should be this:

SELECT id, activitydate, accountid, Account.Name 
FROM task 
WHERE isrecurrence = false 
ORDER BY activity date ASC

The __r is for custom objects. Salesforce uses it's own names for standard Objects. Also it is a master detail. There is only 1 account for an activity.

0

Try this way:

[Select Id, Name,
    (Select Id, Subject from Tasks) 
    from Account]

or

Task[] tasksList = [SELECT Id, Subject, ActivityDate, Owner.Name FROM Task where accountid in ( SELECT id FROM Account)];
1
  • 1
    A bit more explanation would be great. Oct 28, 2013 at 10:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .