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