Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 KB | None | 0 0
  1.   public PageReference welcomeEmail(){
  2.         //Get all Committee Memberships that Start this year TO INSERT
  3.         thisYear = System.Today().year();
  4.         //Incoming President Name
  5.         NU__CommitteeMembership__c president = [SELECT NU__Account2__r.NameWithCredentials__c,
  6.                                                        NU__Committee2__r.Name,
  7.                                                        NU__CommitteePosition__r.Name,
  8.                                                        NU__StartDate__c
  9.                                                 FROM NU__CommitteeMembership__c
  10.                                                 WHERE NU__CommitteePosition__r.Name = 'President'
  11.                                                 AND NU__Committee2__r.Name = 'Board of Directors'
  12.                                                 AND NU__StartDate__c = THIS_YEAR
  13.                                                ];
  14.        
  15.         List<NU__CommitteeMembership__c> committeeMemberships = Database.query(
  16.             'SELECT NU__Account2__c,NU__Committee2__r.Name,NU__CommitteePosition__r.Name,NU__EndDate__c,NU__StartDate__c ' +
  17.             'FROM NU__CommitteeMembership__c ' +
  18.             'WHERE CALENDAR_YEAR(NU__StartDate__c)= '+thisYear
  19.         );
  20.         //Declare new list for accounts
  21.         List<Id> memberId = new List<Id>();
  22.         Map<Id, AACE_CommitteeEmail__c> emails = new Map<Id, AACE_CommitteeEmail__c>();
  23.         List<AACE_CommitteeEmail__c> finalList = new List<AACE_CommitteeEmail__c>();
  24.         System.debug('welcome emails '+committeeMemberships);
  25.         //Loop through committeememberships to get individual account Ids
  26.        
  27.         for(NU__CommitteeMembership__c cm : committeeMemberships){
  28.  
  29.             if(memberId.contains(cm.NU__Account2__c)){
  30.                 //This fires when the member has more than one committee
  31.                 emails.get(cm.NU__Account2__c).Committees__C = emails.get(cm.NU__Account2__c).Committees__C + '<br /> '+cm.NU__Committee2__r.Name + ' - '+cm.NU__CommitteePosition__r.Name;
  32.                
  33.             }else{
  34.                 memberId.add(cm.NU__Account2__c);
  35.                 AACE_CommitteeEmail__c a = new AACE_CommitteeEmail__c(Account__c = cm.NU__Account2__c, EmailType__c = 'welcome', Committees__C = cm.NU__Committee2__r.Name + ' - '+cm.NU__CommitteePosition__r.Name, StartDate__c = cm.NU__StartDate__c, EndDate__c = cm.NU__EndDate__c, PresidentName__c=president.NU__Account2__r.NameWithCredentials__c );
  36.                 emails.put(cm.NU__Account2__c,a);
  37.             }
  38.         }
  39.         System.debug('EMAILS '+emails);
  40.         System.debug('president '+president);
  41.         for (Id key : emails.keySet()) {
  42.             // The "key" variable is also available inside the loop
  43.             finalList.add(emails.get(key));
  44.             // ... emailing logic
  45.         }
  46.        
  47.         //This is all of the individual Committee Members
  48.         System.debug('Right here '+finalList);
  49.        
  50.        
  51.         try {
  52.             insert finalList;  
  53.             } catch(DmlException e) {
  54.                 System.debug('An unexpected error has occurred: ' + e.getMessage());
  55.         }
  56.         return null;
  57.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement