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 another task owner

Hi there,

 

I was trying create a trigger to send an email notification for another task owner when the first task is completed.

And they have a relation in the same case.

 

My code is getting error:

 

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}';
    
    List<Id> ownerIds = new List<Id>();
  	for (Task task : Trigger.new) {
    if (task.Subject == 'Impressão - Preencher quantidade de fotos') {
      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);
  							}
    
    Map< Id, Case > cases = new Map< Id, Case >( );
  	for(Task record: Trigger.new ) {
    if (record.WhatId != null && record.WhatId.getSObjectType( ) == case.SobjectType )  {
      cases.put( record.WhatId, null );
    }
    }
  
	for (Task task : Trigger.new) {
        if (task.Subject == 'Impressão - Formando tem fotos separadas?' && task.status == 'Conclúido'){
        if (new String[] {cases.put(task.whatid)})
    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');
      }
    }
}

When task with subject = "Impressão - Formando tem fotos separadas?" is completed.

The task owner with the subject = "Impressão - Preencher quantidade de fotos" will recieve an email notification.

Just send the email only when the whatID is the same.

Vinit_KumarVinit_Kumar

What is the error message you are getting.

Everton CP7Everton CP7

Thanks !

 

I'm getting trouble with "MAP".

"Unexpected token: MAP".

 

I can't enter in some page documentations here.

I leave the proxy here and the same happens.

Vinit_KumarVinit_Kumar

It is still not clear,I mean the error is on which line.Could you please elaborate?

Everton CP7Everton CP7

Line 19.

 

Map< Id, Case > cases = new Map< Id, Case>( );

Vinit_KumarVinit_Kumar

Everything looks good except spaces.Can you remove spaces and try :-

 

Map<Id,Case> cases = new Map<Id,Case>();

Everton CP7Everton CP7

I changed there and the error proceed.