You need to sign in to do that
Don't have an account?
MiguelGuerreiro
Trigger on CaseComment
hello,
im trying to create a trigger to prevent users from adding comments on closed cases.
this is my code:
trigger CloseComment on CaseComment (before insert) {
for (CaseComment t: Trigger.new)
{
Case c = new Case(Id = t.ParentId);
if(c.Status == 'Closed' && c.Process__c == 'Payroll')
{
t.addError(c.Process__c + 'case closed' + c.Status);
}
else
t.addError(c.Process__c + 'case open' + c.Status);
}
}
but i can't get the values of any case fields. I guess im missing something simple.
Any ideas?
I've never run into that issue, but I don't move much data with dataloader anymore. Glad to see the trigger is running properly for you :)
-Richard
All Answers
You're trying to create a new case from the old case and insert the Id there. That won't work. You need to get the case from the parent like so:
Case c = (Case)t.Parent;
On second thought I don't think it'll provide the case object in a trigger like that. You'll probably have to use SOQL to retrieve it from the ID:
Id parentId = t.ParentId;
Case c = [Select Status,Id from Case where Id=:parentId];
-Richard
I rewrote this in a batch friendly way for you since it'll likely need to query the database for the case object:
trigger CloseComment on CaseComment (before insert) {
Map<Id,CaseComment> cMap = new Map<Id,CaseComment>();
for (CaseComment t: Trigger.new){
cMap.put(t.ParentId,t);
}
Set<Id> idSet = cMap.keySet();
for(Case c:[select Id,Status,Process__c from Case where Id in :idSet]) {
if(c.Status == 'Closed' && c.Process__c == 'Payroll'){
CaseComment cm = (CaseComment)cMap.get(c.Id);
cm.addError(c.Process__c + 'case closed' + c.Status);
}else {
CaseComment cm = (CaseComment)cMap.get(c.Id);
cm.addError(c.Process__c + 'case open' + c.Status);
}
}
}
Also an important note to you: SObject.addError prevents DML execution. Meaning this trigger would fail to allow you to ever add a case comment. You only want to use the addError message when you want to prevent the insert,upsert,update,delete action from happening. This trigger is a good example to understand how they work, but is completely disfunctional because of that. To get it to work the way you'd like it to, comment out or remove the else section of your if statement, but for development purposes it will show you the trigger working as expected.
-Richard
Thank you Richard. I will try your code. Do you think it will work for batch uploads also?
My code does get the case id but that the only thing it gets. So I guess its smarter to get a array of ids and do a select to get the case fields... but I don't have sufficient knowledge.
Case c = new Case(Id = t.ParentId);
Richard,
Your trigger is working fine when your try to add a comment usign the browser. But I'm getting this error when using the data loader:
"Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2"
Any ideas?
Did you comment out or remove the else block that throws and error when the case is open? If not this trigger errors every time and that will cause problems. I left that in for you just because I figured you were using it so see the trigger in action.
-Richard
Maybe I get that error message because I'm adding comment to closed cases? Still the error message is not consistent (check last 3 lines)... but it's working ;)
"CASE NUMBER","CASE ID","CASE COMMENTS","ERROR" "00599865","500T0000002GrvS","sdfsdfsd","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","sdfsdfsd","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","SDGSDFGDFG","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","sdfsdfsd","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","SDGSDFGDFG","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","sdfsdfsd","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","SDGSDFGDFG","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","sdfsdfsd","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","SDGSDFGDFG","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","sdfsdfsd","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","SDGSDFGDFG","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","sdfsdfsd","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","SDGSDFGDFG","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","sdfsdfsd","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","SDGSDFGDFG","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","sdfsdfsd","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","SDGSDFGDFG","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","sdfsdfsd","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","sdfsdfsd","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","SDGSDFGDFG","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","sdfsdfsd","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","SDGSDFGDFG","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","sdfsdfsd","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","SDGSDFGDFG","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","sdfsdfsd","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","SDGSDFGDFG","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","sdfsdfsd","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","SDGSDFGDFG","Too many retries of batch save in the presence of Apex triggers with failures: when triggers are present partial save requires that some subset of rows save without any errors in order to avoid inconsistent side effects from those triggers. Number of retries: 2" "00599865","500T0000002GrvS","sdfsdfsd","Payrollcase closedClosed" "00599865","500T0000002GrvS","SDGSDFGDFG","Payrollcase closedClosed" "00599865","500T0000002GrvS","sdfsdfsd","Payrollcase closedClosed"
I've never run into that issue, but I don't move much data with dataloader anymore. Glad to see the trigger is running properly for you :)
-Richard
My requirement: Whenever a ParentObject__c records get inserted create two ChildObject__c records. I am trying to create a record in Opportunity(Parent) and need to create two Child (Product) record.
I wrote trigger here is the error i am facing:
My trigger:
error:
Method does not exist or incorrect signature: void createproduct(List<Product2>) from the type OpportunityTriggerHandler
Apex class: