function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Elan Cramer 8Elan Cramer 8 

SOQL statement on a Custom Object

I have a custom object called University/Company. On the custom object is a lookup field to an account object called Company. I'd like to write a SOQL query that pulls the task information from this Company object via the University/Company object. Is this possible? I'm relatively new to SOQL, so any help would be greatly appreciated. Thanks!
Best Answer chosen by Elan Cramer 8
ApuroopApuroop
Try this please,
SELECT Id FROM Task WHERE WhatId IN (SELECT Company__c FROM University_Company__c)
Adjust the spellings of the custom field and the object.


For reference, getting the tasks from Opportunity:
SELECT Id FROM Task WHERE WhatId IN (SELECT AccountId FROM Opportunity)

All Answers

ApuroopApuroop
Try this please,
SELECT Id FROM Task WHERE WhatId IN (SELECT Company__c FROM University_Company__c)
Adjust the spellings of the custom field and the object.


For reference, getting the tasks from Opportunity:
SELECT Id FROM Task WHERE WhatId IN (SELECT AccountId FROM Opportunity)
This was selected as the best answer
Elan Cramer 8Elan Cramer 8
Thanks Apuroop! This is what I was looking for. 

One other quick question--on the custom object, University/Company, there's a custom field called University. Do you know if there's a way to include that column in the output?
ApuroopApuroop
Is there a relationship between this custom object and the Task?

Also, mark a best answer if you think this question is solved. :)
Elan Cramer 8Elan Cramer 8
I don't think the custom object is directly related to the Task. On the Task, there's the Related To field, which is an account. The account is related to the custom object becuase the account is a lookup field on the custom object. Please let me know if there's anything I can clarify. Thanks!
ApuroopApuroop
I don't think you can get that University field in the SOQL statement above. You can do Parent - Child - GrandChild querying. (Not sure how deep you can go). In your case, Account has two different related objects, one parent having two different childs. I hope am making sense.

Question is why do you even need University field when you're querying tasks from an Account?
Elan Cramer 8Elan Cramer 8
I'm looking to include the University field because I'd like to use the University field to filter. In other words, I'd like to pull tasks from an account related to a specific university. The University ID isn't on the task or the account. It's on the University/Company custom object. Do you think this is possible?
Elan Cramer 8Elan Cramer 8
I thought doing something like this would solve it, but it doesn't return any records:
SELECT Id FROM Task WHERE WhatId IN (SELECT Company__c FROM University_Company__c WHERE University_ID__c='001G000000stRptIAE')
 
Elan Cramer 8Elan Cramer 8
Hi Apuroop, did you get a chance to read my message above? 
ApuroopApuroop
Sorry Elan, lost the notification on this.

Does this query return any records? I don't think so, because you're giving an account Id to check against the University_Id__c. Get the correct University_Id__c which has an accountId.
SELECT Company__c FROM University_Company__c WHERE University_ID__c='001G000000stRptIAE'
If yes, this below query should workfine. As long as the Company__c is returning the Account Ids, we should be good. That's what the WhatId on task is looking for.
SELECT Id FROM Task WHERE WhatId IN (SELECT Company__c FROM University_Company__c WHERE University_ID__c='001G000000stRptIAE')
Elan Cramer 8Elan Cramer 8
Got it. Thanks for your help Apuroop!