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
Rick-ProfaceRick-Proface 

Help with test method. Unable to retrieve object

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();
    
   }

}

 

 

 

Rick-ProfaceRick-Proface

Thanks for the response. I looked through the code and I'm not sure what to look for. Maybe I need to understand what happens when you insert a test record. Is that record available for my Visual Force page to access ( the one I'm trying to get the PDF contents of) ?

 

Thanks,Rick

Going South.ax738Going South.ax738

Yes. ofcouse, when you insert a record, that record would be available for further processing with test case. As we all do test cases on test sandbox and move the classes to production, the records in test is not the same as production and when you hard-reference test case records, they fail when we attempt to move saying invalid reference.Its a good practice to insert all 'master' records for foreign keys that you need for test cases before hand within test case to be used for other inserts/updates. All inserts or updates you do on test methods has no effect on real data be it sandbox or production.

 

In that example test method, the attachment test method is given as a second test method and suggests to do the same for your test method also instead of writing it within.

 

Rick-ProfaceRick-Proface

Can you elaborate on your stement below please? I'm not sure why I need a second test method...

 

Thanks, Rick

 

"In that example test method, the attachment test method is given as a second test method and suggests to do the same for your test method also instead of writing it within."

Going South.ax738Going South.ax738

Seems like many developers are having a problem on getContentAsPDF() as its an un-documented function.  It seems that this function does not seem to work as expected, hence suggested the way the other thread have given.

 

Yes, you can combine both test methods as one too as long as you get the pagereference to recenlty created quote record and assign the blob. [X]

 

Another alternative & intersting notes.

 

http://community.salesforce.com/t5/Apex-Code-Development/PDF-Generation-Behavior/m-p/120245

 

An idea exchange entry:

http://sites.force.com/ideaexchange/ideaView?c=09a30000000D9xt&id=08730000000HzknAAC

 

Honestly, I am not sure on outcome either. ;(

hanhan

Not sure if this helps, but this is what I did and it worked fine with the test method:

 

    Blob b;
    try {
      b = pdf.getContent();
    } 
    catch ( VisualforceException ex ) {
      String myString = 'dummy'; 
      b = Blob.valueof(myString);  // to pass test coverage
    }

I'm simply catching the exception so the test method can move on without stopping.

 

Cheers

Rick-ProfaceRick-Proface

Thanks for replying, but I still could not get it to work as I am using the getContentAsPDF() method. If I didn't use that I would get another error even though on my page I use renderas=" PDF"

 

Still frustrated....

 

 

Rick