0

I have made an external API call (JSON type). And I have stored the fields and values in a Map<String, List> object.

Now I have to created a visual force page that displays the Map in columnwise format.

Please refer to the below code:

public with sharing class testClass{    
    
    // Initialization
    public string companyName{get; set;}

    public void showRecord(){
        Map<String, List<Object>> pageToWordMap = new Map<String, List<Object>>();
        
        // Input from the user
        String str =  companyName; // input from Visual force page
        String query = '';
        
        //create a rest endpoint
        string requestEndPoint = '***** end point *******' ;
        
        // requests        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint(requestEndPoint);
        request.setMethod('GET');
        request.setHeader('ApiToken', '*********************');   
        request.setHeader('Accept', 'application/json');  
      
        HttpResponse response = http.send(request);

         if (response.getStatusCode() == 200 && response.getBody() != null){
            Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(response.getBody());
            List <Object> data = (List<Object>)results.get('Data'); 
                         
             
            // for loop
            
            for(Object dta : data){
                // getting the attributes of data
                Map<String, Object> attributes = (Map<String, Object>) dta;
                // now loop through the data
                for(String lst : attributes.keyset()){
                    // handle response
                    String indexKey = lst;
                    
                    if(!pageToWordMap.containsKey(indexKey)){
                        // add the key and new list
                        pageToWordMap.put(indexKey, new List<Object>());
                    }
                    // get the key and add the value
                    pageToWordMap.get(indexKey).add(attributes.get(indexKey));
                } 
                
                // for debug purpose
                /*
                for(String curPage: pageToWordMap.keySet())
                {
                    String key = curPage;
                    String val = (String)pageToWordMap.get(curPage);
                    System.debug(key+'-----'+val);
                } */
            }
        }
    }
}

The Visualforce code, that I am trying is:

<apex:page controller="testClass" showHeader="false" sidebar="false"> 
> 
>       <apex:form>
>             <apex:pageBlock title="Company Search">
>                 <apex:pageBlocksection >
>                     Please enter the company name <apex:inputText value = "{! companyName}"/>
>                     <apex:commandButton action="{! showRecord}" value="submit"/>
>                 </apex:pageBlocksection>             
>             </apex:pageBlock>
>             <apex:pageBlock title="The Data">
>                 <apex:pageBlockTable value = "{! pageToWordMap}" var = "json">
>                   <apex:column headerValue="Name" value="{! json.NAME}"/>
>                 </apex:pageBlockTable>
>             </apex:pageBlock> 
>         </apex:form> </apex:page>

I am new to APEX programming, and now I am not able to figure out how to create the record columns.

The error is ***

Unknown property 'testClass.pageToWordMap'


The column header names are:

  1. NAME
  2. BVDID
  3. COUNTRY_ISO_CODE

please help?????

0

pageToWordMap is a local variable to showRecord(). You want to make it a property of your Apex class instead, so that it can be referenced from your Visualforce page. That is why the error message says the property is unknown - it doesn't exist, yet.

Map<String, List<Object>> pageToWordMap { get; private set; }
// the private keyword here prevents the value from being set via Visualforce, 
// it can only be set within testClass

To prevent issues with null values it's also a good idea to set pageToWordMap to an empty map in a constructor.

public testClass() {
    pageToWordMap = new Map<String, List<Object>>();
}
| improve this answer | |
  • ok. and how would I use this to create a record table in VF page? – Suel Ahmed Jul 2 at 19:35
  • @SuelAhmed that's a good point, apex:pageBlockTable expects a List and not a Map. Outputting your map's values may be problematic since they're generic Objects. You may need a new, wrapper class to hold the values you're interested in (name, bvdid, and country code). – nbrown Jul 2 at 20:39
  • there was another way I created a wrapper class, Please refer to question salesforce.stackexchange.com/questions/311497/… Again the question arises on how to use this to create a VF page – Suel Ahmed Jul 2 at 20:49
  • @SuelAhmed your other post has the same issue. The Visualforce page can only access properties from a class, so you will need to make Data a property by giving it a getter, and then you need to reference each field you want in your table by giving it a column. developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/… – nbrown Jul 2 at 21:01
  • I understand the problem now but if its possible can you give me a small example on how to do exactly what you said? – Suel Ahmed Jul 2 at 21:27

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.