You need to sign in to do that
Don't have an account?

Add Conditions in Send Email Notification
Hello All,
I had a requirement where i need to send an email notification 100 days before the contract end date.So based on this we had a batch and schedule class written.Now we would like to add few more condition in the code.
We have two picklist value as 'Status Renewed' and "Status Renewed next Year".This picklist field has some values as Status renewed for nxt quarter , pipeline etc the other picklist has renewed and renewed lost.
Batch and Schedule Class:
I had a requirement where i need to send an email notification 100 days before the contract end date.So based on this we had a batch and schedule class written.Now we would like to add few more condition in the code.
We have two picklist value as 'Status Renewed' and "Status Renewed next Year".This picklist field has some values as Status renewed for nxt quarter , pipeline etc the other picklist has renewed and renewed lost.
Condition is when the "Status Renewed" is equal to "Status renewed for next quarter " or "Pipeline" AND when "Status Renewed next Year" is not equal to "Renewed" or"Renewed lost".So how do i give this condition in an execute method.When this condition is satisfied ,then the email notification should be sent to the owner before 100 days.
Batch and Schedule Class:
global class NotificationEmailtoAccountExecutive implements Database.Batchable < sObject >, Schedulable, Database.Stateful { global List<String> errorMessages = new List<String>(); global Database.QueryLocator start(Database.BatchableContext bc) { Date ed = Date.today().addDays(100); System.debug(Date.today().addDays(100)); set<Id> setContractIds = new set<Id>(); for(Contract_role__c objContract: [SELECT Contract__c FROM Contract_role__c WHERE Role__c = 'Subscription Administrator' AND Contract__r.EndDate =: ed]) { setContractIds.add(objContract.Contract__c); } return Database.getQueryLocator('Select id, Contract_Name__c , EndDate ,Contact_Email__c, Contract_End_Date_2__c, Owner.Email, Owner.Manager.Email ,Account.Owner.Email,Account.Owner.Manager.Email FROM Contract WHERE Id IN: setContractIds'); } global void execute(Database.BatchableContext bc, List < Contract > recs) { List < Messaging.SingleEmailMessage > mailList = new List < Messaging.SingleEmailMessage > (); for (Contract c: recs) { if (c.Contact_Email__c != null) { List < String > toAddresses = new List < String > (); Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); toAddresses.add(c.Contact_Email__c); toAddresses.add(c.Owner.Email); toAddresses.add(c.Account.Owner.Email); toAddresses.add(c.Owner.Manager.Email); toAddresses.add(c.Account.Owner.Manager.Email); mail.setToAddresses(toAddresses); mail.setSubject('Notification Before 100 Days of Contract End Date'); String messageBody = '<html><body>Hi ' + c.Contract_Name__c + ',<br>Your Contract Expires within 100 Days . <br>Kindly take action.<br><br><b>Regards,</b><br>ADP</body></html>'; mail.setHtmlBody(messageBody); mailList.add(mail); } } Messaging.sendEmail(mailList); } global void finish(Database.BatchableContext bc) { AsyncApexJob aaj = [Select Id, Status, NumberOfErrors, JobItemsProcessed, MethodName, TotalJobItems, CreatedBy.Email from AsyncApexJob where Id =:BC.getJobId()]; // Send an email to the Apex job's submitter notifying of job completion. Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {aaj.CreatedBy.Email}; mail.setToAddresses(toAddresses); mail.setSubject('JOB Salesforce NotificationEmailtoAccountExecutive Finished: ' + aaj.Status); String bodyText='Total Job Items ' + aaj.TotalJobItems + ' Number of records processed ' + aaj.JobItemsProcessed + ' with '+ aaj.NumberOfErrors + ' failures.\n'; bodyText += 'Number of Error Messages ' + errorMessages.size() + '\n'; bodyText += 'Error Message' + String.join(errorMessages, '\n'); mail.setPlainTextBody(bodyText); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } global void execute(SchedulableContext SC) { NotificationEmailtoAccountExecutive batchable = new NotificationEmailtoAccountExecutive(); database.executebatch(batchable); } }Any help very much appreciated.
Please try the below code:
or
Please do let me know if it helps you.
Regards,
Mahesh
line breaks not allowed in string literals at line 18 column -1
Try this,
Regardsm
Mahesh
I tried in this way : But still the system throws an error as : Any Suggestion.
it should work
Regards,
Mahesh
It works when i use : and a email is sent .When i start giving / adding the condition in the query , i cant see any email is sent and in the debug logs nothing gets executed.Any help very much appreciated.
Regards,
Mahesh
1. Run batch apex on daily basis to send email notification
2. Email should be send 30 days, 60 days and 90 days before contract end date on opportunity where stage is 8
3. Email should go to only those contacts in associated account where 'sendemail' checkbox is true.
Now my problem is how to add third condition and how to send email to contact email address.