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
EvertonSzekeresEvertonSzekeres 

Date Format Email Body

Hi,

 

I'm tryin to send a date field by email.

But my date is showing this MM/dd/yyyy.

 

And I am looking for this dd/MM/yyyy.

 

So I tried:

 

trigger Email_EventoFinalizado on Case (after insert, after update) {

List<Case> CaseMap = new List<Case>();
    for(Case c : Trigger.new){
        IF(c.motivo__c == 'Último evento'){
     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
          String[] toAddresses = new String[]{};   
            toAddresses.add('everton.gomes@cp7.com.br');
          mail.setToAddresses(toAddresses);          

          mail.setSubject(''+c.subject+'');
            
            Datetime myDT = Datetime.now();
            String myDate = myDT.format('dd/MM/yyyy');
            
            mail.setHtmlBody('<p>Turma: '+c.PIT__c+' - '+c.Nome_da_turma__c+'</p><p>Evento: '+c.Nome_do_evento__c+'</p><p>Data&colon; '+myDT(c.Data_do_evento__c)+'</p><p>'+c.Description+'</p>');
 
          Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
        }               
    }
}

 Error: Method does not exist or incorret signature : myDT(Date)

 

Thanks !

Best Answer chosen by Admin (Salesforce Developers) 
Sean TanSean Tan

The error simply boils down to your syntax. If you need to format the date in that specified format, since Salesforce does not have a Date.format method that allows you to specify the format (it only returns it in context of the locale of the user), you will need to convert it to a date time first if you want this. Something like this should work:

 

trigger Email_EventoFinalizado on Case (after insert, after update) {
    
    List<Case> CaseMap = new List<Case>();
    for(Case c : Trigger.new){
        IF(c.motivo__c == 'Último evento'){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[]{};   
            toAddresses.add('everton.gomes@cp7.com.br');
            mail.setToAddresses(toAddresses);          
            
            mail.setSubject(''+c.subject+'');
            
            DateTime myDT = DateTime.newInstance(c.Data_do_evento__c.year(), c.Data_do_evento__c.month(), c.Data_do_evento__c.day(), 0, 0, 0);
            String myDate = myDT.format('dd/MM/yyyy');
            
            mail.setHtmlBody('<p>Turma: '+c.PIT__c+' - '+c.Nome_da_turma__c+'</p><p>Evento: '+c.Nome_do_evento__c+'</p><p>Data&colon; '+myDate+'</p><p>'+c.Description+'</p>');
            
            Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
        }               
    }
}

 

All Answers

AshlekhAshlekh

 

Hi ,

 

The alternative is :

 

Date  d = Date.today(); 

String mydate = d.day()+'/'+d.month()+'/'+d.year();

 

 

It this post helps you then please mark it as a solution and don't forget to give me kudo's.

 

Thanks

Ashlekh

crop1645crop1645

I'm puzzled here by the original code sample, the following works just fine

 

Execute Anonymous: Datetime myDT = Datetime.now();
Execute Anonymous: String myDate = myDT.format('dd/MM/yyyy');
Execute Anonymous: system.debug('myDate='+ myDate);
18:22:04.039 (39277000)|EXECUTION_STARTED
18:22:04.039 (39287000)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
18:22:04.039 (39950000)|USER_DEBUG|[3]|DEBUG|myDate=10/09/2013

 

 

 

EvertonSzekeresEvertonSzekeres

I´m trying to make this field "data_do_evento__c" showing like this "dd/MM/yyyy"

 

trigger Email_EventoFinalizado on Case (after insert, after update) {

List<Case> CaseMap = new List<Case>();
    for(Case c : Trigger.new){
        IF(c.motivo__c == 'Último evento'){
     Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
          String[] toAddresses = new String[]{};   
            toAddresses.add('everton.gomes@cp7.com.br');
          mail.setToAddresses(toAddresses);          

          mail.setSubject(''+c.subject+'');
            
            Datetime myDT = Datetime.now();
            String myDate = myDT.format('dd/MM/yyyy');
            
            mail.setHtmlBody('<p>Turma: '+c.PIT__c+' - '+c.Nome_da_turma__c+'</p><p>Evento: '+c.Nome_do_evento__c+'</p><p>Data&colon; '+myDT(c.Data_do_evento__c)+'</p><p>'+c.Description+'</p>');
 
          Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
        }               
    }
}

 thanks,

Sean TanSean Tan

The error simply boils down to your syntax. If you need to format the date in that specified format, since Salesforce does not have a Date.format method that allows you to specify the format (it only returns it in context of the locale of the user), you will need to convert it to a date time first if you want this. Something like this should work:

 

trigger Email_EventoFinalizado on Case (after insert, after update) {
    
    List<Case> CaseMap = new List<Case>();
    for(Case c : Trigger.new){
        IF(c.motivo__c == 'Último evento'){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[]{};   
            toAddresses.add('everton.gomes@cp7.com.br');
            mail.setToAddresses(toAddresses);          
            
            mail.setSubject(''+c.subject+'');
            
            DateTime myDT = DateTime.newInstance(c.Data_do_evento__c.year(), c.Data_do_evento__c.month(), c.Data_do_evento__c.day(), 0, 0, 0);
            String myDate = myDT.format('dd/MM/yyyy');
            
            mail.setHtmlBody('<p>Turma: '+c.PIT__c+' - '+c.Nome_da_turma__c+'</p><p>Evento: '+c.Nome_do_evento__c+'</p><p>Data&colon; '+myDate+'</p><p>'+c.Description+'</p>');
            
            Messaging.SendEmail(new Messaging.SingleEmailMessage[] {mail});
        }               
    }
}

 

This was selected as the best answer