-1

I need some advice in Trigger. Below is my requirement

We have Account, Contact, Opportuntiy objects. Account field is a lookup field(Account) on both contact and opportunity. There is no direct relationship between Contact and opportunity. Basically account is a junction object here. When Account field on contact is updated, Account field on Opportunity should be updated with the same value on contact.

Can anyone guide me how can we achieve this?

3
  • Could you please add a bit more detail here? This "junction object" is not giving 1:1 relationships. You can have arbitrary numbers of Contacts against the Account, and arbitrary numbers of Opportunities against the Account. How will you know which Opportunities should be updated and when?
    – Phil W
    Commented Oct 7, 2020 at 10:08
  • @PhilW Thanks for your response. That is what my question there is no relationship between contact and opportunity. Is there any possibility to achieve this?
    – Santhiya
    Commented Oct 7, 2020 at 10:12
  • 2
    The Opportunity object has a Contact field to allow you to directly link the Opportunity to a Contact. You can then use this to allow you to query the opportunities that refer to the contact and update them with a new Account when the Contact's Account changes.
    – Phil W
    Commented Oct 7, 2020 at 10:26

1 Answer 1

2

This is a non-answer answer, because I wanted to elaborate on a response to this requirement.

What you have described is not a best-practice way to use Salesforce and misses some key elements of the Sales Cloud data model.

There is no direct relationship between Contact and opportunity

As Phil pointed out, this is not true anymore - there is a direct relationship between Contact and Opportunity. More importantly, there's also the long-extant Opportunity Contact Role junction object between Contact and Opportunity. The purpose of that object is tracking relationships between Contacts and Opportunities.

Basically account is a junction object here.

Account is not a junction object, either literally or by analogy, in this scenario. Junction objects have two master-detail parents; Account is the parent object of both Contact and Opportunity.

When Account field on contact is updated, Account field on Opportunity should be updated with the same value on contact.

This is the crux of the matter and it really doesn't make sense to me. Account has a one-to-many relationship with both Contact and Opportunity. For Opportunities to "follow" Contacts as they are reparented between Accounts suggests to me either (a) the business has not thought this through or (b) they are using the wrong Account model.

If this is a B2C or nonprofit use case, where Opportunities are really linked to Contacts and Accounts are largely irrelevant, the organization should be using the NPSP Household Model or Person Accounts or something similar, not trying to move Opportunities around after Contacts.

How would this scenario make sense, for example, if you have Account A with Contacts B and C, and Opportunity D, and Contact B moves to Account Q? Why does Contact C then lose their association with Opportunity D?


All that said, if you must implement this trigger as given, it's not hard. You'll have to write an after update trigger on Contact, use Trigger.oldMap to find Contacts that have been reparented, and then write code to reparent the Opportunities related to their old Accounts.

But I strongly recommend you don't.

Not the answer you're looking for? Browse other questions tagged .