• han
  • NEWBIE
  • 0 Points
  • Member since 2010

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

Hi,

 

I put together some code from examples on the board and and having problems with the test method. The rest of the code works fine in Sandbox, but still this test method fails.  I keep getting "Unable to retrieve object" on the getcontentasPDF statement.  Any help would be appreciated.

 

Here is my code:

 

public with sharing class testing {
	ApexPages.StandardController controller;
	Private id quoteid;
			
   //Class constructor
    public testing (ApexPages.StandardController c)
     {
        controller = c;
                         
     }     
	
		
	/* This action method will create a PDF of the quote, email the quote and add a record to the
	   Activity History table for later review. */
    public PageReference attach() {
         quoteid = System.currentPageReference().getParameters().get('id');
        
        /* Get the page definition */
        PageReference quotePDF = Page.StandardQuotePDF; 
        
        /* set the quote id on the page definition */
        quotePDF.getParameters().put('id',quoteid);
        
        /* get information from quote */
        Quote myQuote = [Select Quote_Email_Text__c,Quote_File_Name__c FROM Quote where Id = :quoteid];
        
        /* generate the pdf blob */
        Blob pdfBlob = quotePDF.getContentAsPDF();
        
        /* create the attachment against the quote */
        Attachment a = new Attachment();
        a.parentId = quoteid;
        a.name= myQuote.Quote_File_Name__c + '.pdf';
        a.body = pdfBlob;
        
        
        //get name and email address for testing
        User you = [Select Email,Name FROM User WheRE Id = :UserInfo.getUserId() LIMIT 1];
        
        //Create Record in Activity History
        Task myTask = new Task(WhatId = quoteid,
        Status='Completed',
        Subject='Quote Sent by Email',
        Description=myQuote.Quote_Email_Text__c + '\nFile Name='+myQuote.Quote_File_Name__c + '.pdf');
        insert myTask;
        
        
        //Create and attachment
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage (); 
        email.setSaveAsActivity(true);
        email.setToAddresses(new String[]{you.Email});
        email.setSenderDisplayName(you.Name);
        email.setSubject('Pro-face Quotation');
        email.setPlainTextBody(myQuote.Quote_Email_Text__c);
        //email.setHtmlBody('Dear Pro-face America customer,<br /> Your personalized quote is attached. <br />If you need any assistance please email customercare@profaceamerica.com.');
        
      //Create an email attachment
        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
      //Set name of PDF 
        efa.setFileName(myQuote.Quote_File_Name__c+'.pdf'); 
      //Set body of PDF
        efa.setBody(pdfBlob); 
      //Attach the PDF to your email
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
    
    //  Send email 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
        
        /* insert the attachment & return to Opportunity*/
        insert a;
        
        /* send the user back to the quote detail page */
        return controller.view();
    }
    
//******************
//Test Method 
//******************
   public static testMethod void TestattachQuote() {
  

    // Insert test Opportunity
      Opportunity testOppty = new opportunity (
       Name = 'Test Opportunity',
       Amount = 50, 
       StageName = 'Closed Won',
       Annual_Quantity__c = 2,
       CloseDate = System.today());
       insert testOppty;
       
    // Insert test Quote
   	   Quote testQuote = new Quote (
       Name = 'Test Quote2',
       OpportunityId = testOppty.Id,
       //OpportunityId = '0068000000VFfSN',
       SAP_Code__c ='C000000');
       insert testQuote;
       
    // Instantiate VisualForce Page
        PageReference pg = Page.StandardQuotePDF; 
        Test.setCurrentPage(pg); 
        ApexPages.currentPage().getParameters().put('id', testQuote.id);
    
    // Instantiate attachQuote controller
       ApexPages.StandardController c = new ApexPages.standardController(testQuote);
       
       testing qe = new testing(c);
       qe.attach();
    
   }

}