I have two objects, Financial_Account and Client; they are connected with master-detail relationships to Client_Financial_Account_Relationship object. In a trigger on 'Financial_Account' I want to compare a field from Financial_Account and one on Client. My guess is to query for the junction object where Financial_Account_Id is equal to the one in my loop. Am I right there, is this the best way ? If so then how can do I get a list of all the associated records ?

I'm thinking something like this:

public static void checkOfferEnrollmentDateChange(List<Financial_Account__c> financialAccounts) {

        List<Client_Financial_Account_Relationship__c> relatedClients = 
 [Select ID,Customer__c,Financial_Account__c From Client_Financial_Account_Relationship__c Where IN :financialAccounts];


Junction object in Salesforce

Change account ownership and its affect on related records

1 Answer 1


You're on the right track, you just need to query the associated parent records on each side:

List<Client_Financial_Account_Relationship__c> relatedClients = 
 [Select ID,Customer__r.SomeField__c,Financial_Account__r.SomeField__c From Client_Financial_Account_Relationship__c Where Financial_Account__c IN :financialAccounts];
for(CLient_Financial_Account_Relationship__c rel: relatedClients) {
  if(rel.Customer__r.SomeField__c == rel.Financial_Account__r.SomeField__c) {

Of course, this is just hypothetical code, but it should get you started.

  • Thanks ! for posterity: it's 'Where Financial_Account__c IN :financialAccounts] '
    – MadMax
    Jun 21, 2018 at 21:35
  • @madmax I just copied that line direct from your question, but I'll toss that in, too.
    – sfdcfox
    Jun 21, 2018 at 21:37
  • Can you update object through the relationship__r field? As in: ' update rel.Customer__r; '
    – MadMax
    Jul 2, 2018 at 21:53
  • @madmax It's unusual to do that, but it should work. Ideally, you should put all records you intend to update in to a list.
    – sfdcfox
    Jul 2, 2018 at 22:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .