Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public PageReference welcomeEmail(){
- //Get all Committee Memberships that Start this year TO INSERT
- thisYear = System.Today().year();
- //Incoming President Name
- NU__CommitteeMembership__c president = [SELECT NU__Account2__r.NameWithCredentials__c,
- NU__Committee2__r.Name,
- NU__CommitteePosition__r.Name,
- NU__StartDate__c
- FROM NU__CommitteeMembership__c
- WHERE NU__CommitteePosition__r.Name = 'AACE President'
- AND NU__Committee2__r.Name = 'AACE Board of Directors'
- AND NU__StartDate__c = THIS_YEAR
- ];
- List<NU__CommitteeMembership__c> committeeMemberships = Database.query(
- 'SELECT NU__Account2__c,NU__Committee2__r.Name,NU__CommitteePosition__r.Name,NU__EndDate__c,NU__StartDate__c ' +
- 'FROM NU__CommitteeMembership__c ' +
- 'WHERE CALENDAR_YEAR(NU__StartDate__c)= '+thisYear
- );
- //Declare new list for accounts
- List<Id> memberId = new List<Id>();
- Map<Id, AACE_CommitteeEmail__c> emails = new Map<Id, AACE_CommitteeEmail__c>();
- List<AACE_CommitteeEmail__c> finalList = new List<AACE_CommitteeEmail__c>();
- System.debug('welcome emails '+committeeMemberships);
- //Loop through committeememberships to get individual account Ids
- for(NU__CommitteeMembership__c cm : committeeMemberships){
- if(memberId.contains(cm.NU__Account2__c)){
- //This fires when the member has more than one committee
- 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;
- }else{
- memberId.add(cm.NU__Account2__c);
- 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 );
- emails.put(cm.NU__Account2__c,a);
- }
- }
- System.debug('EMAILS '+emails);
- System.debug('president '+president);
- for (Id key : emails.keySet()) {
- // The "key" variable is also available inside the loop
- finalList.add(emails.get(key));
- // ... emailing logic
- }
- //This is all of the individual Committee Members
- System.debug('Right here '+finalList);
- try {
- insert finalList;
- } catch(DmlException e) {
- System.debug('An unexpected error has occurred: ' + e.getMessage());
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement