Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 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 = 'AACE President'
  11.                                                 AND NU__Committee2__r.Name = 'AACE 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.  
  28.         for(NU__CommitteeMembership__c cm : committeeMemberships){
  29.  
  30.             if(memberId.contains(cm.NU__Account2__c)){
  31.                 //This fires when the member has more than one committee
  32.                 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;
  33.                
  34.             }else{
  35.                 memberId.add(cm.NU__Account2__c);
  36.                 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 );
  37.                 emails.put(cm.NU__Account2__c,a);
  38.             }
  39.         }
  40.         System.debug('EMAILS '+emails);
  41.         System.debug('president '+president);
  42.         for (Id key : emails.keySet()) {
  43.             // The "key" variable is also available inside the loop
  44.             finalList.add(emails.get(key));
  45.             // ... emailing logic
  46.         }
  47.        
  48.         //This is all of the individual Committee Members
  49.         System.debug('Right here '+finalList);
  50.        
  51.        
  52.         try {
  53.             insert finalList;  
  54.             } catch(DmlException e) {
  55.                 System.debug('An unexpected error has occurred: ' + e.getMessage());
  56.         }
  57.         return null;
  58.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement