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
RP123RP123 

Exclude email sending on weekend

I am trying to write a schduler where I need to exclude the email sending on weekend (Saturday and Sunday)

For ex: If a Opportunity is created then the email should be trigger on next day.
Also, If the opportunity is created on Friday then the email should be trigger on Monday.

Is it possible to achive above logic using process builder or flow?
 

vxgbf gdsgfgvxgbf gdsgfg
There are some interesting things that we can add in the email directeries you can click here (https://bestportableblenderonline.com/) and learn about it in details.
Suraj Tripathi 47Suraj Tripathi 47
Hi RP123,

I have written a batch for your requirement, please follow the below code for your reference.

Batch Class:
public class SendEmailsExceptWeekend implements Database.Batchable<sObject>, Database.Stateful,Schedulable {
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('select Id,createdDate, Owner.Email from Opportunity where Contact.Email != null AND createdDate< today AND Mail__c=false');
    }
    public void execute(Database.BatchableContext bc, List<Opportunity> opportunityList){
        try{
            System.debug(opportunityList);
            if(opportunityList != null && opportunityList.size()>0){
                list< Messaging.SingleEmailMessage> emailList = new list<Messaging.SingleEmailMessage>();
                list<string> address = new list<String>();
                for(Opportunity opObj :opportunityList){
                    address.add(opObj.Owner.Email);
                    system.debug(address);
                    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                    email.setSubject('Alert Opportunity');
                    email.setPlainTextBody('A NEW Opportunity IS INSERTED IN SALESFORCE');   
                    email.setToAddresses(address);
                    emailList.add(email);
                    opObj.Mail__c=true;
                }
                system.debug(emailList.size());
                if(emailList.size()>0){
                    Messaging.sendEmail(emailList);
                    system.debug('mail sent');
                }
                update opportunityList;
            }else{
                system.debug('No opportunity');
            }
        }catch(Exception e){
            system.debug('Error is : '+ e.getMessage()+'at line  no: ' +e.getLineNumber());
        }
    }
    public void finish(Database.BatchableContext bc){
        system.debug('Email is sent');
    }
    public void execute(SchedulableContext sCObject){
        Database.executeBatch(new SendEmailsExceptWeekend());
    }
}

Then schedule this batch class from Apex Classes as shown in the below image.
User-added image

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too

Thanks and Regards,
Suraj Tripathi