SOQL query to traverse several levels on Task - salesforce

I'm trying to write some apex code using this query and not getting anywhere:
List<Task> tasks = [SELECT id, whatid, who.account.parent.name FROM task WHERE who.account.parent.name LIKE 'Procter%'];
I'm not surprised this doesn't work, but can't seem to find documentation anywhere that explains how I would go about this. Does anyone have any idea? I'm trying to get all tasks linked to a contact linked to an account with a parent account of "procter and gamble"...

Looks like the options to "go up" in the mixed fields (the ones where Lookup goes to multiple objects like WhatId going to Account or Opportunity) are very limited. I was able to write "WHERE what.name LIKE 'Procter%' but not "WHERE what.parent.name LIKE 'Procter%'".
By the way I think it should be WhatId and not the WhoId (check out the Validation Rule editor for Tasks, try to insert fields "Contact/Lead ID" and "Opportunity/Account ID"). You will also see that you can't "go up" (or in case of this editor - "go right") on these fields while for some other fields you can explore the relation like for "CreatedBy.UserRole.Name".
Can you try this subquery instead?
[SELECT id, whatid FROM task WHERE whatid IN (SELECT Id FROM Account WHERE Parent.Name LIKE 'United%')]

WhatId and WhoID are polymorphic fields, so these fields do not support traversing multiple levels.

I had a similar requirement for hierarchal traversing, in my case i had to select the lead information associated with a task. What I had to do was first query for a list of tasks, then query for a list of leads based off those tasks, then use a wrapper class to combine the two lists into a custom object and display the object accordingly using visualforce. The wrapper class was certainly my answer to the equation since it seems you are in fact unable to query directly using the Who.Id.
Hope this helps!

The AccountID field on Tasks is populated by SF automatically for Contacts. Obviously, it is blank for Leads. So, if you want to get Account data you can just do something like this:
SELECT ID, Who.FirstName, Who.LastName, Account.Parent.Name FROM Task WHERE WhoID = '00Q12SDFUUALDLKJF'
Obviously, Account.Parent.Name is blank for Leads.

Related

Access a field from another table using the object relation

I am new to SalesForce and SOQL so sorry in advance if the question has already been answered, if yes link it to me.
The aim of my SOQL query is to get all the contract information to generate PDF.
There are tables: Contract, Contact and Account
In the Contract table there are fields: Maitre_d_apprentissage__c, MaitreApprentissageNom1__c, MaitreApprentissagePrenom1__c, Apprenti__c, ApprentiNom__c, ApprentiPrenom__c
There are relationships:
Apprenti__r which link Apprenti__c to Contact table
Maitre_d_apprentissage__r which link Maitre_d_apprentissage__c to Contact table
When I looked at table, I saw that MaitreApprentissageNom1__c was equal to Maitre_d_apprentissage__r.LastName and ApprentiNom__c was equal to Apprenti__r.LastName. So I conclude I could get other information of Apprenti__c and Maitre_d_apprentissage__c from the Contact Table following the same principle. So I added to my query Apprenti__r.Date_de_naissance__c and Maitre_d_apprentissage__r.Date_de_naissance__c to get the Date_de_naissance__c field which is in my Contact table.
I see in the results that the query succeeds in getting the information but some values have changed column (lines 6 and 7), you can see the difference between query 1 and query 2. In the first query I only return the Apprenti__r.Date_de_naissance__c and in the second query I return Apprenti__r.Date_de_naissance__c and Maitre_d_apprentissage__r.Date_de_naissance__c
Query 1:
SELECT ApprentiNom__c, ApprentiPrenom__c, Apprenti__r.Date_de_naissance__c, MaitreApprentissageNom1__c, MaitreApprentissagePrenom1__c
FROM Contract
Result 1:
Query 2:
SELECT ApprentiNom__c, ApprentiPrenom__c, Apprenti__r.Date_de_naissance__c, MaitreApprentissageNom1__c, MaitreApprentissagePrenom1__c, Maitre_d_apprentissage__r.Date_de_naissance__c
FROM Contract
Result 2:
I would like to understand from where is coming the problem and how to correct it. Thank you in advance.
It's possible that it's just your query editor displaying stuff incorrectly. You can see it got confused with 2 lookups to Contact table, why there's even a column header "Contact.Date_de_naissance__c" (and why it's there twice). And they aren't shown in the order you requested...
What editor you're using? You could try built-in "Developer Console" or http://workbench.developerforce.com/
What do you need it for? In Apex order of fields won't matter, in REST API query the values fetched via lookup will come as JSON sub-objects so there will always be a way to figure out exactly which value is coming from which relation.
In Dev Console try to run this and check if it solves your fears:
System.debug(JSON.serializePretty([SELECT
ApprentiNom__c, ApprentiPrenom__c,
Apprenti__r.Date_de_naissance__c,
MaitreApprentissageNom1__c, MaitreApprentissagePrenom1__c,
Maitre_d_apprentissage__r.Date_de_naissance__c
FROM Contract]));
Then add Maitre_d_apprentissage__r.LastName to query and see what changed, what stayed as is.

Query of Arrays in Salesforce

I need to do 1 of two things (I believe):
1- Get a Custom Object ID so I can query it directly
2- Get a list of values of a specific field within the Object entries.
Ultimate End goal:
Add and modify rows in my custom object via external API. However to do this I need to check and make sure my new entry/row does not already exist.
What I have:
I have a custom object (called Customer_Arrays__c). It is a table that I can add new rows to (I will call entrys). Each entry has 6 or 7 fields. 1 of these fields is called (external_ID__c). This is the field I utilize to match to new incoming data to see if the entry already exists, or if it needs to add a new row to my table. This Customer_Arrays__c is a child to my opportunity I believe – it is part of every opportunity and each line item I add has a field defaulted to the opportunity.
Help I need:
1- How do I query the value of my Cutomer_Arrays__c based upon an opportunity ID?
2- How do I query a list of values in my (external_ID__c) based upon an opportunity ID?
Thanks for your help! I have read half a dozen+ posts on similar topics and am missing something. Examples of some Past try's that failed:
Select external_ID__c,FROM Custom_Arrays__c WHERE Opportunity='00...'
Select Id (Select ID, Custom_Arrays__c from Custom_Arrays__c) from Opportunity where id ='00...'
List FROM Custom_Arrays__c WHERE Opportunity='00...'
Select Id, external_ID__c, (Select external_ID__c FROM Custom_Arrays__c) WHERE Opportunity__c='00...'
Thanks again!
Only you know how did you name the lookup field (foreign key) from arrays to Opportunity. You'll need to check in setup, next to where external_ID__c is. Since it's a custom field (gets __c at the end), my guess is you went with default.
Try
SELECT Id, Name, External_Id__c
FROM Customer_Arrays__c
WHERE Opportunity__c = '006...'
Thank you eyescream, that got me almost all the way there. Turns out I also needed a __r for the parent child relationship.
Here is a snip out of my final code that works - I think it covers everything:
SELECT Field1__c, Opportunity__r.Id, Opportunity__r.Opportunity__c,
FROM Customer_Arrays__c
WHERE Opportunity__r.Id = '006...'.
Thank you so very much!!!

Salesforce apex collaborationgroup/collaborationgroupmember relationship?

I'm trying to get a list of collaboration groups and their associated members. I'm trying to do the following soql query but it does not recognise the CollaborationGroup.CollaborationGroupMembers relationship
List<CollaborationGroup> cgs = new List<CollaborationGroup>([Select OwnerId, Id, (select CollaborationGroupMember.MemberId from CollaborationGroup.CollaborationGroupMembers) From CollaborationGroup]);
Looking at the API, CollaborationGroupMembers has a CollaborationGroupId which is the ID of the associated CollaborationGroup, so the relationship should exist - can anyone tell me why it's not working?
Thanks
J
edit: I have figured this out the other way round (going from member to group (child to parent) rather than vice-versa) by doing the following:
List<CollaborationGroupMember> cgs = new List<CollaborationGroupMember>([select MemberId, CollaborationGroupMember.CollaborationGroup.OwnerId from CollaborationGroupMember where CollaborationGroupMember.CollaborationGroup.Name]);
However, I'd still like to know why I couldn't do it from parent to child as I was trying to in the first place?
Thanks
As per the documentation here, it's not having a relation from CollabarationGroup into CollabarationGroupMember. But as you have mentioned, it's having the relation in the otherway around, see this. So if you need to collect members from a particular group you can try
SELECT MemberId FROM CollaborationGroupMember WHERE CollaborationGroupId='your_group_id'
or filter it from CollaborationGroup.Name as you are already doing.

How to query all task which are having relatedto(whatId) as Account or Contact

I am using an soql query to get tasks. My requirement is to get all tasks which are related account or contact object. Also few other fields inside Account or contact object whichever the object related. Is there a simple way instead of writing multiple queries.
Please provide more specific information next time. Generally speaking you can refer to a parent object by reference name followed with a dot. Here is an example
Select Account.Name, AccountId From Task Where Account.Name = 'John'
Here Account is the name of the reference (from task) and AccountId is the referencing field.
Your question is a little unclear. Are you looking for all TASKS related to any ACCOUNT or CONTACT or related to specific ACCOUNTS or CONTACTS?
If the former, try
SELECT Id, Subject,
FROM TASK
WHERE
What.Type = 'Account' OR
Who.Type = 'Contact'
if the latter, use the IN :list syntax already suggested by Moti. The What.Type is useful, as opposed to AccountId!=null, because it will not return TASKS associated with, for example, OPPORTUNITIES (if you want that behavior, use AccountId!=null and maybe drop the Who.Type if you associate all CONTACTS with OPPORTUNITIES, as it would be redundant).
In either case, your issue will be pulling specific data from CONTACT whos, as polymorphic fields only allow access to a limited number of fields. Can't seem to find that list right now. I don't believe old SOQL supports the kind of the syntax to do that in one query--that's why SOQL polymorphism made such a bang--though I could be wrong.
Now, if you have SOQL TYPEOF available to you, you should be able to do something more interesting like:
SELECT Id, Subject,
TYPEOF What
WHEN Account THEN AccountNumber
END,
TYPEOF Who
WHEN Contact THEN FirstName
END
FROM Task
WHERE
What.Type = 'Account' OR
Who.Type = 'Contact'
How about
select {column list] from task where parentid in (select id from account where ...) or parentid in (select id from contact where ...)
Alternatively, if you're inside Apex and already have the contact or account ids in a list (we'll use idList), you can use:
select {column list] from task where parentid in :idList

Using Salesforce Apex to find a record and pull data from a field

In salesforce we have two objects. The first is a Component pricing object (Comp_Pricing__c). This has records with component part numbers and pricing in their respective fields, Part_Number & Pkg_Price__c. We also have an object in which we put together proposals with quotes. In this object we use an apex class and trigger to run our calculations to determine quantities of parts needed. We would like to have the apex class, based on a variable (apPart), search through the records, find the corresponding part number and then pull back the price for use in further apex calculations. I believe I will need to run a query on the records but have no idea how to do this. Can I get some help?
list cpFTList = [SELECT Pkg_Price__c FROM Component_Pricing__c WHERE Component_Pricing__c.Part_Number__c = :PRT_Pr_Ft]; Pr_Ft = cpFTList[0].Pkg_Price__c;
This is the final query that works.
Thanks Chiz for the help.

Resources