3

I am still new to Salesforce and haven't quite wrapped my head around SOQL queries yet. I still tend to think more in terms of SQL. Anyway, I am trying to write a statement that gives me all "Call Tasks" that are attached to all Opportunities of an Account.

SELECT Id, (SELECT Id FROM Opporunity, (SELECT Id FROM Task WHERE Type="Call") FROM Account

Any ideas in how to make this work? Tia.

1 Answer 1

4

This is one way:

select Id, Subject
from Task
where WhatId in (select Id from Opportunity where AccountId = :accountId)
and Type = 'Call'
order by Subject

The Relationship Queries documentation is well worth reading.

PS

On the subject of where to get the Account ID, probably the simplest way in a controller extension is this, though in the "new" case there won't be an ID hence the alternate logic:

public with sharing class MyController {
    private Id accountId;
    public MyController(ApexPages.StandardController sc) {
        accountId = sc.getId();
    }
    ...
    public Task[] getTasks() {
        if (accountId != null) {
            return [
                    select Id, Subject
                    from Task
                    where WhatId in (select Id from Opportunity where AccountId = :accountId)
                    and Type = 'Call'
                    order by Subject
                    ];
        } else {
            return new Task[] {};
        }
    }
}
12
  • 1
    This would work but you need to add the additional WHERE clause of Type = "Call" Commented Nov 12, 2014 at 14:31
  • @JesseAltman Woops missed that thanks. Will add it.
    – Keith C
    Commented Nov 12, 2014 at 14:32
  • Thank you. I am planning to show the results on a Visualforce page, and now I am wondering whether I need to create a controller extension for the page, as I cannot see anywhere to edit the standard controller.
    – Lily
    Commented Nov 12, 2014 at 15:00
  • @Lily Yes controller extensions are for exactly this purpose, where you have some custom logic to use in addition to the custom controller. The extension can be as small as a few lines of code.
    – Keith C
    Commented Nov 12, 2014 at 15:20
  • @KeithC - I am not sure where the accountID variable comes from?
    – Lily
    Commented Nov 13, 2014 at 14:54

You must log in to answer this question.

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