0

I have the trigger below to move all associated contacts and opportunities when we change account owners through a data loader. It is catching all of the contacts but is occasionally missing some of the opportunities. We have found no similarities between the opportunities being missed. Can someone please help me refine this trigger?!

Trigger-

trigger reassignRelatedContactsAndOpportunities on Account (after update) {
    try {
        Set<Id> accountIds = new Set<Id>();
        Map<Id, String> oldOwnerIds = new Map<Id, String>();
        Map<Id, String> newOwnerIds = new Map<Id, String>();
        Contact[] contactUpdates = new Contact[0];
        Opportunity[] opportunityUpdates = new Opportunity[0];

        for (Account a : Trigger.new) {
            if (a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId) {
                oldOwnerIds.put(a.Id, Trigger.oldMap.get(a.Id).OwnerId);
                newOwnerIds.put(a.Id, a.OwnerId);
                accountIds.add(a.Id);
            }
        }

        if (!accountIds.isEmpty()) {
            for (Account act : [SELECT Id, 
                                (SELECT Id, OwnerId 
                                  FROM Contacts), 
                                (SELECT Id, OwnerId 
                                  FROM Opportunities 
                                  WHERE IsClosed = False AND 
                                        StageName != 'Closed Won – Alternate Channel' AND
                                        StageName != 'Closed Won - Consolidated Billing') 
                                FROM Account WHERE Id in :accountIds]) {
                String newOwnerId = newOwnerIds.get(act.Id);
                String oldOwnerId = oldOwnerIds.get(act.Id);
                for (Contact c : act.Contacts) {
                    if (c.OwnerId == oldOwnerId) {
                        Contact updatedContact = new Contact(Id = c.Id, OwnerId = newOwnerId);
                        contactUpdates.add(updatedContact);
                    }
                }
                for (Opportunity o : act.Opportunities) {
                    if (o.OwnerId == oldOwnerId) {
                        Opportunity updatedOpportunity = new Opportunity(Id = o.Id, OwnerId = newOwnerId);
                        opportunityUpdates.add(updatedOpportunity);
                    }
                }
            }
            update contactUpdates;
            update opportunityUpdates;
        }
    } catch(Exception e) {
        System.Debug('reassignRelatedContactsAndOpportunities failure: '+e.getMessage());
    }
}
1
  • What opportunities are being missed out there are specific criteria being looked for selecting the oppty related to the account.
    – Rao
    Jan 27 '15 at 21:55
0

In your soql to find Opportunities you are using the following criteria:

SELECT Id, OwnerId FROM Opportunities WHERE IsClosed = False AND StageName != 'Closed Won – Alternate Channel' AND StageName != 'Closed Won - Consolidated Billing'

Have you checked to see whether your opportunities being missed simply do not match the given criteria?

if you need to check which opportunities are being selected you can debug using the following line:

system.debug('Here is the opportunity updates list ' + opportunityUpdates);

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.