You need to sign in to do that
Don't have an account?
Trigger works ok in web, small batches but fails upon API action
The trigger below deletes related objects when our custom object tracking sales coverage - Coverage_person__c - is deleted.
It works fine in the web site, works ok on dataloader deletes in small bacthes of 1, or 5, but at higher numbers it fails.
Here is the code:
trigger CPChatterandATMDeleterBulked on Coverage_Person__c (before delete) { List<string> AcctIds = new List<string>(); List<string> useIds = new List<string>(); for(Coverage_Person__c p:trigger.old) { AcctIds.add(p.Account__c); useIds.add(p.User__c); } List<EntitySubscription> esSet= [select id from EntitySubscription where ParentId IN :AcctIds and subscriberid IN :useIds]; List <AccountTeamMember> atmList = [select id from AccountTeamMember where UserId IN :useIds and AccountId IN :AcctIds]; for(Coverage_Person__c p:trigger.old) { delete esSet; delete atmList; } }
here is the error when i try to delete a list of 16 records:
"CPChatterandATMDeleterBulked: execution of BeforeDelete
caused by: System.DmlException: Delete failed. First exception on row 0 with id 0E8K0000000JnNNKA0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []
Trigger.CPChatterandATMDeleterBulked: line 22, column 1"
However if I shorten the list to 5 records, that record does not produce any error.
Any thoughts?
Thanks
I'm not sure why it works with 5 and fails with 16, but I don't see why you are trying to delete the lists of related objects in a for loop.
I would think that both the lists (esSet, atmList) would get delted in the first iteration and wouldn't need to be deleted again.
Try deleting the bottom for loop and just having the two delete statements after they are declared.
All Answers
I'm not sure why it works with 5 and fails with 16, but I don't see why you are trying to delete the lists of related objects in a for loop.
I would think that both the lists (esSet, atmList) would get delted in the first iteration and wouldn't need to be deleted again.
Try deleting the bottom for loop and just having the two delete statements after they are declared.
thanks! I eliminated the second For loop and just did a test with 67 and it worked.
not sure why it was there - probably an artefact from consctrucing the trigger from another one. cheers.
No problem, I'm glad I could help!
Do you think you could help me with another trigger I am having the same issues with?
This is the trigger that creates the Account Team member and Chatter Subscription upon insertion of the new Coverage Person record. it works fine via web but now testing via API (data loader or Excel Connector) am getting various errors.
Not all inserted Coverage Persons will relate to a User record.
If I insert a lot of ones that don't find a record, it seems to work.
If I insert ONE Coverage Person for a person that has a User account it works. But if I insert a list with more than the 1 record that matches a user, I get errors.
Here is my code:
here is my error:
Insert Row Failed:
CPChatterandATMAdder: execution of AfterInsert
caused by: System.DmlException: Insert failed. First exception on row 2; first error: DUPLICATE_VALUE, duplicate value found: <unknown> duplicates value on record with id: <unknown>: []
Trigger.CPChatterandATMAdder: line 41, column 1
thanks!
To break it down to what the problem lies - the subscriptions
So the error comes from 2 or more subscriptions that have the same subscriberid ..
I'm a little confused on the code but try changing this:
if (! preSubs.contains(a.id))
to this:
if (preSubs.contains(a.id))
Again, I'm not sure if that will work, I'm just brainstorming ideas.
the
if (! preSubs.contains(a.id))
tells it if the person is not already a subscriber, then do the rest... so if i take that ! away it fires ok but it does nothing.
Okay, so try changing it to this ..
if (!preSubs.contains(a.id) && p.User__c == a.Id)
I think you're just missing a check dealing with how the coverage person relates to the user so something like that should be the solution.
yes that seems to be working, thanks much!
You're welcome.