+ Start a Discussion
Michel DouwmaMichel Douwma 

Problem with Apex Class

Hi,

 

I'm having a problem with a Apex Class i've written. The class is intended to convert incoming e-mails to Bugs (Custom Object) and notify the sender that his/her e-mail has been received. In the notification the Bug ID should be included. Unfortunaltely when submitting an e-mail, in the sent notification null is displayed in stead of the actual Bug ID.

 

Can anyone tell me what i'm doing wrong?

 

Thanks in advance!

 

This is the script:

 

 

global class IncomingEmailToBugxxxx2 implements Messaging.InboundEmailHandler {
 
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope envelope)
    {
 
 
  // Create an inboundEmailResult object for returning the result of the Apex Email Service
 Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
        try
        {
            // Obtain the applicants name and the position they are applying for from the email.
            String emailFrom = envelope.fromAddress;
            String emailBody = email.plainTextBody;
            String emailSubject = email.subject;
            

  // Process the email information.
            processBugEmail(emailFrom, emailBody, emailSubject, email.binaryAttachments);            

            // Success.
            result.success = true;          
        }
       catch (Exception e)
        {
            // Return an email message to the user.
            result.success = false;         
            result.message = 'An error occured processing your message. ' + e.getMessage();
        }
        return result;
   }

   public static void processBugEmail(String emailFrom, String emailBody, String emailSubject, Messaging.InboundEmail.BinaryAttachment[] binaryAttachments)
   {
 

 
  // New Task object to be created
 
   SFDC_Bug__c newBug = new SFDC_Bug__c();
       newBug.Problem_Description__c =  emailBody;
       newBug.Priority__c = 'P4 - Low';
       newBug.Status__c = 'Open';
       newBug.Problem__c = emailSubject;
  insert newBug;

       if (binaryAttachments!=null && binaryAttachments.size() > 0)
        {
            for (integer i = 0 ; i < binaryAttachments.size() ; i++)
            {
                Attachment attachment = new Attachment();
                attachment.ParentId = newBug.Id;
                attachment.Name = binaryAttachments[i].Filename;
                attachment.Body = binaryAttachments[i].Body;
                insert attachment;


            }
        }
        
        // Confirm receipt of the applicants email
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses( new String[] { emailFrom });
        mail.setSenderDisplayName('xxxx Bug Management');
        mail.setReplyTo('no-reply@xxxx.net');
        mail.setSubject('Bug registration notification - ' + newBug.Name +'');
        mail.setPlainTextBody('*** AUTOMATED MESSAGE - PLEASE DO NOT REPLY AS YOU WILL NOT RECEIVE A REPLY ***' +
        '\n\nDear Sir, Madam,' +
        '\n\nThank you for your e-mail regarding a bug in the xxxx-platform.' +
        '\n\nYour e-mail has been correctly received and has been registerd with the reference number ' + newBug.Name + '. We will investigate the bug you have reported as quickly as possible and provide you with the nessesary feedback.' +
        '\n\nIf you have any questions regarding this matter, please do not hesitate to contact us by sending an e-mail to xxxx or if you prefer, you can contact us by telephone at xxxx during business hours. While contacting us please quote the reference number mentioned above.' +
        '\n\nKind regards,' +
        '\n\nxxxx Support');
         
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });               
   }
   
   /*
    * Represents errors that have occured interpretting the incoming email message
    */
   class ProcessApplicantException extends Exception
   {
   }  
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
kyle.tkyle.t

I am pretty sure that you need to requery for "New Bug."  I am assuming that Name is set to be an auto-number.  In the scope of the class, you never set the name.  The name is set by salesforce automatically (outside the scope of this class) and therefor not available to you.  if you query for the bug it should be available to your.

 

one of many ways to accomplish this:

String newBugName = [select Name from SFDC_Bug__c where Id = :newBug.id].Name;

All Answers

kyle.tkyle.t

I am pretty sure that you need to requery for "New Bug."  I am assuming that Name is set to be an auto-number.  In the scope of the class, you never set the name.  The name is set by salesforce automatically (outside the scope of this class) and therefor not available to you.  if you query for the bug it should be available to your.

 

one of many ways to accomplish this:

String newBugName = [select Name from SFDC_Bug__c where Id = :newBug.id].Name;

This was selected as the best answer
Michel DouwmaMichel Douwma

Thanx! That did the trick :)