+ Start a Discussion
shobana ravi 9shobana ravi 9 

Email Notification

Hi,

 I want to send a email notification to customer as payment reminder. if customers credit term is 30, so he should be notified 5th each month and if the customers credit term is 35, he should be notified 7th each month.
how can i achieve this using apex.

please help me on this.
Harsh P.Harsh P.
Hi Shobana,

Use below trigger.
trigger SendEmailAccordingToCreditTerm on Payment__c(before insert) {
    
    date myDate1 = date.newInstance(1989, 4, 5);
    date myDate2 = date.newInstance(1989, 4, 7);
    Integer day1 = myDate1.day();
    Integer day2 = myDate2.day();

    if(Trigger.isbefore && Trigger.isInsert){
        for(Payment__c p : trigger.new){
            if(p.CustomerCreditTerm__c== 30 && system.today().day() == day1 && p.email__c != null){
                Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                message.toAddresses = new String[] { p.email__c };
                //message.optOutPolicy = 'FILTER';
                message.subject = 'Reminder';
                message.plainTextBody = 'Your credit term is 30.This is first reminder to you';
                Messaging.SingleEmailMessage[] messages =   new List<Messaging.SingleEmailMessage> {message};
                Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
            }
            if(p.CustomerCreditTerm__c== 35 && system.today().day() == day2 && p.email__c != null){
                Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                message.toAddresses = new String[] { p.email__c };
                //message.optOutPolicy = 'FILTER';
                message.subject = 'Reminder';
                message.plainTextBody = 'Your credit term is 35.This is first reminder to you';
                Messaging.SingleEmailMessage[] messages =   new List<Messaging.SingleEmailMessage> {message};
                Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
            }
        }
    }

}

Mark as Best Ansewer to help other Peoples.

Regards,
Harsh P.