You need to sign in to do that
Don't have an account?

getContent() fails when called from testMethod
Hi All,
I built a VF page with custom controller which calls another VF page that returns a PDF. The PDF is is extracted via getContent(), and attached to an email. It all works fine in the sandbox through the UI, but the test method fails, unfortunately without detailed log information.
I already found these two threads but none really points to an explanation why it fails in test methods while working fine in the UI:
Here is the relevant code from the action method:
public PageReference send(){ for (string address:CCAddresses) { if(!address.endsWith('@symmetricom.com')) address += '.full'; } PageReference pdfPage = Page.quotePDF; pdfPage.getParameters().put('id',theQuote.ID); //pdfPage.setRedirect(true); System.Debug('pdfPage::::::::::::::::::::::::::::::::::::::: ' + pdfPage); transient Blob theBlob; theBlob = pdfPage.getContent(); transient Attachment a = new Attachment ( parentID = theQuote.ID, name = 'SymmQuote ' + theQuote.Name + '.pdf', body = theBlob);
It fails at the line theBlob = pdfPage.getContent(); when called from the test method, otherwise it works fine. Error message is:
System.VisualforceException: List has no rows for assignment to SObject
The quotePDF page uses the same controller and I was wondering if that causes the issue. Maybe the test cannot handle another instance of the same controller? The PageReference for the pdfPage is created correctly
here is the test method:
PageReference pageRef2 = Page.quoteSend; Test.setCurrentPage(pageRef2); ApexPages.currentPage().getParameters().put('id',quote1.Id); quoteLineEdit_v1 editController2 = new quoteLineEdit_v1(); editController2.send();
There are definitely known problems with getContent() in test methods, and for the moment you probably need to either avoid it or surround with try ... catch and ignore the failure.
Hard to know exactly what is wrong in your case, but in general, the problems stem from the fact that getContent makes a second http request which does not have access to any test data (because data created in a test method does not get committed to the database, so will not be found by a separate request).
All Answers
There are definitely known problems with getContent() in test methods, and for the moment you probably need to either avoid it or surround with try ... catch and ignore the failure.
Hard to know exactly what is wrong in your case, but in general, the problems stem from the fact that getContent makes a second http request which does not have access to any test data (because data created in a test method does not get committed to the database, so will not be found by a separate request).
Hi aballard,
your explanation makes sense, kind of what I suspected. I guess we will have to wait for the next release...
I will still have more than 75% code coverage so I am ok with it, just good to know that is is probably not a bug in my code.
I will also talk to support and see if they have more information
Thanks,
Arnt
I am having the same issue and I ended up not callingthe attach method in my test cases.
I also changed the API version to 17.0 and it ignores the error . This looks like a VF / APex bug with getContent()
Same issues here, I actually got "System.VisualforceException: Unable to retrieve object"
Screwing down the api to 16, will work but then I have seen issue that Adobe complains about decoding error and cannot open the PDF.
Bit stuck with this bug, have to use the try... catch blocks. I have filed a case but there is no response..
I had the same issue, and can confirm when I changed my Controller to version 17.0 the error disappeared.
However the Page I am creating my attachment to, shows as an unauthorised, even though the site has access to the page.
Any ideas?
PS: Could you update this with the Case link, if its public?
The case is not public, I logged it via self service.
Anyone with a case on this can ask support to link it to bug W-675904 .
(note this applies specifically to problems with getContent called froma testmethod only)
I was encountering the same troubles and "System.VisualforceException: Unable to retrieve object" error as others. I shelved the Apex class b/c I did not need it to be deployed to production. I came back to it this week to see if Summer '10 may have had an impact and Voila! the code shoots right through with no issues (and I changed nothing). However, when I created a new class and test class to perform the same funcionality for a different object, "System.VisualforceException: Unable to retrieve object" returned.
The long and short is, if you are having this problem, I would play with the versions in your class(es) and page(s). When I took my new Classes and Page and set their versions to be identical to my old, working, ones Voila! again, they worked like a champ.
Which API version did you use and I'm unclear if you changed the matadata for the page or for the unit test class?
Hi ,
Here is consideration that getContent mehod method can't be used in:
My case is similar to above. what to do if i am not getting the value for page.getcontent() in the below. Blob invoiceContent = !Test.isRunningTest() ? invoicePage.getContent() : Blob.ValueOf('dummy text');?
I am not getting the value in getcontent() for few records? please help?