You need to sign in to do that
Don't have an account?
uptime_andrew
Trigger After Delete - Getting Error
I have a custom object that I am trying to execute a trigger on after insert, update, and delete. The trigger functions as expected for the insert and update cases, but I'm getting an error when trying to execute it after delete (I tried changing to before delete as well, and got the same error).
The trigger is below. I get the error at line "for (MyObject thisObject : System.Trigger.new) {", so I imagine my syntax must be incorrect here for the "delete" case. However, I've had difficulty finding an example to refer to in the Force.com Cookbook or by searching Salesforce.com. Any insight would be appreciated.
Here is my trigger:
trigger MyTrigger on MyObject (after insert, after update, after delete) {
Map<String, MyObject> soMap = new Map<String, MyObject>();
for (MyObject thisObject : System.Trigger.new) {
Decimal Total = 0;
Long Count = 0;
// First, check for records meeting criteria A
for (MyObject obj : [ SELECT Amount from MyObject where Account__c = :thisObject.Account__c and fieldA = 'A']) {
// sum the amounts
Total += obj.Amount;
Count++;
}
// if no records exist, look for criteria B
if (Count == 0) {
for (MyObject obj : [ SELECT Amount from MyObject where fieldA = 'B' and CreatedDate = LAST_N_DAYS:365 AND Account__c = :thisObject.Account__c ]) {
// total the amounts
Total += obj.Amount;
}
// get 50% of the total
Total = 0.5 * Total;
}
// update value
for (Account a : [select id from Account where id = :thisObject.Account__c]) {
a.customField = Total;
update a;
}
}
}
The trigger is below. I get the error at line "for (MyObject thisObject : System.Trigger.new) {", so I imagine my syntax must be incorrect here for the "delete" case. However, I've had difficulty finding an example to refer to in the Force.com Cookbook or by searching Salesforce.com. Any insight would be appreciated.
Here is my trigger:
trigger MyTrigger on MyObject (after insert, after update, after delete) {
Map<String, MyObject> soMap = new Map<String, MyObject>();
for (MyObject thisObject : System.Trigger.new) {
Decimal Total = 0;
Long Count = 0;
// First, check for records meeting criteria A
for (MyObject obj : [ SELECT Amount from MyObject where Account__c = :thisObject.Account__c and fieldA = 'A']) {
// sum the amounts
Total += obj.Amount;
Count++;
}
// if no records exist, look for criteria B
if (Count == 0) {
for (MyObject obj : [ SELECT Amount from MyObject where fieldA = 'B' and CreatedDate = LAST_N_DAYS:365 AND Account__c = :thisObject.Account__c ]) {
// total the amounts
Total += obj.Amount;
}
// get 50% of the total
Total = 0.5 * Total;
}
// update value
for (Account a : [select id from Account where id = :thisObject.Account__c]) {
a.customField = Total;
update a;
}
}
}
All Answers
Trigger.New is now available in Trigger for event after Delete
Thanks for that.