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
EWEW 

Problem Using Dynamic Apex with Visualforce apex:repeat Components

I have a custom object called "Product Bundle" (pb__c). What I would like to do is: 1) dynamically query the object definition to find the API names and labels for all of its fields, 2) build a string from the field names that I can use in a SOQL statement to query all rows (there are only 4 records for this table), 3) iterate through BOTH the field labels AND SOQL results to display this data on a Visualforce page.

Basically, I want to show that a Visualforce page can be built to automatically display newly created custom fields and their values, without having to change the page markup each time a new field is added.

I have so far been able to dynamically get the field labels and values, and I've been able to construct the query string and get the data, but I'm having trouble with putting the labels with the field values on the page. Basically I need to iterate through both the field metadata results as well as the record query results. I know that there will be use of the <apex:repeat> component, but not sure how to proceed any further.

Here are the relevant methods I have so far (note that the methods to retrieve the field labels are omitted below, but they are just a repeat of getting the field names, so I left them out for readability).

Code:
 /*
  * BASE METHOD FOR GETTING THE ENTIRE FIELD MAP FOR THE PRODUCT BUNDLE (pb__c) OBJECT
  */

 public map<string, sobjectfield> getpbfieldmap() {
  sobjecttype objtoken = schema.getglobaldescribe().get('pb__c');
  describesobjectresult objdef = objtoken.getdescribe();
  map<string, sobjectfield> bundlefieldmap = objdef.fields.getmap();
  return bundlefieldmap;
 }


 /*
  * EXTRACTS ALL FIELD NAMES FROM THE BASE METHOD MAP, REMOVES THE ONES I DON'T WANT,
  * THEN PUTS THE REMAINING NAMES INTO A LIST ORDERED ALPHABETICALLY
  */

 public list<string> getpbfieldnames() {
  list<string> pbfieldnames = new list<string>();
  set<string> namestoexclude = new set<string> {
   'Id', 'IsDeleted', 'OwnerId', 'CreatedById', 'LastModifiedById', 
   'CreatedDate', 'LastModifiedDate', 'SystemModstamp', 'order__c'
  };
  map<string, sobjectfield> fieldmap = getpbfieldmap();
  for (sobjectfield f : fieldmap.values()) {
   string nametocheck = f.getdescribe().name;
   if (namestoexclude.contains(nametocheck)) {
    continue;
   } else {
    pbfieldnames.add(nametocheck);
   }
  }
  pbfieldnames.sort();
  system.debug('Final field name list: ' + pbfieldnames);
  return pbfieldnames;
 }


 /*
  * TRANSFORMS THE FIELD NAME LIST INTO A CONCATEDATED STRING THAT CAN BE USED IN
  * A SOQL QUERY
  */

 public string getpbfieldnamesforquery() {
  string output = '';
  list<string> fn = getpbfieldnames();
  for (integer i = 0; i < fn.size(); i ++) {
   if (i == 0) {
    output = fn.get(i);
   } else {
    output += ', ' + fn.get(i);
   }
  }
  system.debug('Field names used for query string: ' + output);
  return output;
 }


 /*
  * RETRIEVES AN ARRAY OF PRODUCT BUNDLE RECORDS USING THE DYNAMIC SOQL QUERY RETURNED
  * FROM THE METHODS ABOVE
  */

 public pb__c[] getproductbundles() {
  string fieldstoquery = getpbfieldnamesforquery();
  string fullquerystring = 'select ' + fieldstoquery + ' from pb__c order by order__c';
  pb__c[] bundles = database.query(fullquerystring);
  system.debug(bundles);
  return bundles;
 }

 
Any advice or direction would be greatly appreciated. Thanks!


Message Edited by EW on 10-28-2008 01:32 PM
SiriusBlackOpSiriusBlackOp

Just FYI:

 

If you are looking for a way to create and use Product Bundles in salesforce, then check this native app on the AppExchange.

 

http://sites.force.com/appexchange/listingDetail?listingId=a0N300000016d1tEAA

 

:smileywink:

Mitesh SuraMitesh Sura

There is a native app Product Bundle (Kits) for Salesforce on AppExchange that helps create and seel Bundles in Salesforce. need: http://appexchange.salesforce.com/listingDetail?listingId=a0N30000007r2QFEAY. You may have found a solution, thought I post anyways so others can take advantage of the app.