You need to sign in to do that
Don't have an account?
Warren Wade, AE
roll up for after delete
When a Preservation Activity (child) gets deleted, I need the Preservation Object (parent) to recalculate the Max Date.
(also, I'm not a coder so I tried to annotate what I thought was happening in the code that's why those notes are there.)
(also, I'm not a coder so I tried to annotate what I thought was happening in the code that's why those notes are there.)
trigger RollUpRecentActivity on Preservation_Activities__c (after insert, after update, after delete) { set<Id>setPreserveId = new set<Id>(); //set new variable "setPreserveID" list<Preservation_Object__c>lstPreserve = new list<Preservation_Object__c>(); //set a new Preservation Object list called "lstPreserve" if(trigger.isAfter){ //if it's afterUpdate system.debug('@Developer --> isAfter:'); //debug statement if(trigger.isInsert || trigger.isUpdate){ //if it's on insert OR update for(Preservation_Activities__c oActivities:trigger.new){ //set oActivities which equals the activity/ies that are trigger.new setPreserveId.add(oActivities.Preservation_Object__c);//to the setPreserveId variable, add the PReservation objects of the activities in the trigger } } system.debug('@developer-->setPreserveId: '+setPreserveId); //debug statement if(setPreserveId.size()>0){ //if there's actually an id for(Preservation_Activities__c oActivities:[ Select Preservation_Object__c,id,Name,Activity_Type__c,Activity_Date__c from Preservation_Activities__c where Preservation_Object__c In:setPreserveId and Activity_Date__c != null and isDeleted = FALSE order by Activity_Date__c desc limit 1]){ Preservation_Object__c oPreserve = new Preservation_Object__c(); oPreserve.Id = oActivities.Preservation_Object__c; oPreserve.Last_Activity_Type__c = oActivities.Activity_Type__c; lstPreserve.add(oPreserve); } system.debug('@developer-->lstPreserve: '+lstPreserve); if(lstPreserve.size()>0){ update lstPreserve; } } } }
Actually you are not adding context veriable trigger.IsDelete so when it will be delete not filled trigger query.So add this.---
trigger RollUpRecentActivity on Preservation_Activities__c (after insert, after update, after delete) {
set<Id>setPreserveId = new set<Id>(); //set new variable "setPreserveID"
list<Preservation_Object__c>lstPreserve = new list<Preservation_Object__c>(); //set a new Preservation Object list called "lstPreserve"
if(trigger.isAfter){ //if it's afterUpdate
system.debug('@Developer --> isAfter:'); //debug statement
if(trigger.isInsert || trigger.isUpdate || trigger.isdelete){ //if it's on insert OR update
for(Preservation_Activities__c oActivities:trigger.new){ //set oActivities which equals the activity/ies that are trigger.new
setPreserveId.add(oActivities.Preservation_Object__c);//to the setPreserveId variable, add the PReservation objects of the activities in the trigger
}
}
system.debug('@developer-->setPreserveId: '+setPreserveId); //debug statement
if(setPreserveId.size()>0){ //if there's actually an id
for(Preservation_Activities__c oActivities:[
Select Preservation_Object__c,id,Name,Activity_Type__c,Activity_Date__c
from Preservation_Activities__c
where Preservation_Object__c In:setPreserveId and Activity_Date__c != null and isDeleted = FALSE
order by Activity_Date__c desc limit 1]){
Preservation_Object__c oPreserve = new Preservation_Object__c();
oPreserve.Id = oActivities.Preservation_Object__c;
oPreserve.Last_Activity_Type__c = oActivities.Activity_Type__c;
lstPreserve.add(oPreserve);
}
system.debug('@developer-->lstPreserve: '+lstPreserve);
if(lstPreserve.size()>0){
update lstPreserve;
}
}
}
}
All Answers
Actually you are not adding context veriable trigger.IsDelete so when it will be delete not filled trigger query.So add this.---
trigger RollUpRecentActivity on Preservation_Activities__c (after insert, after update, after delete) {
set<Id>setPreserveId = new set<Id>(); //set new variable "setPreserveID"
list<Preservation_Object__c>lstPreserve = new list<Preservation_Object__c>(); //set a new Preservation Object list called "lstPreserve"
if(trigger.isAfter){ //if it's afterUpdate
system.debug('@Developer --> isAfter:'); //debug statement
if(trigger.isInsert || trigger.isUpdate || trigger.isdelete){ //if it's on insert OR update
for(Preservation_Activities__c oActivities:trigger.new){ //set oActivities which equals the activity/ies that are trigger.new
setPreserveId.add(oActivities.Preservation_Object__c);//to the setPreserveId variable, add the PReservation objects of the activities in the trigger
}
}
system.debug('@developer-->setPreserveId: '+setPreserveId); //debug statement
if(setPreserveId.size()>0){ //if there's actually an id
for(Preservation_Activities__c oActivities:[
Select Preservation_Object__c,id,Name,Activity_Type__c,Activity_Date__c
from Preservation_Activities__c
where Preservation_Object__c In:setPreserveId and Activity_Date__c != null and isDeleted = FALSE
order by Activity_Date__c desc limit 1]){
Preservation_Object__c oPreserve = new Preservation_Object__c();
oPreserve.Id = oActivities.Preservation_Object__c;
oPreserve.Last_Activity_Type__c = oActivities.Activity_Type__c;
lstPreserve.add(oPreserve);
}
system.debug('@developer-->lstPreserve: '+lstPreserve);
if(lstPreserve.size()>0){
update lstPreserve;
}
}
}
}
Thanks for catching that. That makes sense as to why it wasn't firing; however, now I'm getting an "Attempt to de-reference a null object" error at line 5. Again, thanks for your help on this. I'm an admin trying to resolve some code.