function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rafi Pryntz-NadwornyRafi Pryntz-Nadworny 

Apex Trigger Help for Many to Many Relationships

I recently started working with a freelancer to build some Apex triggers, and I am finding it difficult to hold him to the solution we need. It would be beneficial to get 5 minutes of an experienced Apex developer to see if there's a better way to achieve our goals that would be easier for the freelancer to develop.

We need a way of showing related server update fields on the relevant contact records. However, since contacts can have multiple accounts, we need a way of picking which server update to show (the newest created) on a contact record. Your input is appreciated.
Map of custom objects and relationships between them.
Best Answer chosen by Rafi Pryntz-Nadworny
Paul S.Paul S.
Ah, I see.  He's just chaining after update triggers together in order to get from a server update to a contact record.  I also think I see why you've lost the multi-jorg account functionality...

"We have jorgs with close to a thousand jorg accounts."

This line of code, however, assumes a single jorg account for each jorg.  This is in the JorgTriggerHelper class.
List<Jorg_Account__c> JAList=[select id,name,Server_Update_Name__c,Jorsek_Organization__c from Jorg_Account__c where Jorsek_Organization__c in:Idset];
       
for(Jorg_Account__c j: JAList){       
    JAMap.put(j.Jorsek_Organization__c, j);
}
Each time the for loop encounters a new Jorg Account record where the Jorg Organization is the same as one that already exists in the JAMap, it simply overwrites the existing record with the new one.  This is because map keys must be unique.

What should be happening is to create a map that is Jorsek Organization => List<Jorg Accounts>, then loop through that in order to set the last server update field.  Once that occurs, you should see updates on all the relevant Jorg Account records which should then trigger updates on the appropriate contact records.

All Answers

Paul S.Paul S.
Rafi - what is the issue your developer is having?  Is there code you can post?
Rafi Pryntz-NadwornyRafi Pryntz-Nadworny

Hi Paul, 

He created 5 triggers and 13 classes so it would be a lot to post. I can give you an account on our sandbox if you want to see for yourself. 

The problem he is having is with the many-to-many relationship between Jorgs and Contacts. 

For contacts that just have 1 account, his trigger works fine. It's the contacts with multiple accounts that isn't working.

I'm not a developer myself so I can't judge his methods are more complicated (and fragile, he was fixing an Error: System.LimitException: Too many SOQL queries: 101 when the code broke) then they need to be.

Cheers
Paul S.Paul S.
5 triggers and 13 classes related to updating that one field? Or is this one requirement part of a bigger project?
Rafi Pryntz-NadwornyRafi Pryntz-Nadworny
Just the one field. That's why I started to wonder what was going on.
Paul S.Paul S.
Got it. It’s possible that it’s just a number of different versions of the same thing. Either way, I’m happy to help however I can. If you want to send along the object/field names here, that should be fine. If you want to provide sandbox access, that’s fine, too.
Rafi Pryntz-NadwornyRafi Pryntz-Nadworny
Thanks, Paul! Can you give me an email address for your sandbox account?
Paul S.Paul S.
pjsubbio@hotmail.com You’ll probably have to append something to the end of that as I’m sure I have a developer org out there somewhere with that as the username.
Paul S.Paul S.
Rafi - I didn't review the existing code to try to figure out where the too many SOQL queries error was coming from (though they usually occur because of recursive code or queries residing in a for loop).  I do believe, however, that I have an idea that might get that field updated provided that your Jorsek Org records don't have an excessive number of related Jorg Account records which don't in turn also have an excessive number of related Contact records.
Rafi Pryntz-NadwornyRafi Pryntz-Nadworny
Hmm. We have jorgs with close to a thousand jorg accounts. Each of those only has one contact. There are a few contacts with multiple jorg accounts, but out of those, they have around 30-40 jorg accounts each. Is that too much?

When I googled the SOQL error, the FOR loop came up. Our freelancer said he fixed it, but in the process the contacts with multiple jorgs no longer update. I forgot to provide the list of 5 triggers he created before. It does look like a lot of versions of the same code.
  •  JorgAccountTrigger
  •  JorgActivityTrigger
  •  JorgTrigger 
  •  ServerTrigger 
  •  ServerupdateTrigger
Paul S.Paul S.
Ah, I see.  He's just chaining after update triggers together in order to get from a server update to a contact record.  I also think I see why you've lost the multi-jorg account functionality...

"We have jorgs with close to a thousand jorg accounts."

This line of code, however, assumes a single jorg account for each jorg.  This is in the JorgTriggerHelper class.
List<Jorg_Account__c> JAList=[select id,name,Server_Update_Name__c,Jorsek_Organization__c from Jorg_Account__c where Jorsek_Organization__c in:Idset];
       
for(Jorg_Account__c j: JAList){       
    JAMap.put(j.Jorsek_Organization__c, j);
}
Each time the for loop encounters a new Jorg Account record where the Jorg Organization is the same as one that already exists in the JAMap, it simply overwrites the existing record with the new one.  This is because map keys must be unique.

What should be happening is to create a map that is Jorsek Organization => List<Jorg Accounts>, then loop through that in order to set the last server update field.  Once that occurs, you should see updates on all the relevant Jorg Account records which should then trigger updates on the appropriate contact records.
This was selected as the best answer
Rafi Pryntz-NadwornyRafi Pryntz-Nadworny
Thanks a bunch Paul! I'll pass this over to the freelancer and see if he can resolve the issue.

Happy new year!
Paul S.Paul S.
Sure thing. If there’s anything else I can help with, let me know. Happy New Year to you, too.