• LiceaSolcom
  • NEWBIE
  • 0 Points
  • Member since 2010

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies
I have an error in generating a test method for an apex class that is called from a button which generates a Visualforce to send an email.
the error is marked by wanting to migrate to production but the percentage of code already covered the mistake that I mark is this:
 

20100208225023.103:Class.ClassSendMail.tryTest: line 81, column 1: returning System.PageReference from method public System.PageReference send() in 4304 ms20100208225023.103:External entry point: returning from end of method static testMethod void tryTest() in 4808 ms

and additionally mark I get this error but not limited to that which marks

 

Maximum heap size: 804735 out of 1000000 ******* CLOSE TO LIMIT 

 

This is the apex code
 

public class sendEmail {public String subject { get; set; }public String body { get; set; }private final Opportunity Opportunity;// Create a constructor that populates the Opportunity objectSelect Opportunity public sendEmail() {Opportunity = [Select o.Deposito_Dental__c, o.Deposito_Dental__r.E_Mail__c, o.Deposito_Dental__r.Name, o.Name from Opportunity o where id = :ApexPages.currentPage().getParameters().get('id')];}public Opportunity getOpportunity() {return Opportunity;}public PageReference send() {// Define the emailMessaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();// Reference the attachment page and pass in the Opportunity IDPageReference pdf = Page.attachmentPDFOpp;pdf.getParameters().put('id',(String)Opportunity.id);pdf.setRedirect(true);// Take the PDF contentBlob b = pdf.getContent();// Create the email attachmentMessaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();efa.setFileName('attachment.pdf');efa.setBody(b);String addresses;if (Opportunity.Deposito_Dental__r.E_Mail__c != null){addresses = Opportunity.Deposito_Dental__r.E_Mail__c;/*/ There may be more Deposito_Dental__r, so loop through the whole listfor (Integer i = 1; i < Opportunity.Deposito_Dental__r.size(); i++){if (Opportunity.Deposito_Dental__r[i].E_Mail__c != null){addresses += ':' + addresses;}}*/}String[] toAddresses = addresses.split(':', 0);// Sets the paramaters of the emailemail.setSubject( subject );email.setToAddresses( toAddresses );email.setPlainTextBody( ' Favor de atender la Orden como se espesifica en el archivo adjunto. ' );email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});// Sends the emailMessaging.SendEmailResult [] r =Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});//return null;PageReference opptyPage = new ApexPages.StandardController(opportunity).view();opptyPage.setRedirect(true);return opptyPage;}}

 

 

This is the page Visualforce
 

<apex:page controller="sendEmail"><apex:messages /><apex:pageBlock title="Enviar mail a el deposito dental {!Opportunity.name} "><p>Se adjuntara en formato PDF el formato de la oportunidad</p><br /><apex:dataTable value="{!Opportunity.Deposito_Dental__r}" var="contact" border="1"><apex:column ><apex:facet name="header">Name</apex:facet>{!contact.Name}</apex:column><apex:column ><apex:facet name="header">Email</apex:facet>{!contact.E_Mail__c}</apex:column></apex:dataTable><apex:form ><br /><br /><apex:outputLabel value="Subject" for="Subject"/>:<br /><apex:inputText value="{!subject}" id="Subject" maxlength="80"/><br /><br /><apex:outputLabel value="Body" for="Body"/>:<br /><apex:inputTextarea value="{!body}" id="Body" rows="10" cols="80"/><br /><br /><br /><apex:commandButton value="Send Email" action="{!send}" /></apex:form></apex:pageBlock></apex:page>

 

 
and this is the test method  

/** * This class contains unit tests for validating the behavior of Apex classes * and triggers. * * Unit tests are class methods that verify whether a particular piece * of code is working properly. Unit test methods take no arguments, * commit no data to the database, and are flagged with the testMethod * keyword in the method definition. * * All test methods in an organization are executed whenever Apex code is deployed * to a production organization to confirm correctness, ensure code * coverage, and prevent regressions. All Apex classes are * required to have at least 75% code coverage in order to be deployed * to a production organization. In addition, all triggers must have some code coverage. * * The @isTest class annotation indicates this class only contains test * methods. Classes defined with the @isTest annotation do not count against * the organization size limit for all Apex scripts. * * See the Apex Language Reference for more information about Testing and Code Coverage. */ @isTestprivate class ClassSendMail{ //Clase de prueba para el trigger que se llama obtenerListaDePreciosPedidosyRech static testMethod void tryTest() { try{ Account testAccount = new Account();Opportunity testOpportunity = new Opportunity ();Contact testContact = new Contact();Deposito_Dental__c testDepDen = new Deposito_Dental__c(); // ··········· Deposito_Dental__c ··············// Select d.E_Mail__c, d.Name, d.Nombre_del_encargado__c, d.Pa_s__c from Deposito_Dental__c dtestDepDen.E_Mail__c = 'usuario@dominio.com';testDepDen.Name = 'prueba';testDepDen.Nombre_del_encargado__c = 'omar';testDepDen.Pa_s__c = 'México';insert testDepDen; // ··········· CUENTA ··············testAccount.Name = 'CuentaTest';testAccount.Apellidos__c = 'Last Cuenta';testAccount.Pais__c = 'México';testAccount.Tipo_de_entidad__c = 'Consultorio particular';insert testAccount; Account IdAccount = [Select Id from Account where id = :testAccount.Id LIMIT 1]; // ··········· CONTACTO - ODONTOLOGO ··············testContact.FirstName = 'test';testContact.LastName = 'last';testContact.Pa_s__c = 'México';testContact.AccountId = IdAccount.Id;testContact.Especialidad__c = 'Ortodoncista';//testContact.Frecuencia_de_visitas__c = ;//testContact.Ranking__c = ;insert testContact; Contact IdContact = [Select Id from Contact where id = :testContact.Id LIMIT 1];// ··········· OPORTUNIDAD ·············· Select o.Id, o.Name, o.StageName, o.Tipo_de_Orden__c from Opportunity otestOpportunity.Name = 'PruebaClassCajasCanceladas';testOpportunity.Tipo_de_Orden__c = 'Promoción';testOpportunity.StageName = 'Por entregar';testOpportunity.CloseDate = System.Today();testOpportunity.AccountId = IdAccount.Id;testOpportunity.Odont_logo__c = IdContact.Id;testOpportunity.Deposito_Dental__c = testDepDen.Id;//testOpportunity.sendEmailPage();insert testOpportunity; Opportunity OppId = [Select Id, Name from Opportunity where Id = :testOpportunity.Id LIMIT 1 ]; //ApexPages.Opportunity sc = new ApexPages.Opportunity(OppId.id);pagereference pg =new pagereference('/apex/send?id='+OppId.Id); system.test.setCurrentpage(pg);sendEmail testsendEmail = new sendEmail();testsendEmail.send();//testsendEmail.body('a'); } catch (DmlException e) { System.debug(e.getMessage()); } }}

 



 thank you very much and I hope I can help.

I have some apex code that has been running since winter '09 and it still works in Winter '10.  It consistently fails in Spring '10 

The apex code that invokes a visualforce page and then invokes a getContent method to retrieve the PDF that is created by the Visualforce page.    It consistently fails in Spring '10 with the following error

System.VisualforceException: core.apexpages.exceptions.ApexPagesGenericException:  

 

Has anyone seen this and have you figured out what to do? Here is a code snippet - it fails on the getContent.  I have not heard of any changes that are in Spring '10 that should cause this to fail.

 

 

PageReference pdfPage = new PageReference(Page.MyPage.getURL() + '?id=' + quoteId); System.debug('URL for attachment ' + pdfPage.getURL()); System.debug('create new attachment'); //create an attachment //Attach as an Attachment Attachment attachment = new Attachment(); attachment.ParentId = quoteId; attachment.name = 'QuoteSent' + System.now()+'.' + extension; attachment.body = pdfPage.getContent();

 

 

 

  • February 08, 2010
  • Like
  • 0