3

I am trying to display the contents of a remote text file on a Visualforce Page. I have implemented a class called MyApexClass containing a getTextFileContentAsString method that returns the content of a remote text file as a string.

I then try to invoke that method in the Visualforce page and display it with apex:outputText like so "{!MyApexClass.TextFileContentAsString}". However, I get an error saying:

Error: Unknown property MyApexClass.TextFileContentAsString referenced in MyTemplate. 

What is the correct way to invoke the method, and get the returned string to render on the VisualForce page?

Here are the Apex Class and the VisualForce page excerpts:

Apex Class:

public class MyApexClass {

  public String getTextFileContentAsString() {
    String url = 'http://somehost.com/theTextFile.txt';
    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint(url);
    req.setMethod('GET');
    HttpResponse res = h.send(req);
    return res.getBody();
  }
}

VisualForce Page excerpt:

...
<div>
<apex:outputText value="{0}">
<apex:param value="{!MyApexClass.TextFileContentAsString}" />
</apex:outputText>
</div>
...

Do I need to do anything special to make the MyApexClass "visible" to the VisualForce page?

  • You cannot pass parameters to method bindings made in your Visualforce page check this link salesforce.stackexchange.com/questions/11784/… – SFIntegrator Feb 15 '14 at 9:39
  • I have now removed the parameter and hardcoded the URL, as user320 suggested. However, I am still stuck with the "Unknown function MyApexClass.TextFileContentAsString. Check spelling." error. – moog Feb 15 '14 at 9:44
  • Remove parenthesis also – SFIntegrator Feb 15 '14 at 9:47
  • Have you referenced your controller class in the apex:page tag? – Keith C Feb 15 '14 at 10:17
  • Hi Keith, that turned to be the key point. Once I referenced controller class in the apex:page tag, it works. My concern now is - would explicitly specifying the controller break previous behaviour? How do I know which controller was being picked up when the controller class was not specified explicitly in the page tag? – moog Feb 15 '14 at 10:47
1

Your approach is just fine. It's just lacking an exclamation mark, and the argument should be removed, try:

<apex:param value="{!TextFileContentAsString}" />

Then hard-code the URL into the req.setEndpoint in your Apex class. (Visualforce getters cannot have parameters)

  • Hi, when I put the exclamation mark, I get: Error: Unknown function MyApexClass.TextFileContentAsString. Check spelling. – moog Feb 15 '14 at 9:22
  • Make sure you have the method implemented in your code and it returns the string. – zuke Mar 1 '14 at 10:08
0

This {!}, is the syntax for merge fields in visualforce. I guess you are missing '!'. You can also cast responce body to string, just to be sure.

0

Remove () after method name

<apex:outputText value="{!TextFileContentAsString}">
</apex:outputText>

Public class yourController{
   //getter method of controller
   public string getTextFileContentAsString{
     return MyApexClass.TextFileContentAsString();
   }   
}
  • I now get an Unknown property error instead. I suspect my class is not visible because somehow, the VisualForce page is not "associated" with it. Something to do with "controllers"? – moog Feb 15 '14 at 9:52
  • If TextFileContentAsString is not the get method of controller, then you need to define a getter that interns call TextFileContentAsString to get the string value – SFIntegrator Feb 15 '14 at 10:00

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.