0

I have this problem with query. I have a trigger on the Account object. When it gets updated I get the IDs from trigger and query all the related Billing Account objects. Then from Billing Account object I would like to query User.Name field. Billing Account has a 'many to one' relationship to Account and User has a 'lookup' relationship to Account. Any suggestions on how can I do this? I'm trying to get 2 lists one with ZIDs which is on Billing Account object and another with names from the User object. For example I have 2 Billing Accounts on Account. If I query Billing Account object I get those 2 IDs. And if I query User object I get just one name and list get out of sync if there are many Accounts entering the trigger. That's why I'm trying to do the query from Billing Account object and get the relationship BillingAccount.Account.User.Name

enter image description here

Thank you for your time!

Nerijus

1

So you can shape this query in a couple of different ways, depending on your specific needs - I confess to not fully following your description. One route is to do a Parent-Child query from Account, and include a relationship traversal in the child query:

SELECT Id, OwnerId, ... other Account fields..., 
       (SELECT Id, OwnerId, Owner.Name ... FROM Billing_Accounts__r) 
FROM Account 
WHERE Id IN :Trigger.newMap.keySet()

That gets you a List<Account> result, where each entry in the list has a property Billing_Accounts__r that is typed as a List<Billing_Account__c>, and each of which has an OwnerId property with the Billing_Account__c's owner, and optionally relationship properties like Owner.Name if you choose to query them.

Note that I'm using Trigger.newMap.keySet() to query against all of the Accounts in your current trigger invocation; that may or may not be what you mean, but you could substitute in a filtered Set of Ids.

Another route with less indirection is to formulate the query directly against the child Billing_Account__c object, which gets you back a flat List<Billing_Account__c>:

SELECT Id, OwnerId, Account__r.OwnerId
FROM Billing_Account__c 
WHERE Account__c IN :Trigger.newMap.keySet()

There you simply filter on your lookup field from Billing_Account__c to Account and can look at Owner.Name for each Billing Account.

This is the way I'd go if what you're trying to do is update child object owners to match parent account owners. Then you just iterate over the list and assign eachBillingAccount.OwnerId = eachBillingAccount.Account__r.OwnerId, and perform the update DML.

  • Thank you, David, for reading this. The problem is that Billing Accounts do not have correct owner. It's API user who creates those objects that's why I'm trying to get correct owner from Account object. – Nerijus Oct 16 '18 at 16:16
  • SELECT Zuora__Zuora_Id__c, LastModifiedById.Name FROM Zuora__CustomerAccount__c WHERE Zuora__Account__c IN :Trigger.newMap.keySet() If I execute this query I get "Didn't understand relationship 'LastModifiedById' in field path" but field exists with lookup relationship. Am I missing something? – Nerijus Oct 16 '18 at 19:19
0

Ok so I figured this out. So simple... My query looks like this:

SELECT Zuora__Zuora_Id__c, Zuora__Account__r.Owner.Name
                 FROM Zuora__CustomerAccount__c 
                 WHERE Zuora__Account__c IN :Trigger.newMap.keySet()

The problem was that I was trying to access Name through Zuora__Account__r.OwnerId.Name and not Zuora__Account__r.Owner.Name Still learning and it's amazing trip :)

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.