• erik_eklund
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 4
    Replies

I'm fairly new to Apex (and I'm not a developer by trade) and I'm stumped on a SOQL query for a trigger that I'm writing.

Background info: I'm attempting to set up a process by which we can add Contacts to a custom object called Notification Groups. These Notification Groups can then be associated with Cases, and when a Comment is left on a Case and marked as Public, I want to email the Contacts within the associated Notification Groups. In order to make the necessary many-to-many relationships possible (Contacts can be a part of more than one group, groups can be associated with more than one case), I've created two junction objects: ContactGroupJoiner__c and GroupCaseJoiner__c. They both have Master-Detail relationships with the objects they're joining. Here is a quick graphic to show the relationship:

 

The trigger fires on CaseCommentI'm able to get the Notification Groups that are associated with the case from the following SOQL query:

List<GroupCaseJoiner__c> notificationGroups = [SELECT Notification_Group__r.Id FROM GroupCaseJoiner__c WHERE Case__c = :caseId];

I'm stuck on the next step, though, which would be to look at each item in the notificationGroups list, grab its related Contacts' email addresses, and add them to a list called toSendEmails (declared earlier in the Class). Here is what I tried:

//Create new list that will hold ContactGroupJoiner records associated with the records in notificationGroups.
      List<ContactGroupJoiner__c> contactsInGroups = new List <ContactGroupJoiner__c>();
      
      //For each Group in the notificationGroups list, grab the associated Contact records and store them in contactsInGroups.
      for(Integer i=0; i<notificationGroups.size(); i++)
      {
        contactsInGroups.add( [SELECT Id, Notification_Group__c, Contact__c FROM ContactGroupJoiner__c WHERE Notification_Group__c = :notificationGroups[i].Id] );
      }
           
      //Create placeholder list for Contacts to Mail
      List<Contact> contactsToMail = new List <Contact>();
      
      //For each contact associated with a group, grab that contact's ID and email address.
      for(Integer i=0; i<contactsInGroups.size(); i++)
      {        
        contactsToMail.add( [SELECT Id, Email FROM Contact WHERE Id = :contactsInGroups[i].Id] );
      }
      system.debug('contactsToMail Size = ' + contactsToMail.size());
      
      //For each contact's email address, 
      for(Integer i=0; i<contactsToMail.size(); i++)
      {
        toSendEmails.add(contactsToMail[i].Email);
      }

Some of that seems inefficient to me, and probably should be combined. But anyway, the result of this code, when I test it on a case in my sandbox, is that the public comment will be sent to the Contact on the Case (handled earlier in the class), as well as only one contact (of two) associated to one Notification Group (of two) associated with the Case.

 

I think I've included all the necessary details, but if I haven't please let me know what else would help. Thanks in advance to anyone that reads this far! :)

I'm fairly new to Apex (and I'm not a developer by trade) and I'm stumped on a SOQL query for a trigger that I'm writing.

Background info: I'm attempting to set up a process by which we can add Contacts to a custom object called Notification Groups. These Notification Groups can then be associated with Cases, and when a Comment is left on a Case and marked as Public, I want to email the Contacts within the associated Notification Groups. In order to make the necessary many-to-many relationships possible (Contacts can be a part of more than one group, groups can be associated with more than one case), I've created two junction objects: ContactGroupJoiner__c and GroupCaseJoiner__c. They both have Master-Detail relationships with the objects they're joining. Here is a quick graphic to show the relationship:

 

The trigger fires on CaseCommentI'm able to get the Notification Groups that are associated with the case from the following SOQL query:

List<GroupCaseJoiner__c> notificationGroups = [SELECT Notification_Group__r.Id FROM GroupCaseJoiner__c WHERE Case__c = :caseId];

I'm stuck on the next step, though, which would be to look at each item in the notificationGroups list, grab its related Contacts' email addresses, and add them to a list called toSendEmails (declared earlier in the Class). Here is what I tried:

//Create new list that will hold ContactGroupJoiner records associated with the records in notificationGroups.
      List<ContactGroupJoiner__c> contactsInGroups = new List <ContactGroupJoiner__c>();
      
      //For each Group in the notificationGroups list, grab the associated Contact records and store them in contactsInGroups.
      for(Integer i=0; i<notificationGroups.size(); i++)
      {
        contactsInGroups.add( [SELECT Id, Notification_Group__c, Contact__c FROM ContactGroupJoiner__c WHERE Notification_Group__c = :notificationGroups[i].Id] );
      }
           
      //Create placeholder list for Contacts to Mail
      List<Contact> contactsToMail = new List <Contact>();
      
      //For each contact associated with a group, grab that contact's ID and email address.
      for(Integer i=0; i<contactsInGroups.size(); i++)
      {        
        contactsToMail.add( [SELECT Id, Email FROM Contact WHERE Id = :contactsInGroups[i].Id] );
      }
      system.debug('contactsToMail Size = ' + contactsToMail.size());
      
      //For each contact's email address, 
      for(Integer i=0; i<contactsToMail.size(); i++)
      {
        toSendEmails.add(contactsToMail[i].Email);
      }

Some of that seems inefficient to me, and probably should be combined. But anyway, the result of this code, when I test it on a case in my sandbox, is that the public comment will be sent to the Contact on the Case (handled earlier in the class), as well as only one contact (of two) associated to one Notification Group (of two) associated with the Case.

 

I think I've included all the necessary details, but if I haven't please let me know what else would help. Thanks in advance to anyone that reads this far! :)