Is it possible to write a query to access parent-child and child-parent objects, in one SOQL query?
I have a scenario where, I need to access Account Objects from child and child of Account too, in the same query.
Example:
Select Id,
(Select Id,Name, (Select Id, Address from Addresses__r) from x__r.Account),
x__r.Account.Name
From x
.
(Pardon me, if I use any wrong terms. I am pretty new to Salesforce)
Yes, it is possible to do so.
In the example below, we are using sub-query to retrieve Ids of all Account Territories (parent-to-child relationship) and, at the same time, we are using child-to-parent relationship to retrieve TimeZoneSidKey of the User who has created the record.
SELECT Id, (SELECT Id FROM Accounts_Territories__r), CreatedBy.TimeZoneSidKey FROM Account
Documentation on the topic
Related
I am trying to fetch some entities with distinct values on a property. Say, I have an entity called messages. And it has some properties say personId, typeId, convId, createdTime, etc. I want fetch messages of personId p1 with distinct convIds. How can I do that.
I already referred to
Executing DISTINCT query with objectify for app engine
and some others. And tried something like this.
ofy().load().type(messages.class).limit( 10 ).filter("personId ==", "p1").order("-createdTime").project("convId").distinct(true).list();
I am sure that there are some entities with this combination. But it is fetching no entities.
Please help me with this.
Unfortunately, your query isn't valid. For projection queries you need distinct to be on the values that you are sorting by (i.e. -createdTime). To use distinct you'll need to order by convId.
https://cloud.google.com/datastore/docs/concepts/queries#restrictions_on_queries has more details.
I am using Solr 6.4.2. I have defined 2 cores:
companies, with the fields 'Id, Town, Name, Type, ManagerId'
users, with the fields 'Id, Login, ManagerId, Email'
In users core the field ManagerId is parent-child relation (ManagerId->Id).
Companies and users are related by companies.ManagerId->users.Id
I am trying to build a very simple join query:
{!join from=ManagerId to=Id fromIndex=users}Login:someuser1
url looks like:
?q=*:*&fq={!join%20from=ManagerId%20to=Id%20fromIndex=users}Login:someuser1
nothing works, I always get zero results. I just want to understand how Solr join works. It seems to me that there is a big difference in understanding between Solr joins and SQL joins.
In fact I want to do the queries like:
get all docs from users by company Types
get companies by user managers
Right now I always get zero results no matter how I write the join query.
Try this on the company core (assuming you want the get all companies run by a certain manager login):
login:someuser1 is the filter condition you put on the child table, this should be the manager login you are looking for
from=ManagerId should be the id on the child table, so this is wrong
to=id is the field on the parent table that relates to the child table, so this is wrong
fromIndex is the child table, this is correct
{!join from=ManagerId to=Id fromIndex=users}Login:someuser1
I'm creating a program that manages a manufacturing plant, and I need to show each product's traceability (all the paths it took from creation until it's final delivery.
Let me show you an example:
The plant creates the document A001, with quantity 400.
Then, they need to split the product, creating documents B002 and B003, both with quantity 200 and both with their Parent field value of A001.
After that, they'll split B002 into smaller pieces. That creates documents C004, C005, C006 and C007, all with quantity 50 and all with the Parent field value B002.These smaller pieces can also be split again...
Now, if I wanted to trace the full cycle of document B002, I'd check the Parent field and cross it with the document field to get that info, and then get the documents where the Parent field is B002. That's the "easy" part.
Now the tricky part.
I want to know the full cycle of document C007. I'd have to check his parent, and get the B002 document, THEN have to get that document's Parent and get the A001 document. I'd also check for documents with Parent C007 and find none.
Or know the full cycle of document A001. I'd check if there was any Parent (there won't be), they i'd have to get all the documents with Parent A001, then get all documents with Parent B002 and B003 and so on.
Is there any function on SQL that let's me do this, or do I have to create a procedure that recurs itself over and over to check for both parents and childs? And if so, I have no idea what to do, so any help would be appreciated.
Basically you ask for something simple that has been done thousands of times - find the root of a tree.
There are various approaches to that, among other things a special data type (HierarchyId) that supports that right in SQL Server.
https://msdn.microsoft.com/en-us/library/bb677290.aspx
is the documentation for this.
That said, you likely will use a normal field as ID - and then the best approach is a stored procedure.
http://vyaskn.tripod.com/hierarchies_in_sql_server_databases.htm
has some thoughts about it - as has google tons of it (there are various approaches to query them).
http://blog.sqlauthority.com/2012/04/24/sql-server-introduction-to-hierarchical-query-using-a-recursive-cte-a-primer/
is from quite a reputable source and using a CTE Like this:
WITH MyCTE
AS ( SELECT EmpID, FirstName, LastName, ManagerID
FROM Employee
WHERE ManagerID IS NULL
UNION ALL
SELECT EmpID, FirstName, LastName, ManagerID
FROM Employee
INNER JOIN MyCTE ON Employee.ManagerID = MyCTE.EmpID
WHERE Employee.ManagerID IS NOT NULL )
SELECT *
FROM MyCTE
I have the following three custom objects:
Order__c
Design__c (has a lookup to Order and a lookup to Location, so the design ojbect is the junction object)
Location__c
On the order page layout I want to add a blank section that contains a VF page in order to display the Location records for all the design records for an order.
An order can have many designs and a design can have many locations. I need a way to group and display each design/locations in the VF page on the Order.
How can I build this query and display the results on the VF page?
I was trying a query like this: Select Location_r.Name, Location_r.Mockup From Design_c where Order_c = 'xxxxxxxxxxxxxx'
Also, is there a better way to display the results besides a VF page section in a related list?
Thanks for any help!
Regards.
First things, first. since design is related to the other 2 objects via lookup, it is not a junction object. junction objects are only when it's 2 parents have a master-detail relationship with the child.
I think what you're trying to achieve is to traverse a parent-child-parent relationship. You'll need to use subqueries for the last half. From the documentation- here you go: http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_relationships.htm
Query **child-to-parent relationships**, which are often many-to-one. Specify these relationships directly in the SELECT, FROM, or WHERE clauses using the dot (.) operator.
For example:
SELECT Id, Name, Account.Name
FROM Contact
WHERE Account.Industry = 'media'
This query returns the ID and name for only the contacts whose related account industry is media, and for each contact returned, the account name.
Query **parent-to-child**, which are almost always one-to-many. Specify these relationships using a subquery (enclosed in parentheses), where the initial member of the FROM clause in the subquery is related to the initial member of the outer query FROM clause. Note that for subqueries, you should specify the plural name of the object as that is the name of the relationship for each object.
For example:
SELECT Name,
(
SELECT LastName
FROM Contacts
)
FROM Account
The query returns the name for all the accounts, and for each account, the last name of each contact.
I have an issue where I need to find the top level parent account for a child. The best I've been able to do is filter up from the child w/
SELECT id, name, parent.id, parent.parent.id, parent.parent.parent.id,
FROM Account WHERE id=CHILD_ID
Then iterate through until you find a null parent.id indicating top level account. Not very elegant and doesn't guarantee "top level" account, I would have preferred something like
SELECT id, name,
(SELECT id, name,
FROM Accounts WHERE id = CHILD_ID)
FROM Account WHERE parent.id = null
But that doesn't work as salesforce apparently does not let you traverse parent-child relationships from account to account. Anyone have any suggestions here?
You're right - there's no way to do this in a single SOQL query. Note that your second, desired, query wouldn't give you the correct results either (it would only return a record if the child account's immediate parent had no parent).
Your best bet is to do what you said in your first code block. You can traverse relationships up to 5 objects deep, so you can include parent.parent.parent.parent.parent.id in your SOQL select. Iterate through the fields - if none of them are null, then issue a second SOQL query that changes CHILD_ID to parent.parent.parent.parent.parent.id's value. That way you're guaranteed to eventually find a parent with no parent (since salesforce guarantees no cycles).
You could make this more elegant by only ever selecting parent.id in the query and doing more individual queries, but that will run you up against API/governor limits, so it's probably not a great idea.
If you could use a custom object instead of Account, you could do something like the second query, since you would be able to traverse the parent-to-child relationship from that custom object to itself, but I'm still not sure there's a single query that will give you what you want.
I have run into this many times at customer sites. The best solution I've found is a custom field called "Ultimate Parent" or sometimes called "Legal Entity", but basically the same thing. We used triggers to do the traversal.
The basic idea is that whenever you create an account that is a child account, you copy the "Ultimate Parent" field into the child record.
Usually, we setup the field as a lookup so that queries like yours are much easier. It also allows you to do some very interesting data visualization. For example, by using the parent lookup and the ultimate parent lookup fields, you could find accounts that are siblings, cousins, seconds cousins, etc.