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
Everton CP7Everton CP7 

Notification after 30 days

I need notificiation email to owner's task when task still opens after 30 days.


I create a trigger and I think is correct.

But I don't know how to test this one.


I create a task with Activity Date 30 days before and I don't recieve any email.

And honestly I don't know if I messed up.


Here's my code using before update, before insert:


  List<Task> lst = [SELECT Id, ActivityDate FROM Task WHERE IsClosed = false AND ActivityDate >=];
  for (Task task : {


Thanks for help !

can you paste the complete code. looks like its truncated?
Everton CP7Everton CP7

Thanks Kartik !


trigger Email_tarefa_Sem_Fechar on Task (before insert, before update) {

  static final String SUBJECT = '30 DIAS';
  static final String BODY = 'Atribuido Para: {0}\nRelativo a: {1}\nStatus: {2}\nAssunto: {3}\nComentário Enviado: {4}\nComentário de Conclusão: {5}';

  List<Id> creatorIds = new List<Id>();
  List<Id> ownerIds = new List<Id>();
  for (Task task : {
    if (task.Status <> 'Concluído') {

  List<User> owners = [Select Id, Email from User Where Id in :ownerIds];
  Map<Id,String> ownerIdsToEmails = new Map<Id,String>();
  for (User owner : owners) {
  List<User> creators = [Select Id, FirstName, LastName From User Where Id in :creatorIds];
  Map<Id,String> creatorIdsToNames = new Map<Id,String>();
  for (User creator : creators) {
    creatorIdsToNames.put(creator.Id,creator.FirstName + ' ' + creator.LastName);
  List<Task> lst = [SELECT Id, ActivityDate FROM Task WHERE IsClosed = false AND ActivityDate >=];
  for (Task task : {
      try {

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[] {ownerIdsToEmails.get(task.ownerId)});
        String url = System.URL.getSalesforceBaseUrl().toExternalForm() + '/' + task.WhatId;
        mail.setPlainTextBody(String.format(BODY,new String[]{
          creatorIdsToNames.get(task.CreatedById), url, task.Status,
          task.Subject, task.Description, task.Coment_rio_de_conclus_o_de_tarefa__c
        Messaging.SendEmailResult[] result = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
        if (!result[0].isSuccess()) {
          System.debug(LoggingLevel.ERROR, 'Failed to send email'+result[0].getErrors()[0].getMessage());
      } catch (System.EmailException ex) {
        System.debug(LoggingLevel.ERROR, 'Encountered EmailException');
I am sorry i mis understood your question. I got it now. I dont think you can use a trigger to do what you are trying. You need to verify all the tasks to see if any open ones are available for 30 days. This can be done by Scheduled / Batch class that check for all tasks open and sends email. and it runs on a daily basis.
Everton CP7Everton CP7

I see.

I read some topics and I thik this will work.


I never done a code like this.

Can you help me?

Jerun JoseJerun Jose
Use the example mentioned
to start with. Let us know if you are stuck at any point.