function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ashish Kumar YadavAshish Kumar Yadav 

how to Send email to account team members.if MSA expiry Date is 30 days.

please find below code --

global class SendemailMSA30days implements Database.Batchable<sObject>,Database.AllowsCallouts,Database.Stateful
{
    List<Account> acclis;
    public SendemailMSA30days(){
    }    
    global Database.QueryLocator start(Database.BatchableContext BC){
        List<ID> idss=new List<ID>();
        String soqlQuery = ''; 
        date todaydate = system.Today();
        acclis=[SELECT ID,Name,MSA_expiry_Date__c,OwnerId,AccountOwnerName__c,OwnerEmail__c,OwnerManagerEmail__c FROM Account where MSA_expiry_Date__c!=NULL];
        System.debug('acclis=='+acclis.size());
        
         //new Code
        List<AccountTeamMember> ATM = new List<AccountTeamMember>(
        [SELECT UserId FROM AccountTeamMember WHERE AccountId =:accid.Id]);
         
        for(Account accid:acclis){
            Date contEnd = accid.MSA_expiry_Date__c;
            Integer dayDiff = todaydate.daysBetween(contEnd);
            System.debug('dayDiff@@'+dayDiff);
            if(dayDiff == 30){
                Id newId = accid.Id;
                idss.add(newId);
            }
        }
        System.debug('idss====>'+idss.size());
        
        if(idss.size()>0){
            soqlQuery='SELECT id,Name,MSA_expiry_Date__c,OwnerId,AccountOwnerName__c,OwnerEmail__c,OwnerManagerEmail__c FROM Account where Id IN:idss';
        }
        else{
            soqlQuery='SELECT id,Name,MSA_expiry_Date__c,OwnerId,AccountOwnerName__c,OwnerEmail__c,OwnerManagerEmail__c FROM Account LIMIT 0';
        }
        System.debug('soqlQuery-------->'+soqlQuery);
        return Database.getQueryLocator(soqlQuery);
    }
    
        
    global void execute(Database.BatchableContext BC, List<Account> scope){
        System.debug('scope==>'+scope);
        System.debug('scope==>'+scope.size());
        String Shivams=Account__c.getValues('Fetchadminsemail').AdminEmailAddress__c;
        String shalets=Account__c.getValues('Fetchadminsemail').BussinessHeadEmail__c;
        System.debug('Email'+Shivams+'-'+shalets);
        List<Id> userlistid = new List<Id>();
        List<User> uslist=new List<User>();
        String AccownerName;
        List < Messaging.SingleEmailMessage > mailList = new List < Messaging.SingleEmailMessage > (); 
        /* for (Account acc: scope) {
userlistid.add(acc.OwnerId);
}
System.debug('userlistid=='+userlistid);
System.debug('userlistidsize()=='+userlistid.size());*/
        List < String > toAddresses = new List < String > ();
        List < String > CCAddresses = new List < String > ();
        CCAddresses.add(Shivams);
        CCAddresses.add(shalets);
        // uslist=[SELECT Id, Email,Manager.Email FROM User where ID IN:userlistid];
        /* for(User c : [SELECT Id,Name,Email,Manager.Email FROM User where id IN:userlistid]){
AccownerName=c.Name;
toAddresses.add(c.Email);
CCAddresses.add(c.Manager.Email);
System.debug('toAddresses'+toAddresses);
System.debug('CCAddresses-111---'+CCAddresses);
System.debug('CCAddresses--111--'+CCAddresses.size());
}*/
        for(Account a:scope){
            //send single emails to these contacts
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            toAddresses.add(a.OwnerEmail__c);
            CCAddresses.add(a.OwnerManagerEmail__c);
            mail.setToAddresses(toAddresses);
            Date dt = a.MSA_expiry_Date__c;
            String dtStr = dt.format();
            System.debug('dtStr=='+dtStr);
            mail.setCcAddresses(CCAddresses);
            System.debug('toAddresses----'+toAddresses);
            System.debug('CCAddresses----'+CCAddresses);
            System.debug('CCAddresses----'+CCAddresses.size());
            mail.setSubject('Reminder for MSA expirying within 30 days');
            String messageBody  ='Hello '+a.AccountOwnerName__c+ '<br/>'; 
            messageBody +='<br/>';
            messageBody +='Your MSA signed with the company,'+a.Name+',is getting expired 30 days from today on '+dtStr+'. Please get in touch with the relevant stakeholders.<br/>';
            messageBody +='<br/>';
            messageBody +='Thank you,<br/>';
            messageBody +='Salesforce Admin';
            mail.setHtmlBody(messageBody);
            mailList.add(mail);
            toAddresses.clear();
            CCAddresses.clear();
            CCAddresses.add(Shivams);
            CCAddresses.add(shalets);
        }
        System.debug('mailList@@@'+mailList.size());
        Messaging.sendEmail(mailList);
    }
    global void finish(Database.BatchableContext BC)
    {
        
    }
}

 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Ashish,

Can you elaborate the issue you are facing and scenario you would be implementing?

Looking forward to hearing back from you.

Regards,
Anutej
Ashish Kumar YadavAshish Kumar Yadav
Hi Anutej

Here we are implementing account team member email id we are adding in cc. based on cda expiry date and msa expiry date 30 days and 45 days.in future date.Reminder for CDA expirying within 45 days we are giving reminder to user.same for 30days.