Requirement
We need to create Visualforce page to display the data in our Salesforce instance. However, the data is not stored in any of the Salesforce object. The data is fetched using making a REST call to an external system. The response we are getting is in JSON format, so to display the data we are supposed to parse the JSON response and then display it on Visualforce page.
In this article, I am covering only one problem I faced while implementing this. The brief about the problem is I am able to display the data in the List view(as in standard Salesforce list view), using the PageBlockTable tag. Now, on the click of the record, I need to open a detail page of the corresponding record. Had the data been in Salesforce, I could have directly used the Salesforce ID to uniquely identify the record. But in this case, I dont have any unique identifier that can be used. So I need to capture the click of the User, that is the row index on which the user clicks to display the particular record. Or you can say, the index of the record acts as the unique identifier.
Solution
There can be two approaches to handle this situation:
I am going to discuss only the 2nd approach right now:
I will use a slightly different than normal approach to display the list of records.
In the controller class, we have a list of records, say in variable "allRecords". I will maintain another list of Integer as follows:
List<Integer> indexes = new List<Integer>();
This list of integer contains the numbers from 0 to n-1, where "n" is the size of the list of records, i.e. the size of "allRecords". Make sure to update the "indexes" list whenever "allRecords" is updated.
Now, in Visualforce page, use the "indexes" list to display the records.
Visualforce page code:
<apex:pageblockTable value="{!indexes}" var="ind">
<apex:column headerValue="Name" value="{!allRecords[ind].name}">
<apex:commandLink value="{!{!allRecords[ind].name}}" action="{!yourControllerAction}">
<apex:param name="index" value="{!ind}"/>
</apex:commandLink>
</apex:column>
<apex:column headerValue="Street" value="{!allRecords[ind].streetAddress}"/>
<apex:column headerValue="City" value="{!allRecords[ind].city}"/>
<apex:column headerValue="State" value="{!allRecords[ind].stateProvince}"/>
<apex:column headerValue="Postal Code" value="{!allRecords[ind].postalCode}"/>
</apex:pageblockTable>
In the above code, the index of the link clicked by the user, is passed to the controller using the <apex:param> tag. In the controller, the record on the index can be pulled directly from the list like:
allRecords[index]
All done..!!
We need to create Visualforce page to display the data in our Salesforce instance. However, the data is not stored in any of the Salesforce object. The data is fetched using making a REST call to an external system. The response we are getting is in JSON format, so to display the data we are supposed to parse the JSON response and then display it on Visualforce page.
In this article, I am covering only one problem I faced while implementing this. The brief about the problem is I am able to display the data in the List view(as in standard Salesforce list view), using the PageBlockTable tag. Now, on the click of the record, I need to open a detail page of the corresponding record. Had the data been in Salesforce, I could have directly used the Salesforce ID to uniquely identify the record. But in this case, I dont have any unique identifier that can be used. So I need to capture the click of the User, that is the row index on which the user clicks to display the particular record. Or you can say, the index of the record acts as the unique identifier.
Solution
There can be two approaches to handle this situation:
- Use of javascript to identify the row number of the clicked element of the PageBlockTable.
- Use of <apex:param> tag to pass the index to controller.
I am going to discuss only the 2nd approach right now:
I will use a slightly different than normal approach to display the list of records.
In the controller class, we have a list of records, say in variable "allRecords". I will maintain another list of Integer as follows:
List<Integer> indexes = new List<Integer>();
This list of integer contains the numbers from 0 to n-1, where "n" is the size of the list of records, i.e. the size of "allRecords". Make sure to update the "indexes" list whenever "allRecords" is updated.
Now, in Visualforce page, use the "indexes" list to display the records.
Visualforce page code:
<apex:pageblockTable value="{!indexes}" var="ind">
<apex:column headerValue="Name" value="{!allRecords[ind].name}">
<apex:commandLink value="{!{!allRecords[ind].name}}" action="{!yourControllerAction}">
<apex:param name="index" value="{!ind}"/>
</apex:commandLink>
</apex:column>
<apex:column headerValue="Street" value="{!allRecords[ind].streetAddress}"/>
<apex:column headerValue="City" value="{!allRecords[ind].city}"/>
<apex:column headerValue="State" value="{!allRecords[ind].stateProvince}"/>
<apex:column headerValue="Postal Code" value="{!allRecords[ind].postalCode}"/>
</apex:pageblockTable>
In the above code, the index of the link clicked by the user, is passed to the controller using the <apex:param> tag. In the controller, the record on the index can be pulled directly from the list like:
allRecords[index]
All done..!!