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
admintrmpadmintrmp 

Unable to retrieve object - PDF Email (Not testing)

Hi, I'm getting the error returned in an exception. "Unable to retrieve object"

I get this when I am trying to attach a PDF To an email.

 

  • The PDF works perfectly fine.
  • The page and code are both set to API 21.0.
  • The ID of the PDF is correctly being set and is valid.
  • I am trying to send this email via a site guest profile.
  • It has worked before... but only twice.
  • I am not running this through a test (I know how to fix that one!).

 

Here is where it's falling over:

 

PageReference pdf = Page.ShowInvoice;
pdf.getParameters().put('id', (String)invoice.Id);
Blob pdfContent = pdf.getContent();

 

Any ideas?

 

joshbirkjoshbirk

When it worked twice before, does that mean it now doesn't work with zero changes? Or was it on a different org or something? If it has mysteriously stopped working, I might jump to the part where we recommend opening a support ticket.

 

If you go directly to the ShowInvoice VF page with a valid ID, does the PDF download correctly there ... or is that where the error is getting formed?

NK123NK123

Added this line:before getContent()

 

pdf.setRedirect(

~NK

true);

NK123NK123

Correction:

Add this line:

 

  pdf.setRedirect(

true); 

admintrmpadmintrmp

Actually I had setRedirect() within the code but accidently removed it. It still doesn't work with it in there.

 

When I use a static ID to pull the details for my invoice, it works absolutely fine! I tested the dynamic method of pulling the invoice and it's worked absolutely fine. I'm am somewhat confused by this.

admintrmpadmintrmp

I think I know the issue but I need to find a work-around for this (hopefully without using a scheduled task, but if needs be, I will do it).

 

The invoice is being created within the same method as the email being sent. I'm wandering if the email is failing to find the invoice because of how new the record is.

 

Is there a way around this?

NK123NK123

Hi,

I don't know how the pdf is working in your case, but for me, I am using the PDF output and sending e-mail out which is similar to your case.

 

  // Define the email 
  Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

  // Reference the attachment page and pass in the account ID

  PageReference pdf =  Page.ViewQuote;
  pdf.getParameters().put('id',(String)quote.id);
  pdf.setRedirect(true);

  // Take the PDF content
  Blob pdfBlob = pdf.getContent();
       
  // Create the email attachment
  Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
  strAttName = 'test.pdf';
  efa.setFileName(strAttName);
  efa.setBody(pdfBlob);


  // Sets the paramaters of the email 
  String subject='subject1';   
  String body = 'body1';

  email.setSubject( subject );
  email.setToAddresses( 'test@t.com');
  email.setPlainTextBody( body );

  email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

  // Sends the email 
  Messaging.SendEmailResult [] r =
   Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});  
  
  

~NK

trustchristrustchris

Hey,

 

I have just run into the same problem.  I was going to put my sendemail method  in a seperate class and use @future, but you can't use it with the getcontent() method. d'oh!

 

I'm inserting a lead, and then calling my send email method and passing the lead id so that it can generate my pdf.  Seems as though the query is coming back empty as get the unable to retrieve object error.

 

Am wondering If I can instantiate a controller for my pdf and pass if the lead object, rather than rely on a query?

 

Did you ever find a solution to this??  Scratching my head at the moment, will let you know if I find anything.

 

Cheers

 

Chris

admintrmpadmintrmp

Hi trustchris.

 

I've got other priorities, so I've left this one for a bit but I am still very interested in finding a solution for this as I will need it at some point or another.

 

If I ever find a solution or vice versa I'll be happy to share the advice. It might be worth getting in contact with support on this. They will normally answer quite quickly if you mention that it's a bug.

 

Can I ask, what profile are you using to send your emails with, and are you using a customer portal/sites?

 

Cheers

Luke

trustchristrustchris

Hi Luke

 

I've run across this problem before, where a recently inserted object isn't immediately available in a query, especially when using sites.  But this is the first time experiencing a similar issue as an sf user.

 

I have been testing using a sys admin profile, but it will be used in sites with an un-authenticated guest user eventually.

 

Had someone mention serializing the object into XML/json and passing it across to the page that way.  So that may be an option.

 

Will let you know how I get on!

 

Cheers

 

admintrmpadmintrmp

I can't use JSON, since mine is being run through a HTTP Request from a Google Checkout API server. The basic idea is to create an invoice record and send it all within a single method from the last request that's sent by Google.

 

This means, I have to find a solution that is server-side only. As for any other time, I send the invoices out seperately to the creation of the record.

 

The solution for me would be to create a scheduled task, but I really do not want to make my app so that it's heavily relied on schedules (due to governor limits).

 

I hope you understand what I'm saying there.

trustchristrustchris

Yeah, I think I get what you're saying... This is more what I was thinking;

 

 

PageReference pdf = Page.ShowInvoice;
//don't pass the id of the invoice object
//pdf.getParameters().put('id', (String)invoice.Id);
//instead add a header that contains json and let the pdf controller deserialize.
pdf.getHeaders.put('X-Json', 'insert json here!');
Blob pdfContent = pdf.getContent();

//will need a method to turn invoice into json/xml

 

 

 

My idea is to add a json header to the pageRef object called by the getContentMethod(), that sends over a serialized sObject.  that way my PDF page controller can do it's magic and use the sObject info from the header.

 

Guess will only work with a limited set of sObject values.

 

Looking through the docs, Scheduled apex wouldn't be an option, as you can't call getContent() methods from there!  A time based trigger wouldn't work either.

 

Gonna pass this of to SF Support as well, as the object should be available for the controller.

 

Cheers

 

Chris

 

 

admintrmpadmintrmp

Ah, I see. I haven't gone into wide detail with JSON.

 

Did you have any luck on seeing if this worked? Or do you mean Salesforce needs to include a method to allow this to work?

trustchristrustchris

No Luck Yet,

 

Ditched the json idea for the moment, and am just going to try and pass the information in the headers.  Can't seem to get it to work though.

 

Will give it another crack tomorrow.  Also lodged a ticked with support, so we'll see if they can help us out!

 

Cheers

 

Chris

admintrmpadmintrmp

Okay. Hope we get some answer from this! :o)

 

I will soon be coming up to the time when I have to sort this out (within the next week). If I find any answers, I'll pop 'em through.

trustchristrustchris

Just a quick update,

 

Salesforce have access to my code and are trying to figure out what's going on.

 

They've had access for a couple of days and just emailed me to say they are still looking into the issue,

 

I'll let you know when I find out more.

 

Cheers

 

Chris

admintrmpadmintrmp

Hi, did you ever figure out what the issue was here?

 

Thanks

vishal@forcevishal@force

I'm facing this same issue. Any updates here?

Clement DubeClement Dube
Just wondering if you were able to fix this?

I am having the same issue.

Basically my page goes from page 1 to a page 2, and the pdf should be generated automatically when move into page 2, the first time, it doesnt generate it and i get an 
system.security.NoDataFoundException: Unable to retrieve objectClass.

the bizard part is that if from page 2 I go back to page 1 and then back to page 2 the pdf gets  generated