0

Background:

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) There is a priority field on the account object. There are different campaigns namely, Red, Green, Blue which are mapped to the priority field of account. So Let's see priority 1 means blue bucket/campaign, priority 2 means orange campaign, priority 3 means red campaign 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 find out the highest priority from all those accounts associated with the specific contact, then add the contact as a campaign member in the corresponding campaign. So if the highest priority from all those accounts come out to be 2, the contact should be added in orange campaign, if the highest priority comes out to be 3, the contact should be added as campaign member in red campaign.

I was thinking to start it by creating a bulk trigger on account object and maybe 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. Then write a second trigger that checks the value of 'highest priority' field on contact object and based on that add the contact in the corresponding campaign(red,orange,blue e.t.c). Thoughts on that? Is it possible to do? Any other way to achieve this whole functionality efficiently?

Thanks

4

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 @David for providing your insights. It was really helpful. Yes, you are right. One contact is associated with multiple accounts. The reason why I need to add contact as a campaign member is because we need to send emails to the people added in the campaign. – Student Feb 9 at 18:03
  • Also, if a contact is already added to a campaign and the priority changes, then the contact should be deleted from the current campaign and added to the appropriate campaign. – Student Feb 9 at 18:03
  • @Student I think one of the most important points in this answer is that you probably don't need to work on all of your contacts (or whatever base object you end up working off of). You can likely re-arrange whatever logic you may have so that it works only on the information that is changing. Idempotency, in my experience, ends up being pretty important on the Salesforce platform. If you can write code that is both idempotent and only works on things that are changing, you'll likely end up with some pretty solid logic that can withstand things like other triggers or workflow being added. – Derek F Feb 9 at 19:10
  • @David Here is a small twist so contact is not directly associated with Accounts but it is actually associated with 'Affiliation object' and affilaition object is further directing to Account. I am trying to create a declarative rollup summary on Affiliation object, API name 'npe5__Affiliation__c' but unfortunately it says object does not exist. – Student Feb 9 at 22:45
  • @Student The Nonprofit Success Pack is very different from the standard Sales Cloud object model. It would be best to make that clear in your question. I don't have an NPSP org handy to test on at the moment. – David Reed Feb 10 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.