function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
teknicsandteknicsand 

Debugging Apex Test Class

Hi guys...

 

I need some help debugging this issue. I have an email apex class that I'm trying to test. 

 

 

global class sendPDFEmailClass
{

public string sendmy1Email(Opportunity opp, Blob pdf1, Blob pdf2, blob pdf3, String pb_id, String bc_email)
{

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
User user = [select id, username, Profile.Name, email from User where id = :Userinfo.getUserId()];
String[] toAddresses = new String[] {opp.Mindstreams_Account_Owner_Email__c, user.email, bc_email};
system.debug('user email' + user.email);

mail.setToAddresses(toAddresses);
mail.setReplyTo(user.Email);//sf user email add
mail.setSenderDisplayName(User.username);//sf user name

String subject1 = 'Purchase Details';
mail.setSubject(subject2);
mail.setBccSender(true);
mail.setUseSignature(true);
/Choosing approporiate email content(store links) based on pricebook name
String std_txt = 'W1';
String std__txt = 'W2';

if (pb_id == '01s30000000FgDWAA0')//01sR00000008ixAIAQ 01s30000000FgDWAA0
{
mail.setPlainTextBody(std_plain_txt);
mail.setHtmlBody(std_html_txt);
}

if (pb_id == '01s30000000FkWUAA0')//01sR00000008ixBIAQ
{
mail.setPlainTextBody(w2);
mail.setHtmlBody(w2);
}

//Attachments
try{
Messaging.EmailFileAttachment mailAttachment;
mailAttachment = new Messaging.EmailFileAttachment();
mailAttachment.setFileName('Order Items.pdf');
mailAttachment.setBody(pdf1);

Messaging.EmailFileAttachment mailAttachment1;
mailAttachment1 = new Messaging.EmailFileAttachment();
mailAttachment1.setFileName('Israeli Direct Bank Transfer.doc');
mailAttachment1.setBody(pdf2);

Messaging.EmailFileAttachment mailAttachment2;
mailAttachment2 = new Messaging.EmailFileAttachment();
mailAttachment2.setFileName('Mindstreams® Ongoing Billing options.pdf');
mailAttachment2.setBody(pdf3);

mail.setFileAttachments(new Messaging.EmailFileAttachment[] { mailAttachment, mailAttachment1, mailAttachment2});
}
catch (Exception e) {system.debug('error attaching files to the email' + e); return ('Email Not Sent because documents failed to attach');}

//Send email results
List<Messaging.SendEmailResult> results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
if (!results.get(0).isSuccess())
{
System.StatusCode statusCode = results.get(0).getErrors()[0].getStatusCode();
String errorMessage = results.get(0).getErrors()[0].getMessage();
system.Debug('email error:' + errorMessage);
ID d = results.get(0).getErrors()[0].getTargetObjectId();
return ('Email Not Sent: Check debug logs for error message or contact System Administrator');
}
else
{
if (opp.email_counter__c == null) {opp.email_counter__c = 0;}
opp.email_counter__c++;

update opp;
return ('Email Sent');

}
}

//US Email
public string sendmyEmail(Opportunity opp, Blob pdf, Blob pdf2, String pb_id, String bc_email)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

//String[] toAddresses = new String[] {'sandeep.kelvadi@neurotrax.com'};

User user1 = [select id, username, Profile.Name, email from User where id = :Userinfo.getUserId()];
User opp_owner = [select id, username, Profile.Name, email from User where id = :opp.OwnerId];
String[] toAddresses = new String[] {opp.Mindstreams_Account_Owner_Email__c,bc_email, opp.OwnerId, user1.Email};
system.debug('user email' + user1.email);
system.debug('opportunity owner email' + opp_owner.email);
mail.setToAddresses(toAddresses);

mail.setReplyTo(opp_owner.Email);//opportunity owner email add
mail.setSenderDisplayName(opp_owner.username);//opportunity owner user name

String subject2 = 'New Account Purchase Details';
mail.setSubject(subject2);
mail.setBccSender(true);
mail.setUseSignature(true);

//store Paypal links
String store_link;
system.debug('pb_id parameter passed from myopportunitycontroller: ' + pb_id);


String TP1_US_plain_txt='W1.';
String TP1_US_html='W2';

if (pb_id == '01s30000000FkSlAAK')//01s30000000FkSlAAK;01sR00000008ix5IAA
{
store_link = 'https://www';
system.debug('store link :' + store_link);
mail.setPlainTextBody(std_US_plain_txt);
mail.setHtmlBody(std_US_html);
system.debug('std_US_html');
}

else if (pb_id == '01s30000000FkHcAAK')//01sR00000008ix6IAA
{
store_link = 'https:';
system.debug('store link :' + store_link);
mail.setPlainTextBody(disc1_US_plain_txt);
mail.setHtmlBody(disc1_US_html);
system.debug('disc1_US_html');
}

else
{
system.debug('Email not Sent. Pricebook match not found' + pb_id);
return('Email Not Sent: Pricebook match not found');
}


//Attachments

try{
Messaging.EmailFileAttachment mailAttachment;
mailAttachment = new Messaging.EmailFileAttachment();
mailAttachment.setFileName('Mindstreams® Invoice.pdf');
mailAttachment.setBody(pdf);


Messaging.EmailFileAttachment mailAttachment1;
mailAttachment1 = new Messaging.EmailFileAttachment();
mailAttachment1.setFileName('Mindstreams® Ongoing Billing options.pdf');
mailAttachment1.setBody(pdf2);
mail.setFileAttachments(new Messaging.EmailFileAttachment[] { mailAttachment, mailAttachment1});
}
catch (Exception e) {system.debug('error attaching files to the email' + e); return ('Email Not Sent! Documents failed to attach');}

List<Messaging.SendEmailResult> results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
if (!results.get(0).isSuccess())
{
System.StatusCode statusCode = results.get(0).getErrors()[0].getStatusCode();
String errorMessage = results.get(0).getErrors()[0].getMessage();
system.Debug('email error:' + errorMessage);
ID d = results.get(0).getErrors()[0].getTargetObjectId();
return ('Email Not Sent: Check debug logs for error message or contact System Administrator');
}
else
{

if (opp.email_counter__c == null) {opp.email_counter__c = 0;}
opp.email_counter__c++;
try{
update opp;
return ('Email Sent');
}
catch (Exception e) { system.debug('update failed'); return ('Email not sent. Email Counter update failed.');}
}
}
}

 There are 2 functions each meant to send out an email for a specific target of customers. In my case, I'm trying to test, for now, for the second function. The following lines of code in the 2nd function is where I'm hitting the brick wall:


User opp_owner = [select id, username, Profile.Name, email from User where id = :opp.OwnerId];

 

My rudimentary test class looks like this

 

 

@isTest

private class sendPDFEmailTestClass {

public static testMethod void sendmyEmailTest()
{
//Data, variables etc
Blob pdf1, pdf2, pdf3;

Opportunity testopp = new Opportunity (name='test opp');
testopp.StageName = 'Closed Won';
testopp.CloseDate = date.today();
insert testopp;

system.debug('testopp stage: ' + testopp.StageName);
system.debug('testopp name: ' + testopp.Name);
system.debug('testopp ownerId: ' + testopp.OwnerId);
PageReference opportunityinvoice = Page.opportunityinvoice;
opportunityinvoice.getParameters().put('id',testopp.id);
pdf1 = opportunityinvoice.getContent();
pdf2 = opportunityinvoice.getContent();
pdf3=opportunityinvoice.getContent();


//testing getpricebook2()
Product2 testpd = new Product2 (name='test product');
testpd.productcode = 'test pd code';
insert testpd;
Pricebook2 s = [select id, name from Pricebook2 where IsStandard = true];
system.debug('test price book name: ' + s.name);
PricebookEntry testpbe = new PricebookEntry ();
testpbe.pricebook2id = s.id;
testpbe.product2id = testpd.id;
testpbe.UnitPrice = 100;
testpbe.IsActive=true;
insert testpbe;

testopp.Pricebook2Id = s.Id;
update testopp;

String bc_email = 'test@test.com';
String pb_id = s.Id;

Opportunity queryopp = [select stageName, name, pricebook2id, ownerid from Opportunity where id =: testopp.Id];
system.debug(queryopp.StageName);
system.debug(queryopp.Pricebook2Id);
system.debug(queryopp.OwnerId);


// testopp.OwnerId
//user & profile testing
Profile p = [select id from profile where name='Standard User'];
User u = new User(alias = 'standt', email = 'stduser@test.com', emailencodingkey='UTF-8', lastname='Testin', languagelocalekey='en_US', localesidkey='en_US', profileid=p.Id, timezonesidkey='America/Los_Angeles', username='stduser@test.com');



User opp_owner = [select id, username, Profile.Name, email from User where id = :testopp.OwnerId];

sendPDFEmailClass testemail = new sendPDFEmailClass();
testemail.sendmyEmail(testopp, pdf1, pdf2, pb_id, bc_email);
// testemail.sendmyEmail1(opp, pdf1, pdf2, pdf3, pb_id, bc_email)

}

}

 I get the error that list has no rows for assignment for this linein the Apex Class

  User opp_owner = [select id, username, Profile.Name, email from User where id = :opp.OwnerId]

 

 

 

 I tried assigning the opportunity owner with the user created in the test class, but I got a cross reference error. 

 

Can someone help me here? Really need to get this fixed. It's not allowing me to create any code code since the apex coverage is below 75%.

 

Thanks in advance!

 

Message Edited by teknicsand on 08-26-2009 07:26 AM
Message Edited by teknicsand on 08-26-2009 07:28 AM
sfdccoder1sfdccoder1

You wrote " I tried assigning the opportunity owner with the user created in the test class but I got a cross reference error.".

I'm not sure why you're getting the error message but i would try in this case to assign a "real" user i.e. select the first user record in the User table.

Unlike other test methods that should reference only records created in the test class, you can always count on at least one real user record.