function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
CezuCezu 

Heeelp Email Service Test Class

Hi i must wrote test class to this class but i dont know how 

 

Can somebody help ?? 

 

global class Emailservice implements Messaging.InboundEmailHandler { 
  global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
    Messaging.InboundEnvelope envelope) {
    
    Messaging.InboundEmailResult result = new Messaging.InboundEmailresult(); 
    
    Contacts_History__c emaill = new Contacts_History__c ();
    List<Candidate__c> CandiList = [SELECT ID FROM Candidate__c 
                                   WHERE E_mail__c =: email.fromAddress LIMIT 1];                             

    emaill.Subject__c = email.subject;
    emaill.Body__c = email.htmlBody;
    emaill.Candidate__c = CandiList[0].id;
    emaill.Date_of_contact__c = datetime.now();
    emaill.Way_of_contact__c = 'E-mail';
       
    
    String subToCompare = 'REC-';               
    Integer startNum = email.subject.IndexOf(subToCompare);                                                              
    String phrase = email.subject.mid(startNum, 10);
                        
   /* List<Recruitment__c> recruitmentList = [SELECT ID FROM Recruitment__c 
                                   WHERE name = :phrase LIMIT 1];
        if (!recruitmentList.isEmpty()) {
            emaill.Recruitment__c = recruitmentList[0].id;  
            }*/
             
        insert emaill;
        
     if (email.textAttachments != null) {                                                      
            for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
                Attachment attachment = new Attachment();
     
                attachment.Name = tAttachment.fileName;
                attachment.Body = Blob.valueOf(tAttachment.body);
                attachment.ParentId = emaill.Id;
                insert attachment;
            }
        }
        //obsluga zalacznikow binarnych
        if (email.binaryAttachments != null){                                                       
            for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
                Attachment attachment = new Attachment();
     
                attachment.Name = bAttachment.fileName;
                attachment.Body = bAttachment.body;
                attachment.ParentId = emaill.Id;
                insert attachment;
            }
        }
    
    return result;
    }
}
sfdcfoxsfdcfox
Jeff Douglas has written a nice article on that very subject:

http://blog.jeffdouglas.com/2010/03/12/writing-an-inbound-email-service-for-salesforce-com/