function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Stephen E 4Stephen E 4 

Upload a CSV using apex/visualforce and represent as a pageblocktable BUT for an unknown object/unknown amount of fields

Hello!

We have made programs in the past that allow users to upload CSVs and import data. (But for a specific object, and a specific set of fields). If you know the object, and the format of the CSV, its very simple to represent your data as a pageblocktable. (Set values in column 1 to Account.Name for example)

However, in this case, is it possible to upload a CSV and represet the data as a pageblock table but for an unknown object/unknown set of fields? 

 
jigarshahjigarshah
Stephen,

When you say unknown Object and Fields really means, the User is unaware of the target object and fields to which the data within the CSV file will be mapped before the file is uploaded.

However, in order to insert the records it is imperative to know the object and its respective field mappings. Hence, you can either maintain a configurable set of object and field mappings at compile time using a Custom Setting OR have the respective objects and field mappings chosen at run time via a user interface while the file is being uploaded.

If your worry is churning out the dynamic table based on the varying number of columns, you can actually leverage a <apex:dataTable> with <apex:column> in combination <apex:repeat> to generate dynamic number and type of columns. Apply the same stylesheets as that of a <apex:pageBlockTable> to a dataTable and have it magically transform into a pageBlockTable look and feel.

The following stackExchange thread also provides the code to generate a dynamic table in the fashion that I mentioned above, except that it uses a pageBlockTable - https://salesforce.stackexchange.com/questions/245/dynamic-number-of-columns-in-a-pageblocktable

Using a dataTable gives offers better load times and performance since it is a lightweight component.