How to use map to match 2 object record and return value that match

I have scheduled apex class to send email notification to user. it gave the user a list of all customer data stored in salesforce.. now I want to change the output.. from giving all customer data to all user, to only give customer data that match with the user branch..

There are field called Branch__c on User object and Branch__c on Billing_Account__c (The object where I get the customer data)..

Someone told me to use “Map” but Im not familiar with “Map” .. yet.. can someone give me some guide?

How to match the Billing_Account branch and user branch then return the account data along with the email of the user?

This is my old code :

global class SchListBirthday implements Schedulable  {


public static String CRON_EXP = '0 0 0 25 * ? *';
global void execute(SchedulableContext ctx) {

    List mails = new List();
    EmailTemplate et = [SELECT Id, Subject, HtmlValue FROM EmailTemplate WHERE developerName = 'ET_List_Birthday'];

    Date now = Date.today();
    Integer xMonth = now.month();
    Integer xYear = now.year();
    Integer NextMonth;

    if(xMonth == 1){
        NextMonth = 2; 

    }else if(xMonth == 2){
        NextMonth = 3;

    }else if(xMonth == 3){
        NextMonth = 4; 

    }else if(xMonth == 4){
        NextMonth = 5;  

    }else if(xMonth == 5){
        NextMonth = 6; 

    }else if(xMonth == 6){            
        NextMonth = 7; 

    }else if(xMonth == 7){
        NextMonth = 8;  

    }else if(xMonth == 8){
        NextMonth = 9; 

    }else if(xMonth == 9){
        NextMonth = 10; 

    }else if(xMonth == 10){
        NextMonth = 11; 

    }else if(xMonth == 11){
        NextMonth = 12; 

    }else if(xMonth == 12){
        NextMonth = 1;
    }
    String subject = et.Subject;                            
    String htmlBody = et.HtmlValue;

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();                   
           htmlBody += '

'; List listContactBOD = [SELECT Id, Birthdate, Name, Email, Account.Name, Posisi__c FROM Contact WHERE CALENDAR_MONTH(Birthdate) =:NextMonth]; List listCompany = [SELECT Id, Account__c, Account__r.Company_Birthday__c, Account__r.Email__c, Account__r.Name, Branch__c, Account__r.Total_Revenue__c FROM Billing_Account__c WHERE CALENDAR_MONTH(Account__r.Company_Birthday__c) =:NextMonth ORDER BY Account__r.Total_Revenue__c DESC]; for (Contact cc : listContactBOD){ Integer birthYear = cc.Birthdate.Year(); String age = String.valueof(xYear - birthYear); String name = cc.Name; String company = cc.Account.Name; String birthdate = String.valueof(cc.Birthdate); String email = cc.Email; String posisi = cc.Posisi__c; htmlBody += ''; } htmlBody += '
Contact NameCompanyBirthdate (yyyy-mm-dd)AgeEmailTitle
' + name + '' + company + '' + birthdate + '' + age + ' Tahun' + email + '' + posisi + '


'; htmlBody += 'Berikut ini adalah list Customer yang akan ber-Ulang Tahun di Bulan depan :

'; for (Billing_Account__c ba : listCompany){ Integer birthYear = ba.Account__r.Company_Birthday__c.Year(); String age = String.valueof(xYear - birthYear); String company = ba.Account__r.Name; String birthdate = String.valueof(ba.Account__r.Company_Birthday__c); String email = ba.Account__r.Email__c; String cbng = ba.Branch__c; String revenue = String.valueof(ba.Account__r.Total_Revenue__c); htmlBody += ''; } htmlBody += '
Company NameBirthdate (yyyy-mm-dd)AgeEmailBranchTotal Revenue
' + company + '' + birthdate + '' + age + ' Tahun' + email + '' + cbng + '' + revenue + '



Terimakasih,


System Administrator

'; Set setsendTo = new Set(); List listNm = [SELECT Id, User__c, Email__c, Name FROM Named_User__c WHERE Email__c != NULL AND ( User__r.Profile.Name = 'Manager CS' OR User__r.Profile.Name = 'Customer Service' OR User__r.Profile.Name = 'Staff HPL')]; for (Named_User__c nm : listNm) { htmlBody = htmlBody.replace('{!User.Name}', nm.Name); setsendTo.add(nm.Email__c); } List sendTo = new List(); //Convert set to list sendTo.addall(setsendto); OrgWideEmailAddress owa = [select id, Address, DisplayName from OrgWideEmailAddress WHERE DisplayName = 'PT. Pelabuhan Indonesia II (Persero)' limit 1]; for(string sendToEmail : sendTo){ if(sendToEmail != null){ system.debug('===strOwnerEmail===='+sendToEmail); //String [] toaddress= New String[]{'rieftjong95@gmail.com'}; //mail.setTargetObjectId(sendTo); //mail.setToAddresses(sendTo); mail.setOrgWideEmailAddressId(owa.Id); mail.setToAddresses(sendTo); //mail.setSenderDisplayName('IPC - System Administrator'); mail.setSubject(subject); mail.setHtmlBody(htmlBody); List ccTo = new List(); //ccTo.add('rieftjong95@gmail.com'); //mail.setCcAddresses(ccTo); mail.setSaveAsActivity(false); mails.add(mail); } } Messaging.sendEmail(mails); } }

All topic