You need to sign in to do that
Don't have an account?

Help with bad consultant code? - Disable trigger or fix for bulk updates
My organization hired a consultant to do some customizations. One of the things they did was to write a trigger that runs after a gift is created or updated and to update fields on the associated contact's record. But, it is failing when I try to update more than 10ish records at a time, either via the data loader or via bulk update from lists.
Can someone either:
1 - Look at this code and tell me how I can update it so it can handle bulk updates, or
2 - Tell me how to temporarily disable this trigger so I can update the records?
Any help would be grately appreciated!!!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 trigger updateGiftInformation on Opportunity (after update, after insert) {
for(Opportunity o : Trigger.new){
Decimal accountLargestAmount = 0.0;
Decimal contactLargestAmount = 0.0;
Integer accountCount = 0;
Integer contactCount = 0;
Account accountToUpdate = [select Id, Largest_Gift_Date__c, Last_Gift_Given__c, Largest_Gift_Given__c
from Account
where Id = :o.AccountId];
Contact contactToUpdate = [select Id, Largest_Gift_Date__c, Largest_Gift_Given__c, Last_Gift_Date__c,
Last_Gift_Given__c
from Contact
where Id = :o.Contact__c];
Opportunity[] contactOpps = [select CloseDate, Amount
from Opportunity
where Contact__c = :o.Contact__c and
StageName = 'Posted'
order by CloseDate desc];
if(o.AccountId != '0018000000U9G5W' || o.AccountId !=null){
Opportunity[] opps = [select CloseDate, Amount
from Opportunity
where AccountId = :o.AccountId and
StageName = 'Posted'
order by CloseDate desc];
for(Opportunity currOpp : opps){
Decimal tempCurrency = currOpp.Amount;
if(tempCurrency > accountLargestAmount){
accountToUpdate.Largest_Gift_Date__c = currOpp.CloseDate;
accountLargestAmount = tempCurrency;
}
if(accountCount == 0){
accountToUpdate.Last_Gift_Given__c = currOpp.Amount;
accountCount++;
}
}
}
update accountToUpdate;
for(Opportunity currOpp : contactOpps){
Decimal tempCurrency = currOpp.Amount;
if(tempCurrency > contactLargestAmount){
contactToUpdate.Largest_Gift_Date__c = currOpp.CloseDate;
contactToUpdate.Largest_Gift_Given__c = tempCurrency;
contactLargestAmount = tempCurrency;
}
if(contactCount == 0){
contactToUpdate.Last_Gift_Given__c = currOpp.Amount;
contactToUpdate.Last_Gift_Date__c = currOpp.CloseDate;
contactCount++;
}
}
update contactToUpdate;
}
}
i think who had created this trigger is new guy, he dont have any idea how to write a trigger. He has used Query inside for loop and Dml statement too and hard coded id as-well.
This trigger needs a lot of modification.
Can tell me what is the requirement.
You can reach me at shailu_15@hotmail.com
Thanks
Shailu
Hi Parks,
Please use this code. It will help you
Please mark it as solution if it works for you
trigger updateGiftInformation on Opportunity (after update, after insert) {
Set<Id> accId = new Set<Id>();
Set<Id> conId = new Set<Id>();
Set<Id> oppId = new Set<Id>();
for(Opportunity o : Trigger.new){
if(o.AccountId != '0018000000U9G5W' || o.AccountId !=null){
accId.add(o.AccountId);
}
if(o.Contact__c != null){
conId.add(o.Contact__c);
}
oppId.add(o.id);
}
Map<Id,Account> accountToUpdate = new Map<Id,Account>();
for(Account acc : [select Id, Largest_Gift_Date__c, Last_Gift_Given__c, Largest_Gift_Given__c
from Account where Id IN: accId]){
accountToUpdate.put(acc.Id,acc);
}
Map<Id,Contact> contactToUpdate = new Map<Id,Contact>();
for(Contact con : [select Id, Largest_Gift_Date__c, Largest_Gift_Given__c, Last_Gift_Date__c,
Last_Gift_Given__c from Contact where Id IN: conId]){
contactToUpdate.put(con.Id,con);
}
List<Opportunity> opps = new List<Opportunity>([select CloseDate, Amount from Opportunity where StageName = 'Posted' AND (AccountId IN: accId OR Contact__c IN: conId)
order by CloseDate desc]);
List<Account> updatedAccountList = new List<Account>();
List<Contact> updatedAccountList = new List<Contact>();
Decimal largestAmount = 0.0;
Integer count = 0;
for(Opportunity currOpp : opps){
Decimal tempCurrency = currOpp.Amount;
if(tempCurrency > largestAmount){
if(accountToUpdate.containsKey(currOpp.AccountId)){
accountToUpdate.get(currOpp.AccountId).Largest_Gift_Date__c = currOpp.CloseDate;
}
if(contactToUpdate.containsKey(currOpp.Contact__c)){
contactToUpdate.get(currOpp.Contact__c).Largest_Gift_Date__c = currOpp.CloseDate;
contactToUpdate.get(currOpp.Contact__c).Largest_Gift_Given__c = tempCurrency;
}
largestAmount = tempCurrency;
}
if(count == 0){
if(accountToUpdate.containsKey(currOpp.AccountId)){
accountToUpdate.get(currOpp.AccountId).Last_Gift_Given__c = currOpp.Amount;
}
if(contactToUpdate.containsKey(currOpp.Contact__c)){
contactToUpdate.get(currOpp.Contact__c).Last_Gift_Date__c = currOpp.CloseDate;
contactToUpdate.get(currOpp.Contact__c).Last_Gift_Given__c = currOpp.Amount;
}
count++;
}
}
update accountToUpdate.values();
update contactToUpdate.values();
}
Shailu and Amitd - Thanks for your responses, I appreciate the friendly help being such a novice! Amitd - on reviewing your suggestions I realized that the original trigger had a problem based on a very faulty business assumption, so we are revisiting this entirely. Would not have found this withouth your help. And Shailu, will definitely contact you if we get to a point where we are needing more advice, thanks!