I have trigger where it sends email when record is deleted. It works fine but date format isn't getting right in email. It's showing yr-mm-dd 00:00:00 whereas i just need only date in mm-dd-yr format, any idea how to change it?

trigger DeleteEmail on Contract__c (after delete) {
    List<Messaging.SingleEmailMessage> emails = new 

    for (Contract__c cc : Trigger.old) {
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        email.setToAddresses(new String[] {'abc@abc.com'});
        email.setSubject('Contract '+cc.Name+ ' has been Deleted');
        email.setHtmlBody('This message is to alert you that ' + cc.Name + ' has been Deleted <br/><br/>'+'Contract Name: '+cc.Name+ ' <br></br> Contract ID: '+cc.Contract_ID__c+ ' <br></br> Contract Start Date: '+cc.Contract_Start_Date__c+ ' <br></br> Contract End Date : '+cc.Contract_End_Date__c );

You probably want to use: cc.Contract_Start_Date__c.Date().format(). It sounds like the fields were configured as Date/Time values instead of Date values, which is why they include the time component by default. Using date() returns just the date portion.

| improve this answer | |
  • 1
    Awesome it worked – Admin_BA Apr 10 '15 at 20:28
  • My test class isn't giving me coverage, what should i modify it to give me enough coverage to push it to prod? @isTest private class ContractDeleteEmailTest { static testMethod void myUnitTest() { Corporate_Contract__c cc=new Corporate_Contract__c(); cc.Name = 'Test Contract'; cc.Admin_Fee__c = 10; cc.Contract_Start_Date__c = Date.newInstance(2010,01,01); cc.Contract_End_Date__c = Date.newInstance(2020,02,01); insert cc; } } – Admin_BA Apr 10 '15 at 21:12
  • You need to trigger delete: insert cc; delete cc; } } – sfdcfox Apr 10 '15 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.