+ Start a Discussion
BTRBTR 

Cannot find contact in APEX to be used for email service. Returns Outofbounds.

Yes, I'm new to the Salesforce Developer community. But I'm no dummy when it comes to programming and I'm at the end of my rope here. I've been through this 20 different ways now, using various methods I found online and I just cannot get this thing to work.

 

What I'm trying to do is set up an Email Service that creates a case... aka Custom Email-To-Case.

 

I created a sandbox, so that I could write the apex code. My sandbox was empty so I created one account with a contact matching the email address I'm looking for.

 

There are very, very good reasons for doing this...  but the APEX Class absolutely will not look up my contact. Its agonizing.

 

I have an email, lets say it goes like this

 

APEX TEST

From: test@test.com

 

I am able to read in the test@test.com into a string variable and system.debug can write it out correctly.

 

So I'm looking to get the ID of the contact, and the AccountID of the contact.

 

By all accounts, I should be able to do this to access these variables:

 

Linking by email address in variable (this is what I really want)

List<Contact> contacts = [select id from Contact where email = :FinalEmail];

 Linking by hard-coded email address

List<Contact> contacts = [select id from Contact where email = 'test@test.com'];

 Linking to accountID directly

List<Contact> contacts = [select id, AccountId, from Contact where accountId = '001P000000aYGwf'];

 I've tried the older method...

Contact [] contactArray3 = [Select Id, AccountId from Contact where Email =: ContactEmailFinal limit 1];

I've tried setting it to a string variable ....  and about 15 other things

 

But no matter what I do, Index of 0 is out of bounds, and I cannot use the lookups... It simply does not find the contact, ever.

 

The main result I'm looking for is that I can look up a contact, get the contact ID, and connect that to a case

sCase.ContactId = contacts.Id;

 Please help! I'm in so much pain right now. I just want it to work... Thanks in advance.

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Thomas DvornikThomas Dvornik

How are you running this? The only thing I can think of, is that you are running it from a test. If so, try adding @isTest(SeeAllData=true). But really, you should be creating test data. 

 

If it isn't from a test, you do you see the contact if you execute the query through anonymous apex?

All Answers

Bhawani SharmaBhawani Sharma
Can you try adding without sharing keyword in your class and then:
sCase.ContactId = contacts[0].Id;
BTRBTR

I tried doing this without sharing and it doesn't appear to make any difference. 

 

I'm a system administrator with 'view all data' and 'modify all data' enabled. I created the contacts and accounts for testing.

 

When I try to use contacts[0].id, the test always fails with 'List Index out of Bounds 0' 

 

I've come up with a bit of a test that shows I'm not getting any search results... 

public without sharing class CustomEmailToCase implements Messaging.InboundEmailHandler 

{   

 //Declare a variable
     public String DebugEmail;
	 
	 ...
	 
	 DebugEmail = 'test@test.com';
	
	 //---------------------------------Variables are All Set Up
	 system.debug('Variables area all set up');
	 system.debug('----------------------Begin Contact Finding');

	 system.debug('Debug Email ='+DebugEmail);
	 
	 List<Contact> DebugEmailFinalContact = [select id, accountId from Contact where email = :DebugEmail ];
	 system.debug('Arraysize ='+DebugEmailFinalContact.size());
//Errors system.debug('Contact ID ='+DebugEmailFinalContact[0].id);
	 List<Contact> AllEmailFinalContact = [select id, accountId from Contact];
	 system.debug('Arraysize ='+AllEmailFinalContact.size());
	 
	 List<Contact> IdContact = [select id from Contact];
	 system.debug('Arraysize ='+IdContact.size());
	 
	 List<List<SObject>> searchList = [FIND '*test*' IN ALL FIELDS RETURNING Account (Id, Name), Contact];
	 Contact [] searchlistcontacts = ((List<Contact>)searchList[1]);
	 system.debug('ContactArraysize ='+searchlistcontacts.size());
	 
	 Account [] searchlistaccounts = ((List<Account>)searchList[0]);
	 system.debug('AccountArraysize ='+searchlistaccounts.size());
	 
	 Account [] NameAcct = [SELECT Name FROM Account];
	 system.debug('NameAcct ='+NameAcct.size());

     system.debug('----------------------------End Contact Finding');

  My Debug log shows all array sizes to be zero.

 

It seems like it could be a sharing issue, based on a discussion I saw on the forums, but I went into the sharing settings and everything is set up to public read write which is overridden by my permission set with view/modify all data. It seems setting the without sharing would have fixed that as well. 

 

I'm totally stumped! 

 

 

 

 

Avidev9Avidev9
Can you try removing the where clause ?
Try to debug the result from this query

List<Contact> DebugEmailFinalContact = [select id, accountId,Email from Contact ];
BTRBTR

Well I think I've found something... 

 

In the developer console, I changed my perspective manager to all. This provided me more information and capabilites when I look at my debugging log. 

 

I used the query editor to determine that my queries were working. I was able to find my contacts using the query editor. 

 

In my debug log, what I'm seeing is that in the upper right of the screen, there is a box called 'source' 

 

The source box is telling me that "source for apexTestHandler could not be loaded"

 

I'm certain this is the problem, but searching the internet has produced nothing...  

 

Any ideas on how to fix that ??? 

 

 

Thomas DvornikThomas Dvornik

How are you running this? The only thing I can think of, is that you are running it from a test. If so, try adding @isTest(SeeAllData=true). But really, you should be creating test data. 

 

If it isn't from a test, you do you see the contact if you execute the query through anonymous apex?

This was selected as the best answer
BTRBTR

Thank You!!!!!!  That worked! I can't believe it! 

 

adding the (SeeAllData=True) to the @IsTest line fixed it.  Now my searches appear to be functioning correctly! 

 

man I owe you a case of beer! I think you just saved my sanity. I was really not getting why the select function didin't work the same way it did in hundreds of sample codes!