2

I have a query in my class where I'm trying to pull in the position name from a task that is being created on an application.

So I have Position, Application, and Candidate. The task is created on Application, and I would like to pull in the position people are applying to.

Here's the query:

public List<Task> contactTasks {
    get {

        return [SELECT Id, Subject, ActivityDate, WhatId, Description FROM Task 
                        WHERE Subject like 'Rejected%' AND WhoId =: application.Candidate_Contact__c
                             ORDER BY ActivityDate];

    }
}

Is there a way to grab the position?

Thanks.

1

There is a feature in the works that allows you to query this data. It is currently a preview feature and can't be used in production code (yet). You can see the documentation for TYPEOF. Without TYPEOF, you have to perform two queries; one to obtain the tasks, and another to grab the contact or lead. Conversely, you could use a sub-query to query from lead or contact and their tasks.

TYPEOF

SELECT Id, Subject, ActivityDate, WhatId, Description, TYPEOF WhoId WHEN Contact THEN Position__c END FROM Task WHERE WhoId = :application.Candidate_Contact__c

Sub-query

SELECT Id,Name,Position__c,(SELECT Id, Subject, Activitydate, WhatId, Description FROM Tasks) FROM Contact WHERE Id = :application.Candidate_Contact__c
3
  • Thanks! Can it only be done from Contacts or Leads? The Tasks are being created on the custom object Application__c
    – gato negro
    Oct 3 '13 at 17:18
  • For using WhatId, the same general principle applies. You can use subqueries and typeof on any object.
    – sfdcfox
    Oct 3 '13 at 17:58
  • Thanks, I saw that the feature is coming out in Winter 13 to allow the lookups.
    – gato negro
    Oct 10 '13 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.