I'm trying to create a page in memory so that I can get the content. The problem I'm having is that the code expects there to be a record (case) associated with it but I need to provide the URL to a VisualForce page and some other parameters as well. If I set the URL in the constructor of the PageReference, it doesn't resolve the record. How do I both get the record associated and set the URL to a VisualForce page?

String strUrl = '/apex/MyApexPage?id=' + myCase.Id + '&parameter=' + myParm;
PageReference pr = new PageReference( strUrl );
Attachment att = new Attachment( Name = 'myAttachment' );
att.Body = pr.getContent();

I then end up with an exception that there are no rows returned for the page I'm trying to get because in its code, it looks something like:

What do I need to do to get this all to work?

Case theCase = stdContrller.getRecrod();
Case myCase = [SELECT Id, CaseNumber, CreatedDate, CreatedById FROM Case WHERE Id = :theCase.Id];
  • It looks like part of your question has been lost. Can you review and add to it? – Keith C Nov 21 '13 at 6:00
  • No. Nothing is lost. When I create the PageReference using the URL to my VisualForce page, the record doesn't get resolved in the standard controller. Thus the second code snippet fails because theCase.Id is null and throws an exception that no rows were returned. If I build the PageReference using a standard controller instance with a object, I don't see how to set the URL so it lands on my VisualForce page. That is where I'm stuck. – Robert Harper Nov 21 '13 at 13:51
  • Works for me :P System.debug(new PageReference('/apex/CaseDetail?id=500d0000004Ixt5').getContent().toString()); – bigassforce Dec 21 '13 at 15:57

Well, it looks like it was due to a malformed URL. I missed the '=' sign after one of the parameter names. I don't like to copy exact code so I didn't show that I'd missed that little part.

Sorry for the bother.

  • Thanks @Robert Harper! That was the same issue I was having as well. – user16753 Feb 18 '15 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.