Friday, November 29, 2013

Test Class Example For Messaging.IndboundEmail Class

Apex Class:

 global class inBoundEmail implements Messaging.InboundEmailHandler
{
   global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,                 Messaging.InboundEnvelope envelope)
   {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
        String subToCompare = 'Create Contact';

       if(email.subject.equalsIgnoreCase(subToCompare))
        {
            Contact c = new Contact();
            c.Email=email.fromAddress;
           
            // capture phone number and city also from incoming email.
            // Splits each line by the terminating newline character
            // and looks for the position of the phone number and city
            String[] emailBody = email.plainTextBody.split('\n', 0);
            c.LastName=emailBody[0].substring(0);
            c.Phone = emailBody[1].substring(0);
            c.Title = emailBody[2].substring(0);
                     
            insert c;
           
            // Save attachments, if any
            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 = c.Id;
            insert attachment;
            }
           
            }

            //Save any Binary Attachment
           
            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 = c.Id;
            insert attachment;
            }
           }
        }

        result.success = true;
        return result;
           
   }
}

Test Class For Above Class:

   @isTest(seeAllData=true);
Private Class InBoundEmailTest()
{
//Test Method for main class
     static testMethod void TestinBoundEmail()
    {
       Test.startTest();
      // create a new email and envelope object
       Messaging.InboundEmail email = new Messaging.InboundEmail() ;
       Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
     
       // setup the data for the email
      email.subject = 'Create Contact';
      email.fromAddress = 'someaddress@email.com';
      email.plainTextBody = 'email body\n2225256325\nTitle';
     
      // add an Binary attachment

      Messaging.InboundEmail.BinaryAttachment attachment = new Messaging.InboundEmail.BinaryAttachment();
      attachment.body = blob.valueOf('my attachment text');
      attachment.fileName = 'textfileone.txt';
      attachment.mimeTypeSubType = 'text/plain';
      email.binaryAttachments = new Messaging.inboundEmail.BinaryAttachment[] { attachment };

 
      // add an Text atatchment
 
      Messaging.InboundEmail.TextAttachment attachmenttext = new Messaging.InboundEmail.TextAttachment();
      attachmenttext.body = 'my attachment text';
      attachmenttext.fileName = 'textfiletwo3.txt';
      attachmenttext.mimeTypeSubType = 'texttwo/plain';
      email.textAttachments =   new Messaging.inboundEmail.TextAttachment[] { attachmenttext };
     
     
      // call the email service class and test it with the data in the testMethod
      inBoundEmail  testInbound=new inBoundEmail ();
      testInbound.handleInboundEmail(email, env);
       

   }
}

Thursday, November 21, 2013

Like Operator in SOQL Query

Like Operator:

        Like Operator in SOQL act as a Comparison operator  for String Field Expression.Expression is true if the value in the specified fieldName matches the characters of the text string in the specified value. It provides a mechanism for matching partial text strings and includes support for wildcards.


Notes:

  • The % and _ wildcards are supported for the LIKE operator.
  • The % wildcard matches zero or more characters.
  • The _ wildcard matches exactly one character.
  • The text string in the specified value must be enclosed in single quotes.
  • The LIKE operator is supported for string fields only.
  • The LIKE operator performs a case-insensitive match, unlike the case-sensitive matching in SQL.
  • The LIKE operator in SOQL and SOSL supports escaping of special characters % or _.
  • Do not use the backslash character in a search except to escape a special character.
Special Characters % and _

   %-->Specifies that Zero ,One or more characters 
   _ --> Specifies that only One character.

SOQL Query Examples for Like Operator

Example, the following query matches Appleton, Apple, and Bappl , but not Appl:

   SELECT Id, Name FROM Widget__c WHERE Name LIKE 'appl_%'

Example 1:Withou Using Variable

     1.[Select Id,FirstName form User where FirstName LIKE 'srinivas'];

     2.[Select Id,FirstName form User where FirstName LIKE '%srinivas'];

     3.[Select Id,FirstName form User where FirstName LIKE '%srinivas_'];

Example 2:Like Using Variable need to use (:) bind Variavble

    User object contains FirstName as below Records:
     1. srinivasa
     2. msrinivasa
     3. srinivas

   String firstname ='srinivas';

 User u=  [SELECT Id , FirstName,LastName  FROM User where FirstName LIKE : firstName LIMIT 9];

  This query returns Users record from User object where FirstName of user is srinivas only.
  output: srinivas record only number3

Example 2:Using variable with % and _ wildcard characters
  String firstname ='srinivas';
  String str='%'+firstname +'%';
 User u=  [SELECT Id , FirstName,LastName  FROM User where FirstName LIKE : firstName LIMIT 9];

This query will return Users record from User object where FirstName of User contains srinivas
output:It returns Three Records  1. srinivasa 2. msrinivasa 3. srinivas