• ACPreveaux
  • NEWBIE
  • 5 Points
  • Member since 2014
  • Salesforce Admin
  • Cadillac Jack

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 7
    Replies
I'm trying to test an email handler which updates a custom object in my org.  It works successfully in practice, but the test case throws an exception "System.NullPointerException: Argument cannot be null." when saving.  The PlainTextBody in the test case is copied verbatim from a successful email (with the exception that new lines are substituted with "\n" in test code).
Class Code:
global class SF_Title implements Messaging.InboundEmailHandler {
	global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
		Messaging.InboundEmailResult result = New Messaging.InboundEmailResult();
/*Insert email handling code here*/
		//Here we want to add a new title to the 'Titles' Object
		//Create the new title record
		Title__c newTheme = new Title__c();
		//and define a map variable to store the fields we want to include in the insert
		map<string,string> fields = new map<string,string>();
		/*Here I split the email body by newLine and iterate over those lines to create key/value pairs
		 *which then get split and inserted into a map for calling during the field assignment phase.
		 */
		for (string record: email.plainTextBody.split('\n',0)) {
			try{ 
				list<string> KeyValue = record.split(': ',0);
				String Key = KeyValue[0];
				String Value = KeyValue[1];
				fields.put(Key,Value);
				
			} catch(exception e) {
				system.debug(e);//pass if no value associated with key
			}
		}
		
		system.debug(email.plainTextBody);
		
		system.debug(fields);
		//Field assignment phase
		newTheme.OwnerId = '005C0000003fVL3IAM'; //User ID for Allen Preveaux
		newTheme.Name				=fields.get('Title'); //type: Text(80)
		try {
			newTheme.Perc_Payback86__c	= Decimal.valueOf(fields.get('86%')); //type: Percent(2,2)
		} catch(exception e) {
		}
		try{
			newTheme.Perc_Payback87__c	=Decimal.valueOf(fields.get('87%')); //type: Percent(2,2)
		} catch(exception e) {
		}
		try{
			newTheme.Perc_Payback88__c	=Decimal.valueOf(fields.get('88%')); //type: Percent(2,2)
		} catch(exception e) {
		}
		try{
			newTheme.Perc_Payback89__c	=Decimal.valueOf(fields.get('89%')); //type: Percent(2,2)
		} catch(exception e) {
		}
		try{
			newTheme.Perc_Payback90__c	=Decimal.valueOf(fields.get('90%')); //type: Percent(2,2)
		} catch(exception e) {
		}
		try{
			newTheme.Perc_Payback92__c	=Decimal.valueOf(fields.get('92%')); //type: Percent(2,2)
		} catch(exception e) {
		}
		try { 
			newTheme.Perc_Payback94__c	=Decimal.valueOf(fields.get('94%')); //type: Percent(2,2)
		} catch(exception e) {
		}
		try{ 
			newTheme.Perc_Payback95__c	=Decimal.valueOf(fields.get('95%')); //type: Percent(2,2)
		} catch(exception e) {
		}
		try{ 
			newTheme.Class__c 			=fields.get('Class'); //type: Picklist
		} catch(exception e) {
		}
		try{ 
			newTheme.Cover_Cost__c		=fields.get('Lines'); //type: Picklist
		} catch(exception e) {
		}
		try{
			newTheme.Force_Bet__c		=Boolean.valueOf(fields.get('Force Bet')); //type: Checkbox
		} catch(exception e) {
		}
		try{
			newTheme.Legal_Config__c	=fields.get('Legal Configuration'); //type: text(4)
		} catch(exception e) {
		}
		try{
			newTheme.MLPs__c			=Boolean.valueOf(fields.get('MLPs')); //type: Checkbox
		} catch(exception e) {
		}
		try{
			newTheme.Per_Diem__c		=Decimal.valueOf(fields.get('Per Diem')); //type: Number(3,2)
		} catch(exception e) {
		}
		try{
			newTheme.Product_Type__c	=fields.get('Product Type'); //type: Picklist
		} catch(exception e) {
		}
		try{
			newTheme.ProgId__c			=fields.get('ProgID'); //type: Text(15)
		} catch(exception e) {
		}
		try{
			newTheme.Skin_Family__c		=fields.get('Skin Family'); //type: Text(40)
		} catch(exception e) {
		}
		try{
			newTheme.Software_Req__c	=fields.get('Software Required'); //type: Text(40)
		} catch(exception e) {
		}
		try{
			newTheme.MaxBet__c			=Decimal.valueOf(fields.get('Typical Max Bet')); //type:  Currency(3,2)
		} catch(exception e) {
		}
		try{
			newTheme.VidCard__c			=Boolean.valueOf(fields.get('Video Card Required')); //Type: Checkbox
		} catch(exception e) {
		} 
		//perform the insert, and there you have it
		system.debug(newTheme);  //testing command.  Comment/Remove upon official deployment
		try {
			insert newTheme;
		} catch (DmlException e) {
			System.debug('The following error has occurred: '+e.getMessage());
		}
/*End of cool ideas*/
		return result;
	}
}
Test Code:
@istest
public without sharing class Test_SF_Title {
	static testmethod void test_email_title_updater(){
		Messaging.InboundEmail emailAllFields = new Messaging.InboundEmail();
		Messaging.InboundEnvelope envelope = new Messaging.InboundEnvelope();
		
		emailAllFields.plainTextBody = 'Title: Test Title Update 1 \n 86%: 86.05 \n 87%: 87.07 \n 88%: 88.04 \n 89%: 88.97 \n 90%: 89.99 \n 92%: 92.01 \n 94%: 93.97 \n 95%: 95.02 \n Class: 2 \n Lines: 50 \n Force Bet: true \n Legal Configuration: 8378 \n MLPs: True \n Per Diem: 25 \n Product Type:  Core \n ProgID: 117 \n Skin Family: White Buffalo \n Software Required: V14.2.1_C3/r74835 \n Typical Max Bet: 5.00 \n Video Card Required:  True';
		envelope.fromAddress = 'testUser@playags.com';
		SF_Title title_update_tester= new SF_Title();//now I need to include an instance of the SF Title handler and process the above
		title_update_tester.handleInboundEmail(emailAllFields,envelope);
		
		
		//also needed is a version with all exceptions and we need to process that one too to test all of the catch portions of the code. 
	}
}

Test class complains about the NullPointerException on the line where I provide 'title_update_tester.handleInboundEmail(emailAllFields,envelope);'.

I've tried eliminating the spaces between the fields and '\n's, adding fields to the emailAllFields parameter, and moving the fromAddress from envelope to emailAllFields.

hello All,

 

Here I'm again with some un-coverage issue for Apex In-BoundEmailService class in sfdc.

 

Here is myInbound emailservice 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;
             
   }
   }

Thisis the Test Class which is creating no error but it's not covering all test conditions.

 

//Test Method for main class
   
   static testMethod void TestinBoundEmail()
   {
     // 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 = 'Test Job Applicant';
  email.fromAddress = 'someaddress@email.com';
  
  // 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();
  attachment.body = blob.valueOf('my attachment text');
  attachment.fileName = 'textfiletwo.txt';
  attachment.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);
    
   // create a contact data
   
    Contact testContact= new Contact();
    testContact.Email='someaddress@email.com';
    testContact.LastName='lastname';
    testContact.Phone='1234567234';
    testContact.Title='hello';
    insert testContact;
    
    system.debug('insertedcontact id===>' +testContact.Id);
   
   // Create Attachmenat data
   
  Attachment attachmnt =new Attachment();
  attachmnt.name='textfileone.txt';
  attachmnt.body =blob.valueOf('my attachment text');
  attachmnt.ParentId =testContact.Id;
  insert  attachmnt ;
   
   
   }
   

 

Could any one please make me correct in this case.

 

I tried to put all conditions and test data still it's creating problem.

 

Thanks for your valuable suggestions.

 

Thanks & regards,

jaanVivek

This is something that astounds me. Can someonn tell me if there is a way around?

Requirement: Opportunity Stage is set depending on bunch of 9 other picklist values (basically the rep picks the things that are done, and this in turn feeds/computes the stage).

Problem: Stage is a required field. Record cannot be saved without a value in it. Workflow field update and before triggers do not work.

Simple workaround: Set the default value of the stage on creation to the first stage. BUT this is not possible as stage default value cannot be set!!!!!Only scenario where an opportunity’s stage can be set by default is when doing a lead conversion. 

This is just unbelievable. Help!

PS: I have multiple record types for multple sales process.

Message Edited by witoutme247 on 01-13-2010 04:21 AM
When prompted by the apex data loader I enter my salesforce.com username and password, but this message keeps appearing:

Error logging in to Salesforce. Please check your username and password.

When I login to salesforce.com with the same username and password it works.

Does anyone know why I can't login to the data loader?


Message Edited by Lkc037 on 12-11-2007 11:49 AM
  • December 11, 2007
  • Like
  • 0