You need to sign in to do that
Don't have an account?
Luca Benedettini 7
Apex Class for email service
Hi,
I wrote an apex class for one Salesforce email service. I need that when an email arrive, case is created and email's attachemnts attach to the case created. When I test it, doesn't trhow errors or expetions, but also doesn't create case and attachment. Someone could please check my code and give me some tips please?
Here is my code
global class casewithattachments implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){
// Create an inboundEmailResult object for returning the result of the Force.com Email Service
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
//List of email's Attachments
List <Attachment> attch = new List<Attachment>();
// Initialize the Contact ID, so if no Contact is found, a case is still created
Id vconID;
//Case objects to be created
Case newCase = new Case();
// Try to lookup any contacts based on the email from address
// If there is more than 1 contact with the same email address
// an exception will be thrown and the catch statement will be called
try {
Contact vCon = [Select Id, Name, Email From Contact Where Email = :email.fromAddress Limit 1];
// Add a new Case to the contact record we just found above - if not Contact found, value remains null
vconID = vCon.ID;
// Insert the new Case and it will be created and appended to the contact record
//newCase.Description = myPlainText;
newCase.Description = email.htmlBody;
newCase.Priority = 'Medium';
newCase.Status = 'New';
newCase.Subject = email.subject;
newCase.Origin = 'Email';
newCase.ContactId = vconID;
insert newCase;
System.debug('New Case Object: ' + newCase );
}
// If there is an exception with the query looking up and the exception will be written to the Apex Debug logs
catch (System.QueryException e) {
System.debug('Query Issue: ' + e);
}
try{
for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
attch.add(new Attachment(Name = tAttachment.fileName,Body = Blob.valueOf(tAttachment.body),ParentId = newCase.Id));}
for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
attch.add(new Attachment(Name = bAttachment.fileName,Body = bAttachment.body,ParentId = newCase.Id)); }
System.debug('Attachemnt attached' );
}
catch (System.NullPointerException e) {
System.debug('Attachment Issue: ' + e);
}
insert attch;
// Set the result to true, no need to send an email back to the user with an error message
result.success = true; // Return the result for the Force.com Email Service
return result; }
}
I wrote an apex class for one Salesforce email service. I need that when an email arrive, case is created and email's attachemnts attach to the case created. When I test it, doesn't trhow errors or expetions, but also doesn't create case and attachment. Someone could please check my code and give me some tips please?
Here is my code
global class casewithattachments implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){
// Create an inboundEmailResult object for returning the result of the Force.com Email Service
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
//List of email's Attachments
List <Attachment> attch = new List<Attachment>();
// Initialize the Contact ID, so if no Contact is found, a case is still created
Id vconID;
//Case objects to be created
Case newCase = new Case();
// Try to lookup any contacts based on the email from address
// If there is more than 1 contact with the same email address
// an exception will be thrown and the catch statement will be called
try {
Contact vCon = [Select Id, Name, Email From Contact Where Email = :email.fromAddress Limit 1];
// Add a new Case to the contact record we just found above - if not Contact found, value remains null
vconID = vCon.ID;
// Insert the new Case and it will be created and appended to the contact record
//newCase.Description = myPlainText;
newCase.Description = email.htmlBody;
newCase.Priority = 'Medium';
newCase.Status = 'New';
newCase.Subject = email.subject;
newCase.Origin = 'Email';
newCase.ContactId = vconID;
insert newCase;
System.debug('New Case Object: ' + newCase );
}
// If there is an exception with the query looking up and the exception will be written to the Apex Debug logs
catch (System.QueryException e) {
System.debug('Query Issue: ' + e);
}
try{
for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
attch.add(new Attachment(Name = tAttachment.fileName,Body = Blob.valueOf(tAttachment.body),ParentId = newCase.Id));}
for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
attch.add(new Attachment(Name = bAttachment.fileName,Body = bAttachment.body,ParentId = newCase.Id)); }
System.debug('Attachemnt attached' );
}
catch (System.NullPointerException e) {
System.debug('Attachment Issue: ' + e);
}
insert attch;
// Set the result to true, no need to send an email back to the user with an error message
result.success = true; // Return the result for the Force.com Email Service
return result; }
}
Your code is absolutely fine. I guess you might not have set up the Email Service and the Email address. Please go through https://developer.salesforce.com/page/An_Introduction_To_Email_Services_on_Force.com documentation and set up the Email Service.
Also make sure that there must be a single contact in your org with the same email address otherwise the code segment: will give error. Let me know if you still face any problems.
Thanks
All Answers
Your code is absolutely fine. I guess you might not have set up the Email Service and the Email address. Please go through https://developer.salesforce.com/page/An_Introduction_To_Email_Services_on_Force.com documentation and set up the Email Service.
Also make sure that there must be a single contact in your org with the same email address otherwise the code segment: will give error. Let me know if you still face any problems.
Thanks
P.S. In my org in this moment there aren't Contacts , that may be the problem?