+ Start a Discussion
MiguelGuerreiroMiguelGuerreiro 

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?

Message Edited by MiguelGuerreiro on 01-14-2010 09:20 AM
Best Answer chosen by Admin (Salesforce Developers) 
rtuttlertuttle

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

rtuttlertuttle

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 

 

Message Edited by rtuttle on 01-13-2010 05:17 PM
rtuttlertuttle

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 

Message Edited by rtuttle on 01-13-2010 05:29 PM
Message Edited by rtuttle on 01-13-2010 05:31 PM
MiguelGuerreiroMiguelGuerreiro

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);

 

 Will post results later. 

 

MiguelGuerreiroMiguelGuerreiro

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? 

 

rtuttlertuttle

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 

MiguelGuerreiroMiguelGuerreiro
I removed the else block (I even tried removing the else itself) but still get the error message. 
MiguelGuerreiroMiguelGuerreiro

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"

 

Message Edited by MiguelGuerreiro on 01-14-2010 08:02 AM
rtuttlertuttle

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 

This was selected as the best answer
MiguelGuerreiroMiguelGuerreiro
thanks for the help!
rtuttlertuttle
no problem
marioluisscmarioluissc
Just to contribute:

Below is the code that I developed:

 /***
Criada por: Mario Luis Scarpari Citadin
Setor:      Suporte Interno
***/

//Trigger criada para impedir que um Caso Fechado seja comentado.
trigger novoComentarioInsert on CaseComment (before insert, after insert) {

  private CaseComment[] comentario = Trigger.new;
  private Case[] caso = null;
  private SelfServiceUser[] usuario_portal = null;
  private User[] usuario= null;
    private CaseComment[] numero_comentario = null;
    
  //Seleciona o Caso onde o comentário será inserido
  caso = [select id, Status from Case where id= :comentario[0].ParentId];

  
  
  
  if (Trigger.isBefore){
    //Se o caso estiver com o Status Fechado o comentário não é inserido.
    if (caso[0].Status.equals('Fechado')){
      comentario[0].addError('Não é possível comentar um caso Fechado, se existir uma nova dúvida ou questionamento registre um novo Caso.');
    }

      }
  if (Trigger.isAfter){
     //Verifica se o usuário é um usuário do Portal de Auto-Atendimento
     usuario_portal = [select username from SelfServiceUser where id = :comentario[0].CreatedById];
     usuario = [select id from User where id = :comentario[0].CreatedById and profileid = '00e800000017x4M'];
    //Caso for usuário do Portal e o Status do caso for "Pendente com o cliente" o Status do caso muda para "Pendente com Atendimento Web"    
     if (((usuario_portal.size()>0 )||(usuario.size()>0)) && (caso[0].Status.equals('Pendente com o cliente'))){
       caso[0].Status = 'Pendente com Atendimento Web';
      
     }
     numero_comentario = [select id, ParentId from CaseComment where parentid = :comentario[0].ParentId];
     if ( numero_comentario.size() == 1){
           caso[0].Data_do_Primeiro_Tramite__c = comentario[0].CreatedDate;  
         
      }
      caso[0].Quantidade_de_Comentario__c = numero_comentario.size();
      update caso;
  }

}
 marioluiscitadin@gmail.com/marioluis_sc@hotmail.com  
Sudharsan CSudharsan C
Hi,
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:
User-added image
error:
Method does not exist or incorrect signature: void createproduct(List<Product2>) from the type OpportunityTriggerHandler

Apex class:
User-added image