How can we list out records based on soql query of to unrelated Sobjects?

  • 1
  • Question
  • Updated 4 years ago
I have an object, lets say Team, which is used to indicate which team  a user belongs to. My requirement is that I have to find all the users that belong the team that the logged in user belongs to and then retrieve records from another unrelated custom object whose owner are members of the same team.

I tried using sub-query but it I cannot find how to relate conditions from different models.
Photo of Paul Mathew

Paul Mathew

  • 70 Points

Posted 4 years ago

  • 1
Photo of J.

J., Official Rep

  • 7,470 Points 5k badge 2x thumb
Paul,

If I understand the scenario correctly, our "Field from another model" conditions will work. It sounds like this is what you were trying to do anyway, but here's the strategy, just in case:
  1. Start with a model on Team (call it UserTeam) that has aUserInfo of page viewercondition. Include your Team field (call it Team__c) in the fields that this model returns.
  2. Create a model on your related object with a subquery condition on OwnerId that selects Team.User__c where Team is UserTeam.Team__c.
"Field from another model" conditions are covered in section F of that tutorial. Make sense?
Photo of Rob Hatch

Rob Hatch, Official Rep

  • 44,006 Points 20k badge 2x thumb
Actually,  I think Pauls scenario is a little deeper.  But still solvable with the Field from another model strategy.  It just needs an intermediate step. 

The first model is correct.  Return the "Team" that the current user is associated with. 

The missing intermedate step is another model.  We'll call is "TeamMembers"  It will return all Users who have the Team that was returned in the first model.  Use a field from another model condition to get this list. 

Finally build a model on your unrelated object.  Here your condition will be to include records where OwnerId is  IN  the values returned by the "TeamMembers" model.  Be careful to look for the "IN" item in the operators list. 

A final note.  The order in which your models appear in the builder is important.  The queries are run in the order in which the models appear.  So if your TeamMembers model comes before your CurrentUser model it will not have data to correctly set its condition.  You can drag and drop models to get them in the right order.