0

Apex--

global class AvailableTrainers implements Messaging.InboundEmailHandler {    
    global   Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) 
    {
        System.debug('value in email'+email);
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
        if(email.subject != '')
        {
            //string str = email.subject.replace('KH Trainer Availability check for ','');
            list<string> lststr = email.subject.split(' ');
            system.debug('--'+lststr);
            string Oppno = lststr[lststr.size()-1];
            Courses__c objopp = [select id,name,Available_Trainers__c, Email_Subject__c, Email_Body__c, FromAddress__c,ToAddress__c from Courses__c where Name=:Oppno];
            system.debug('-objopp-'+objopp);
            
            if(!email.plainTextBody.contains('Not Available'))
            {
                if(objopp.Available_Trainers__c =='' && objopp.Available_Trainers__c ==null && email.plainTextBody.containsIgnoreCase('Yes Available'))
                {
                    objopp.Available_Trainers__c = email.fromAddress;
                    objopp.ToAddress__c = string.valueOf(email.toAddresses);
                    objopp.FromAddress__c = email.fromAddress;
                    objopp.Email_Subject__c = email.subject;
                    objopp.Email_Body__c = email.plainTextBody;
                    system.debug('-email-'+objopp);
                }else if(email.plainTextBody.containsIgnoreCase('Yes Available'))
                {
                    objopp.Available_Trainers__c = objopp.Available_Trainers__c+','+email.fromAddress;
                    objopp.ToAddress__c = objopp.ToAddress__c+','+email.toAddresses;
                    objopp.FromAddress__c = objopp.FromAddress__c+','+email.fromAddress;
                    objopp.Email_Subject__c = objopp.Email_Subject__c+','+email.subject;
                    objopp.Email_Body__c = objopp.Email_Body__c+','+email.plainTextBody;

                   
                }
                system.debug('-objopp-'+objopp);
            }
            update objopp;
                    System.debug('value in email'+objopp);

        }
        return result;
    }
}

test class is

@isTest
private class AvailableTrainersTest {
     @isTest
    static void testHandleInboundEmail() {
        Messaging.InboundEmail email = new Messaging.InboundEmail();
        email.subject = 'KH Trainer Availability check for Opportunity1';
        email.plainTextBody = 'Yes Available';
        email.fromAddress = '[email protected]';
        email.toAddresses = new String[] {'[email protected]'};
       
        Messaging.InboundEnvelope envelope = new Messaging.InboundEnvelope();
        
      
      // Act
        AvailableTrainers at = new AvailableTrainers();
        Messaging.InboundEmailResult result = at.handleInboundEmail(email, envelope);

       
        
        Courses__c course = [SELECT Id, Available_Trainers__c, Email_Subject__c, Email_Body__c, FromAddress__c, ToAddress__c FROM Courses__c WHERE Name = 'Opportunity1'];
        System.assertEquals('[email protected]', course.Available_Trainers__c);
        System.assertEquals('[email protected]', course.FromAddress__c);
        System.assertEquals('[email protected]', course.ToAddress__c);
        System.assertEquals('KH Trainer Availability check for Opportunity1', course.Email_Subject__c);
        System.assertEquals('Yes Available', course.Email_Body__c);
    }
}

error is ---

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

Class.AvailableTrainers.handleInboundEmail: line 12, column 1 Class.AvailableTrainersTest.testHandleInboundEmail: line 17, column 1

5
  • You are assigning an empty list to a sobject variable. Somewhere you have Sobject a = []; and the list is empty. This can be used only in case the list have only one member.
    – Damecek
    Commented Feb 2, 2023 at 12:40
  • please give me example code line Commented Feb 2, 2023 at 12:42
  • Case c = [ select id from case];
    – Damecek
    Commented Feb 2, 2023 at 12:45
  • Courses__c objopp = [select id,name,Available_Trainers__c, Email_Subject__c, Email_Body__c, FromAddress__c,ToAddress__c from Courses__c where Name=:Oppno]; --- its possible Commented Feb 2, 2023 at 12:54
  • 1
    you need to create data for your test scenatios: use @TestSetup annotation
    – McCubo
    Commented Feb 2, 2023 at 13:21

1 Answer 1

1

As the error messages states you are trying to assign empty list to a single SObject variable. The issue is on the following line:

Courses__c objopp = [select id,name,Available_Trainers__c, Email_Subject__c, Email_Body__c, FromAddress__c,ToAddress__c from Courses__c where Name=:Oppno];

If you cannot guarantee that the list will return one record you should use rather List<Courses__c> instead.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .