function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
CageMMACageMMA 

Trigger not picking up Parent id's???

Hi:

   I am doing a trigger from CaseComment where I went to send out an email to admins when the case owner and case comment owner is the same... but my trigger is not working keeps bringing up null  values for the parent..where am i going wrong

 

trigger TRG_CaseCommentEmailToCaseAssignedTo on CaseComment (after insert,after update)
{
 System.Debug('zitistrue1');
 for(CaseComment c : trigger.new)
 {
  System.Debug('z2' + c.CreatedById);
  System.Debug('z3' +  c.Parent.Ownerid);
  System.Debug('z4' +  c.Parent.RecordTypeid);

 if((c.CreatedById == c.Parent.Ownerid && c.Parent.RecordTypeid=='0123000000092K5ZZA'))
   {
   
    CLS_MailerClass.sendMail(c.CommentBody);
    }
   }
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sravusravu

Make the following changes to your code:

 

****Note: Try to change little bit the code. Use the soql queries outside for loop.

 

trigger TRG_CaseCommentEmailToCaseAssignedTo on CaseComment (after insert,after update)
{
   System.Debug('zitistrue1');
   for(CaseComment c : trigger.new)
   {
      System.Debug('z2' + c.CreatedById);
      System.Debug('z3' +  c.ParentId);
      System.Debug('z4' +  c.Parent.RecordTypeid);
       Case caseOwnerId = [select ownerId from case where Id=:c.ParentId]; //If your query is retrieving more than  one row Use the code below
       if((c.CreatedById == caseOwnerId.ownerId && c.Parent.RecordTypeid=='0123000000092K5ZZA'))
       {
         CLS_MailerClass.sendMail(c.CommentBody);
       }
   }
}

 

//Use this code if your list is retrieving more than one row

 

trigger TRG_CaseCommentEmailToCaseAssignedTo on CaseComment (after insert,after update)
{
   System.Debug('zitistrue1');
   for(CaseComment c : trigger.new)
   {
      System.Debug('z2' + c.CreatedById);
      System.Debug('z3' +  c.ParentId);
      System.Debug('z4' +  c.Parent.RecordTypeid);

      Public Id oId {get;set;}

      List<Case> caseOwnerId = [select ownerId from case where Id =:c.ParentId];

      for(Case cowner : caseOwnerId){

             oId = cowner.OwnerId;

      }

       if((c.CreatedById == oId && c.Parent.RecordTypeid=='0123000000092K5ZZA'))
       {
         CLS_MailerClass.sendMail(c.CommentBody);
       }
   }
}

 

Hope this helps you..............

Accept it as a solution if this resolves your issue

 

All Answers

BritishBoyinDCBritishBoyinDC

CaseComment may be different, but typically, triggers don't have the context for related records, including parent records.

 

I think you would need to re-query for the Case Fields from Case, put them in a map, and then retrieve them during the loop to test the logic

sravusravu

Make the following changes to your code:

 

****Note: Try to change little bit the code. Use the soql queries outside for loop.

 

trigger TRG_CaseCommentEmailToCaseAssignedTo on CaseComment (after insert,after update)
{
   System.Debug('zitistrue1');
   for(CaseComment c : trigger.new)
   {
      System.Debug('z2' + c.CreatedById);
      System.Debug('z3' +  c.ParentId);
      System.Debug('z4' +  c.Parent.RecordTypeid);
       Case caseOwnerId = [select ownerId from case where Id=:c.ParentId]; //If your query is retrieving more than  one row Use the code below
       if((c.CreatedById == caseOwnerId.ownerId && c.Parent.RecordTypeid=='0123000000092K5ZZA'))
       {
         CLS_MailerClass.sendMail(c.CommentBody);
       }
   }
}

 

//Use this code if your list is retrieving more than one row

 

trigger TRG_CaseCommentEmailToCaseAssignedTo on CaseComment (after insert,after update)
{
   System.Debug('zitistrue1');
   for(CaseComment c : trigger.new)
   {
      System.Debug('z2' + c.CreatedById);
      System.Debug('z3' +  c.ParentId);
      System.Debug('z4' +  c.Parent.RecordTypeid);

      Public Id oId {get;set;}

      List<Case> caseOwnerId = [select ownerId from case where Id =:c.ParentId];

      for(Case cowner : caseOwnerId){

             oId = cowner.OwnerId;

      }

       if((c.CreatedById == oId && c.Parent.RecordTypeid=='0123000000092K5ZZA'))
       {
         CLS_MailerClass.sendMail(c.CommentBody);
       }
   }
}

 

Hope this helps you..............

Accept it as a solution if this resolves your issue

 

This was selected as the best answer
BritishBoyinDCBritishBoyinDC

That  would still break in bulk...

 

This would work better:

 

 

trigger TRG_CaseCommentEmailToCaseAssignedTo on CaseComment (after insert,after update) {

Set<Id> ParentCaseIds = new Set<Id>();

for(CaseComment c : trigger.new) {
ParentCaseIds.add(c.ParentId);
}

Map<Id, Case> m = new Map<ID, Case>([select id, ownerId, RecordTypeId from case where Id IN :ParentCaseIds ]);

for(CaseComment c : trigger.new) {

   {
      if(c.CreatedById == m.get(c.ParentId).OwnerId && m.get(c.ParentId).RecordTypeId =='0123000000092K5ZZA') {
         CLS_MailerClass.sendMail(c.CommentBody);
       }
   }
}
}