You need to sign in to do that
Don't have an account?
RDN_LHR
Message Edited by RDN_LHR on 12-09-2008 02:37 AM
Message Edited by RDN_LHR on 12-09-2008 02:40 AM
Message Edited by RDN_LHR on 12-09-2008 02:44 AM
Message Edited by RDN_LHR on 12-10-2008 09:12 AM
Why does this trigger only fire *SOMETIMES* in Production????
I've written the trigger that I'm including below.
The trigger looks for when an account owner has been changed, and then calls up the related contacts and assigns the new account owner as the owner of all contacts.
trigger accountUpdateContactOwner on Account (after update) { // first Id is Account.Id, second Id is Account.OwnerId Map<Id, Id> AcctOwners = new Map<Id, Id>();
// loop through and for each account, put its Id and its OwnerId in the map for (Integer i = 0; i < Trigger.new.size(); i++) { if (Trigger.old[i].OwnerId != Trigger.new[i].OwnerId ) { AcctOwners.put(Trigger.new[i].Id,Trigger.new[i].OwnerId); } } // for each account in the map, find the contacts and loop through them to
// reassign the OwnerId
for (Contact updContact : [select Id, AccountId, OwnerId from Contact where AccountId in :AcctOwners.keyset() limit 999 ]) { updContact.OwnerId = AcctOwners.get(updContact.AccountId); } }
The problem is that I tested it to death before deploying it to production. If the trigger just didn't work at all, I woudln't think it strange. The fact that it only works for certain accounts and not for others, and only in production, is really baffling.
Sadly, I asked Salesforce.com for support, and while they did look at and test my code, they refused to support me and told me I'd have to use discussion boards instead.
The process: Wrote it in development org, wrote unit test, got 100% coverage. Began tests. Loaded thousands of test accounts and contacts. Assigned 2000 contacts to an account, used the UI to make changes, trigger ran, all changes made beautifully. Did the same, but assigned one contact to 2000 accounts, changed owner through the UI (250 at a time), they all worked. Repeated the tests through the Data Loader. Got errors due to limits. Reworked the code to its present form above, re-ran lots of Data Loader tests mirroring what I'd done through the UI and they all worked wonderfully.
I deployed the code to Production and it seemed to work there, but after watching it for a couple days using reports, I found that the trigger was firing only sometimes.
I put a ticket in with Salesforce.com support so they can explain why triggers only fire sometimes. I thought either they work or they don't work, right?? I gave them two account examples to try. The first example had 10 contacts and the trigger works. The second example had one contact which didn't work. Then I found about 1000 other examples, most but not all were modified through the data loader. I thought it was just for large accounts with lots of contacts maybe hitting a limit or something, but most of these accounts had one or two contacts.
How do things work so nicely in development, but not work in production?
Message Edited by RDN_LHR on 12-09-2008 02:37 AM
Message Edited by RDN_LHR on 12-09-2008 02:40 AM
Message Edited by RDN_LHR on 12-09-2008 02:44 AM
Message Edited by RDN_LHR on 12-10-2008 09:12 AM
Message Edited by RDN_LHR on 12-10-2008 02:14 AM
If you have an account where you see some expected behavior, then its because something else is doing it, this trigger code is not. Do you have a system log from a test where you see the expected behavior? what do you unit tests look like?
This screen allows you to transfer an account from one user to another. When you transfer ownership of an account, the new owner will also gain ownership of the following records related to the transferred account:
all notes and open activities for this account owned by you
all contacts within the account owned by you, including all related notes and open activities owned by you
all opportunities (including closed opportunities if you select the Transfer closed opportunities checkbox below) within the account owned by you, including all related notes and open activities owned by you
So when you were testing, what you thought was behavior coming from your trigger is actually the built in behavior of the ownership change UI page.