You need to sign in to do that
Don't have an account?
Chris Lam
Email to Custom Object apex class unable to match up customID in email subject with custom object
Hi Everyone,
I am trying to create an Apex class which will add the incoming email message which contains projectID in the subject heading into an existing project(custom objec) and discard the email message if it doesn't match any of our project ID.
The test class works fine but is not able to trigger the true condition for the below IF statement even though the System.debug(myMatcher.group()); contains a valid Name in the project__c database.
IF ([SELECT COUNT() FROM project__c WHERE Name =:myMatcher.group()] == 1)
I have ran a similar Apex code as below below using developer console (Open Execute Anonymous Window) and the three system debugs are returning TRUE result of the IF statement:
USER_DEBUG [6]|DEBUG|UKGWY0000000002
USER_DEBUG [7]|DEBUG|1
USER_DEBUG [9]|DEBUG|Matched
Whereby running the test class returns below DEBUG results:
USER_DEBUG [19]|DEBUG|UKGWY0000000002
USER_DEBUG [58]|DEBUG|No Match Found
My custom email handler class is as below
Can anyone tell me what I have done wrong?
Kind regards,
Chris
I am trying to create an Apex class which will add the incoming email message which contains projectID in the subject heading into an existing project(custom objec) and discard the email message if it doesn't match any of our project ID.
The test class works fine but is not able to trigger the true condition for the below IF statement even though the System.debug(myMatcher.group()); contains a valid Name in the project__c database.
IF ([SELECT COUNT() FROM project__c WHERE Name =:myMatcher.group()] == 1)
I have ran a similar Apex code as below below using developer console (Open Execute Anonymous Window) and the three system debugs are returning TRUE result of the IF statement:
USER_DEBUG [6]|DEBUG|UKGWY0000000002
USER_DEBUG [7]|DEBUG|1
USER_DEBUG [9]|DEBUG|Matched
Whereby running the test class returns below DEBUG results:
USER_DEBUG [19]|DEBUG|UKGWY0000000002
USER_DEBUG [58]|DEBUG|No Match Found
My custom email handler class is as below
global class CI_EmailToSalesForce implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.Inboundenvelope envelope) { Messaging.InboundEmailResult result = new Messaging.InboundEmailResult(); //Set the Regex Matching Pattern to look for Project number in the email subject heading String EmailSubject = email.subject; String regexMatch = '^(UKGWY\\d+\\b?)'; //Declare a new instance of the Project object project__c p; try { // Check to see if the email subject contains any project ID Pattern MyPattern = Pattern.compile(regexMatch); Matcher MyMatcher = MyPattern.matcher(EmailSubject); if(myMatcher.find()){ System.debug(myMatcher.group()); } //Condition if there is exactly one project ID match from email subject if ([SELECT COUNT() FROM project__c WHERE Name =:myMatcher.group()] == 1) { system.debug('Matched'); p = [SELECT ID FROM project__c WHERE Name =:myMatcher.group()]; Note note = new Note(); note.Title = email.fromName + '(' +DateTime.now() + ')'; note.Body = email.plainTextBody; note.ParentId = p.Id; insert note; // Save attachments, if any if (email.binaryAttachments!=null && email.binaryAttachments.size() > 0) { for (Messaging.Inboundemail.BinaryAttachment bAttachment :email.binaryAttachments) { Attachment attachment = new Attachment(); attachment.ContentType = bAttachment.mimeTypeSubType; attachment.Name = bAttachment.fileName; attachment.Body = bAttachment.body; attachment.ParentId = p.Id; insert attachment; } } else if (email.textAttachments!=null && email.textAttachments.size() > 0) { for (Messaging.Inboundemail.TextAttachment tAttachment :email.textAttachments) { Attachment attachment = new Attachment(); attachment.Name = tAttachment.fileName; attachment.Body = Blob.valueOf(tAttachment.body); attachment.ParentId = p.Id; insert attachment; } } } //Condition if there are more than one project ID match from email subject else if ([SELECT COUNT() FROM Project__c WHERE Name =:myMatcher.group()] > 1) { system.debug('More than 1 project ID found'); } else { system.debug('No Match Found'); } result.success = true; } catch (Exception e) { result.success = false; result.message = 'Oops, I failed.'; } return result; } }My test class is as below:
@IsTest public class CI_EmailToSalesForce_Test { static testMethod void testCI_EmailToSalesForce() { // Create a new email, envelope and attachment object Messaging.InboundEmail email = new Messaging.InboundEmail(); Messaging.InboundEnvelope env = new Messaging.InboundEnvelope(); Messaging.InboundEmail.BinaryAttachment att = new Messaging.InboundEmail.BinaryAttachment(); // Setup the data for the email email.subject = 'UKGWY0000000002 This is a test'; email.plainTextBody = 'This should become a note'; env.fromAddress ='test@test.com'; String contactEmail = 'test@test.com'; att.body = blob.valueOf('test attachment'); att.fileName = 'testfile.txt'; att.mimeTypeSubType = 'text/plain'; email.binaryAttachments = new Messaging.InboundEmail.binaryAttachment[] {att}; // Call the TSG_Email class and test this with the above data CI_EmailToSalesForce CI_EmailObjTst = new CI_EmailToSalesForce(); Test.startTest(); CI_EmailObjTst.handleInboundEmail(email, env); Test.stopTest(); } }
Can anyone tell me what I have done wrong?
Kind regards,
Chris
If you want Org data (Sandbox OR Production) to be visible in the test class, then you will have to explicity tell Test class to see your org data.
Modify your first line of the test class - But, Its not a good practice. You should avoid it, if you can.
Thanks,
Sumit Kumar Singh
All Answers
I can't see any "project__c" rec in the Test Class. Insert a dummy "project__c" record in the test class, your problem should get resolved.
Thanks,
Sumit Kuamr Singh
Thank you for the reply. I don't believe that I need to invoke the project__c object in the test class. What I am trying to achieve
1) Match the project ID in email subject with existing project ID
2) If True, add this email into the notes/activities section of the project record
3) if false, discard this email.
Right now, the apex test class works fine in Console Anonymous Window which returns true condition without the need to invoke the project__c object. However the test class is returning false even though UKGWY0000000002 is a valid name for an existing project record in sandbox.
Cheers,
Chris
If you want Org data (Sandbox OR Production) to be visible in the test class, then you will have to explicity tell Test class to see your org data.
Modify your first line of the test class - But, Its not a good practice. You should avoid it, if you can.
Thanks,
Sumit Kumar Singh
Thank you for the info. I am still new to Salesforce development but understood the issue now. To make the test class trigger all scenario, I should create project record in the test class rather than letting test class access Sandbox or Production data.
Thanks,
Chris