1

I'm having issues calling a class that works fine in Lightning Component within a visualforce page using apex

i tried this but getting the error unknown property httpCallOutCtrl.getCalloutResponseContents

    <apex:pageBlock title="Visualforce (Using standard Visualforce Components)" mode="detail">
                <apex:pageBlockTable value="{!getCalloutResponseContents}" var="sf">
                    <apex:column headerValue="postId" value="{!sf.val}"/>
                    <apex:column headerValue="ID" value="{!sf.id}"/>
                    <apex:column headerValue="Name" value="{!sf.name}"/>
                    <apex:column headerValue="email" value="{!sf.email}"/>
                    <apex:column headerValue="body" value="{!sf.body}"/>
                </apex:pageBlockTable>
    </apex:pageBlock>

heres my class

public class httpCallOutCtrl {
    @AuraEnabled


    public static Map < String, Object > getCalloutResponseContents(ID contactId) {
        String baseURL = 'some-site.com';
        String apiKey = 'some-key';

        Contact contact = [SELECT external_id__c FROM Contact WHERE Id=:contactId];
        string AdvanceId = Contact.external_id__c
        String url = baseURL + AdvanceId +'/?api_key='+apiKey ;

        // Instantiate a new http object
        Http h = new Http();

        // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
        HttpRequest req = new HttpRequest();

        req.setEndpoint(url);
        req.setMethod('GET');

        // Send the request, and return a response
        HttpResponse res = h.send(req);
        System.debug('response:--> ' + res.getBody());

        // Deserialize the JSON string into collections of primitive data types.
        Map < String,
        Object > resultsMap = (Map < String, Object > ) JSON.deserializeUntyped(res.getBody());
        system.debug('resultsMap-->' + resultsMap);
        return resultsMap;

    }

}
  • I guess people are making too many assumptions... What does the response actually look like? Is it really a map? If so, you need to rework some of your code. – sfdcfox Apr 26 '17 at 18:57
  • @sfdcfox if i console.log my response in the lightning controller , it is an object array of objects that i then can iterate over in lightning...I suppose the best way to put this is i need a class that I can use for lightning and apex because our org will support both – Jay Rizzi Apr 26 '17 at 19:11
1

Visualforce is trying to find an attribute getCalloutResponseContents which you have used in <apex:pageBlockTable value="{!getCalloutResponseContents}" var="sf">

Moreover, the value of <apex:pageBlocktable> will be of type List, not Map.

Secondly, you are referencing the method getCalloutResponseContents(ID contactId) which expects parameter as contactId, which is also not matching.

I would suggest this approach.

  1. Create a class which will hold those attributes which you are trying to show in VFP.

MyValueObject

public class MyValueObject {
  public Integer id;
  public String name;
  public String email;
  public String body;
 }
  1. Create myValueObjectResult class

myValueObjectResult

public class myValueObjectResult {
  public myValueObject myValueObj;
}
  1. Create a property in the controller

property

List<myValueObjectResult> lstmyValueObjectResult {get;set;}
  1. from httpResponse prepare the result

change your controller method

HttpResponse response = http.send(request);
result = (myValueObjectResult) JSON.deserialize(response.getBody(), MyValueObject.class);
lstmyValueObjectResult.add(result);
  1. Finally try to show the result in VFP

Visualforce

<apex:pageBlock title="Visualforce (Using standard Visualforce Components)" mode="detail">
    <apex:pageBlockTable value="{!lstmyValueObjectResult}" var="sf">
        <apex:column headerValue="postId" value="{!sf.val}"/>
        <apex:column headerValue="ID" value="{!sf.id}"/>
        <apex:column headerValue="Name" value="{!sf.name}"/>
        <apex:column headerValue="email" value="{!sf.email}"/>
        <apex:column headerValue="body" value="{!sf.body}"/>
    </apex:pageBlockTable>
</apex:pageBlock>

You can refer this answer for this approach.Create an Apex class that calls a REST endpoint and write a test class - Trailhead

| improve this answer | |
  • Can you even pass arguments to a getter method/ construct a getter with parameter ? – Rao Apr 26 '17 at 18:49
  • 1
    I am writing, actually he is doing wrong – Santanu Boral Apr 26 '17 at 18:50
  • @SantanuBoral thank you for the example, the quick question is then after I make these changes, will i be able to call the class in my lightning controller without any issue? I'm trying to make a single class work for both apex(classic ) and lightning – Jay Rizzi Apr 26 '17 at 19:12
  • 1
    @JayRizzi Yes, his latest edits basically describe what I would have, but in more detail. Go with this solution, it should work fine. – sfdcfox Apr 26 '17 at 19:17
  • Ok, i am attempting today and will let you know how it goes, thanks so far everyone – Jay Rizzi Apr 27 '17 at 12:47

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.