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
Allen ManamelAllen Manamel 

Multiple Nested Loops in Apex

Hi,

 Background:

I have 3 objects: Contacts, Accounts, Campaign, Campaign Member that are involved in the scenario
Contact is the parent object, Accounts and Campaigns are child objects to contact(Related Lists)
There is a priority field on the account object.
There are many buckets on the campaign object, Red, Green, Blue which are mapped to the priority field of account.  So Let's see priority 1 means blue bucket, priority 2 means orange bucket, priority 3 means red bucket and so on.
 
What I want to do:

I want my trigger/batch job to loop over all contacts in Salesforce, for each contact, it finds the accounts associated with the contact(related list), checks the priority field on those accounts. The code should note down the highest priority from all those accounts associated with the specific contact, then add the contact as a campaign member in the corresponding campaign bucket. SO if the highest priority from all those accoounts come out to be 2, the contact should be added in orange campaign. 


Any idea how to achieve this? If somebody could provide a sample code to achieve this, it would be very helpful.

Thanks
NagendraNagendra (Salesforce Developers) 
Hi Allen,


I have 3 objects: Contacts, Accounts, Campaign, that are involved in the scenario. Contact is the parent object, Accounts and Campaigns are child objects to contact(Related Lists).

This is incorrect. Account is the parent object of Contact. Contact and Campaign are joined by a junction object, CampaignMember.

I was thinking to [...] create a hidden 'Highest Priority' field on contact object. Every time account is added,updated or deleted, it should calculate the highest priority from all the accounts associated with the parent contact and save it in the hidden field on the contact object

If your Contacts are related to multiple Accounts, you should consider using Declarative Lookup Rollup Summaries to summarize this information instead of writing your own trigger. If you are not using one of the Salesforce features that relates Contacts to multiple Accounts, this is just a formula field.

Edit: Per your comments, you are using the Nonprofit Success Pack's Affiliations object, which acts as a junction object between Contact and Account.

from there check the value of highest priority on the contact object and then add the contact in the corresponding campaign.

Here's a broader question: Why do you need the Contacts to be in these generic Campaigns - what are you using them for? To me, this sounds more like a job for a Report or a List View, both of which are code-free, unless you are planning to use the Campaigns to organize bulk email or some similar process and the use of the Campaign is dictated by your marketing software.

If you really do need the Campaign memberships, this would be the point to write your trigger to react to changes in the summarized field on the Contact and perform updates to the set of Campaign Members for that Contact.
You may in fact need to write nested for loops in that trigger - an outer loop over Contacts and an inner loop over their associated Campaign Members - but I wouldn't worry about that. Write your queries with appropriate filters and ensure that you do not query or perform DML inside a loop, and you'll be fine.

Thanks,
Nagendra