You need to sign in to do that
Don't have an account?
Too many nested for statements and queries.
I am trying to get this trigger to be as efficient as possible. I can get it to work enough but to get the code coverage I change it slightyly in a way that causes too many SOQL queries. Its pretty obvious why as I have three for statemenets nested with SOQL queries in them. Up till now thats the only way I know how to do it. I think I am supposed to move those queires to Maps but I don't know how to conceptually work with maps.
WHAT IT DOES:
When some fields are filled in on the opp it takes those values and insertes them into a new record related to the contact record listed as primary contact on the opp.
HOW I SEE THE PROCESS
1. Trigger an opp cause the Credit Card fields are filled in
2. find the contact ID listed as priamry contact on the Opportunity Contact Roles
3. Create a new payment method record, fill values from the opp and set the ContactID to whoever was the primary contact Role on the Opp.
I think I need to approach my code totally differently.
Something like this should work by using the OpportunityContactRoles aggregate relationship, and the referencing the parent query.
All Answers
Something like this should work by using the OpportunityContactRoles aggregate relationship, and the referencing the parent query.
Awesome, I am so close. When I try the section to see if there are any OppContactRoles I still get the SOQL Limit Which is odd cause there is only the one inside the first For Loop. If I change the search criteria from == 0 to <1 or == NULL I no longer get the error and works when there is a contact role, but if there is not contact role it doesn't seem to know that and doesn't do anything. No error but no work done either. Here is the code as I have it that provides the SOQL Limit error.
Again, if I change the line that says, "IF(CardInfo.OpportunityContactRoles.size() < 1 ){" or "IF(CardInfo.OpportunityContactRoles.size()==NULL){" the test code passes with 90% coverage. If I run with no opp contact role then no payment method will get created, the data on the opportunity doesn't get erased, no message is published on the text field on the opp and no error message. If there is an opp contact role everything runs perfect.
Turns out what the test class is running agains are SOQL queries from other triggers I have that use nested For statements and queries. I will have to go into those and try and fix them to reduce the dependence on nested queries. Thank you.!
Thanks for you help. It is so close to working. Everything works except if I am missing a contact role. The part where it should check if there are any contact roles that are 'Primary' doesn't seem to work. When I have contact roles they work fine. If they don't we are back to nothing happeing. No errors, no message being displayed in the text box, nothing.