1

I thought this would be simple enough but apparently I can't get it to work since I'm doing something really stupid here.

  1. I have an account named CVB in my test org.
  2. I send an email to Salesforce using my email ID and with just CVB in the body.
  3. The Email is being read fine since the first System.debug statement does show the string as CVB.
  4. What my code fails to do is update the CVB account with the datetime. The issue is with the SOQL query since I'm getting the following error in the log:

    FATAL_ERROR System.QueryException: List has no rows for assignment to SObject

I have tried running my SOQL query separately in the dev console and it runs just fine but the code for some reason isn't being able to grab the Account and is populating an empty List. Any ideas?

global class ProcessTelerikEmail implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail  email, Messaging.InboundEnvelope envelope) {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
        String accName = email.plainTextBody;
        System.debug('Account Name is:' + accName);
        Datetime myDateTime = Datetime.newInstance(2015, 6, 3);
        Account accs =  new Account();
        accs = [SELECT Id, Burn_script_run_time__c, Name from Account WHERE Name= :accName ];
        System.debug('Account name is: ' + accs.Name);
        accs.Burn_script_run_time__c = myDateTime ;
        return result;
    }
} 
  • 1
    A possible reason for this is the context user for the inbound Email Service does not have access to read Accounts (private OWD). Another possible reason is the accName variable value has hidden unprintable characters. Also (tip) - avoid plural variable names (accs) when you declare as a singleton. – cropredy Jun 6 '15 at 0:12
  • OK I verified that the context user is myself and I'm the system administrator with Modify all permission. Also, the email body has nothing but the word CVB. I checked the log which says: 20:19:32:049 USER_DEBUG [9]|DEBUG|Account Name is:CVB so it doesn't seem like there are any unprintable characters. – Mayank Srivastava Jun 6 '15 at 0:20
  • just to be sure, add to the debug log: accName.length(); another tip - for traceability reasons, a good idea is to use a separate user seat for the inbound email context user - that way, when a record is modified, you know what 'event' did the modification (i.e., not you doing data maintenance/data loader) – cropredy Jun 6 '15 at 0:26
  • You were right. For the CVB account name, the length is showing as 4 on the debug log. Any ideas on how I can make that unnecessary/unwanted space go away? I will change the email context user as you suggested. Thanks! – Mayank Srivastava Jun 6 '15 at 0:37
2

As analysed by @crop1645, seems like the email service is counting a white space character as well.. Do something like this. Always use a list to assign values from soql. Avoid using sObjects for that.

List<Account>accs = [SELECT Id, Burn_script_run_time__c, Name from Account WHERE Name= :accName.trim() limit 1];

accs[0].Burn_script_run_time__c = myDateTime ;

This should work for you.

| improve this answer | |
  • this might be a bit snarky but etiquette would suggest at least @mentioning the inspiration for your answer – cropredy Jun 8 '15 at 0:05
  • @crop1645 Actually, I had a similar issue once and this is how I resolved that. But you are right, I will edit the answer and mention your contribution. Sorry about that. – the_phantom Jun 8 '15 at 4:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.