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
Nalec7Nalec7 

Opportunity Trigger to be changed to an Account trigger

I have an existing Opportunity trigger in place which is capturing the lead source of the oldest contact in an account. We now need to have this also listed on the Account detail page. I've created a custom field called Orignal Source which mirrors the custom field built on the opportunity. So I just need some help tweaking this code to be used with the account object. Any help would be greatly appreciated.

 

trigger orginalSourceUpdateTrigger on Opportunity (before insert) {
// Original Source requirement class variable
        final Map<String, String> opportunityIDToAccountIDMap = new Map<String, String>();
        Set<String> uniqueAccountIDSet = new Set<String>();
        final Map<String, Contact> accountIDToOldestContactMap = new Map<String, Contact>();
      //  Set<string> oldestContactSet = new Set<String>();
      //  Set<String> oldestContactAssociatedWithCampaignMeber = new Set<String>();

        
        for(Opportunity eachOpportunity :Trigger.new) {  
        
        if(!opportunityIDToAccountIDMap.containsKey(eachOpportunity.id))
        {
        opportunityIDToAccountIDMap.put(eachOpportunity.id, eachOpportunity.AccountId);
        uniqueAccountIDSet.add(eachOpportunity.AccountId);
        }
        
        
        }

        for(Contact con: [Select id, LeadSource, AccountId from Contact where AccountId IN: uniqueAccountIDSet ORDER BY CreatedDate ASC])
        {
            if(!accountIDToOldestContactMap.containsKey(con.AccountId))
            {
            accountIDToOldestContactMap.put(con.AccountId, con);
          //  oldestContactSet.add(con.id);
            }
        }

        
        
        
        // Original Source Update
            try{        
                for(Opportunity opp: Trigger.new)
                {                  
                     opp.Original_Source__c = accountIDToOldestContactMap.get(opportunityIDToAccountIDMap.get(opp.id)).LeadSource;
                }
            }
            
            catch(Exception e)
            {}

}
vishal@forcevishal@force

Hi,

 

You cannot have this working on a before insert trigger on Account because at that point of time, there will be no related Contacts. So can you please confirm the business requirement here? Do you need to have the Original Source on Account on a specific event or condition?

 

Nalec7Nalec7

Thanks Vishal.  This is the code were using at the opportunity object.

 

So the business case is to have the lead source from the oldest ( or 1st created ) contact within the account. Look forward to any help or support. Let me know if you need any clarification.

vishal@forcevishal@force

Ok, I get it.

 

The existing trigger which is on Opportunity does the same thing however the point to understand here is that this trigger gets Contacts which are related to Account. But when we talk about Account, at a point when you are saving a new Account, you will not have any Contacts.

 

So the trigger should ideally be on Contact, after insert and after delete since you only want the oldest contact. And then the same trigger can update fields on Opportunity as well as Account instead of havint two different triggers.

Nalec7Nalec7

I'm not familar with this type of code. I was trying to potentially re-build it by swaping objects Account for Opportunity.

Please let me know if you think you could rebuild it for me. Cheers.

vishal@forcevishal@force

Yes, I can.