• Kash
  • NEWBIE
  • 0 Points
  • Member since 2009

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Dear Friends

I want to create a html attachment and attach it in email.
I got an exception when setting email attachment
body, the Apex codes like this:
=====================================================================
    // Encode the html string as base64 and then create an attachment  
    Blob html = Crypto.generateDigest('SHA1',Blob.valueOf(htmlBody));
    Attachment attachment = new Attachment();

    // set attachment body - base64, and
name
    attachment.Body = Blob.valueOf(EncodingUtil.base64Encode(html));
    attachment.Name = String.valueOf('Task.html');

    // if WhatId is not null, set the
attachment.ParentId to WhatId
    // else set the
attachment.ParentId to WhoId
    if (newTask.WhatId != null) {
        attachment.ParentId = newTask.WhatId;
    } else {
        attachment.ParentId = newTask.WhoId;
    }
    insert attachment;
   
           // add the
attachment to an email message
line251    Messaging.EmailFileAttachment[] fileAttachments = new Messaging.EmailFileAttachment[1];
line252    fileAttachments[0].setBody(attachment.body);
line253    fileAttachments[0].setFileName(attachment.Name);
line254    mail.setFileAttachments(fileAttachments);
=====================================================================
Error message like this:
SendAlertMail: execution of AfterInsert
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.SendAlertMail: line 252, column 21

But I can get the attachment.Name after the "insert attachment" operation.
And I can found the created attachment in related Opportunity.
Can anybody give me some advices?

Thanks


  • December 02, 2008
  • Like
  • 0