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 

Email notification to owner's task

Hi there,

 

I have workflow rules that open task for multiple users when someone create a case.

 

The problem here is when some user complete your task. Noone know about it.
So I need an email notification that send an email to the rest of owner's task in the same case.

 

I tried some codes and nothing.

 

Thanks for help !

 

 

 

Vinit_KumarVinit_Kumar

Everton,

 

You need to create a before update Trigger on Task and based on ParentId you need to query all the related Task and then put all the owners into a list and then send emails through Messaging.SinglEmailMessage.

joshbirkjoshbirk

The workflow itself can't handle the email?

Everton CP7Everton CP7

It's possible with workflows.

But as I have a several workflows based on task's creation I think with a trigger will be better.

 

Thanks Vinit !

I know how to put this in work with just one owner. My difficulty is in the ParentID.

 

I tried this:

 

trigger EmailOwners on Task (before update) {
    static final String SUBJECT = 'TESTE';
    static final String BODY = 'Relativo a: {0}\nStatus: {1}\nAssunto: {2}\nComentário Enviado: {3}\nComentário de Conclusão: {4}';
    
  String caseKeyPrefix = Case.sObjectType.getDescribe().getKeyPrefix();
    Set<Id> caseIds = new Set<Id>();
    for (Task t: Trigger.new)     {
     	if (t.WhatId != null) { 
	 String taskWhatId = t.WhatId;
		if (taskWhatId.startsWith('5001' ) ) {
                        caseIds.add(t.WhatId);
			}
		}
    }
    List<Id> ownerIds = new List<Id>();
  	for (Task task : Trigger.new) {
    if (task.Status == 'Nenhum') {
      ownerIds.add(task.OwnerId);
    	}
    }
        
    List<User> owners = [Select Id, Email from User Where Id in :ownerIds];
  	Map<Id,String> ownerIdsToEmails = new Map<Id,String>();
  	for (User owner : owners) {
    	ownerIdsToEmails.put(owner.Id,owner.Email);
  							}
    
for (Task task : Trigger.new) {
    if (task.Status == 'Concluído') {
      try {

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[] {ownerIdsToEmails.get(task.ownerId)});
        mail.setSubject(SUBJECT);
        
        String url = System.URL.getSalesforceBaseUrl().toExternalForm() + '/' + task.WhatId;
        
        mail.setPlainTextBody(String.format(BODY,new String[]{
          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');
      }
    }
}
}

 

"Nenhum" = My inicial status. When worflow create a task.

"Concluido" = When someone completed a task.