You need to sign in to do that
Don't have an account?
Rafi 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.
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.
"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. 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
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
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.
"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. 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.
Happy new year!