Skip to main content

Hello All! I have been able to complete basic triggers with the training I have completed so far, but i am attempting to create a new trigger and running into some trouble. The result expected from the code is that when a child record(Customer Account) is deleted, a Case Comment will be inserted. When it comes to completing the test It does not pass. I beleive there is an issue with using trigger.old to capture the deleted record data. I am only needing the the Case ID and another custom field from the deleted record. Below is the code that I wrote.  I am hoping a can get a little guidance or suggestion on where I need to make revisions or if I need to move this into a trigger helper class. And suggestions/ Ideas are greatly appreciated!

trigger TriggerCustomerAccount on CustomerAccount__c (after delete) {

If(Trigger.isDelete && Trigger.isAfter){

for( CustomerAccount__c custAcct : trigger.old) {

id caseid = custAcct.Id;

CaseComment newComment;

newComment.ParentId = caseid;

newComment.CommentBody = custAcct.Clientbrand_Name__c + ' Customer Account was deleted on ' + system.now() + ' per flagged for removal';

insert newComment;

}

}

}

6 answers
  1. Jul 6, 2017, 7:41 PM
    Hello Joseph,

    Use the following trigger and it will probably resolve the issue. I have tested it and it is working fine. Let's see if it works in our case here ;)

     

    Trigger TriggerCustomerAccount On Customer_Account__c(After Delete){

    List<Id> caseIds = New List<Id>();

    If(Trigger.IsDelete){

    For(Customer_Account__c custAcct : Trigger.Old){

    If(custAcct.Case__c != null){

    caseIds.add(custAcct.Case__c);

    }

    }

    }

    List<CaseComment> finalCaseCommentListToInsert = New List<CaseComment>();

    For(Case cs : [Select Id, Clientbrand_Name__c FROM Case WHERE Id =: caseIds]){

    CaseComment newComment = new CaseComment();

    newComment.ParentId = cs.Id;

    newComment.CommentBody = custAcct.Clientbrand_Name__c + ' Customer Account was deleted on ' + system.now() + ' per flagged for removal';

    finalCaseCommentListToInsert.add(newComment);

    }

    try{

    If(!finalCaseCommentListToInsert.IsEmpty()){

    insert finalCaseCommentListToInsert;

    }

    }

    Catch(Exception e){

    system.debug('Thrown Exception for TriggerCustomerAccount Is: ' + e.getMessage());

    }

    }

    Hope this helps!

     
0/9000