• EW
  • NEWBIE
  • 25 Points
  • Member since 2004

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 4
    Replies
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
  • October 28, 2008
  • Like
  • 0
Hello all. I'm having an issue with my Visualforce controller code and was wondering if anyone had any ideas for me.

The error I'm receiving is:
Code:
System.Exception: DML currently not allowed

Class.maExtension.save: line 18, column 13
External entry point
 
My controller code is:
Code:
public class maExtension {

 public scr__c scr {get; private set;}

 public maExtension() {
  scr = new scr__c();
 }

 public pagereference save() {
  scr.market_area__c = getmarket().id;
  system.debug('Market ID: ' + scr.market_area__c);
  system.debug('First Name: ' + scr.first_name__c);
  system.debug('Last Name: ' + scr.last_name__c);
  system.debug('Full Address: ' + scr.street_1__c + 
   ' ' + scr.street_2__c + ' ' + scr.city__c + 
   ' ' + scr.state__c + ' ' + scr.zip_code__c);
  try {
   insert scr;
   system.debug('Insert Successful');
  } catch(System.DmlException e) {
   system.debug(e.getDmlMessage(0));
  }
  return null;
 }

 ma__c market;

 public string getzip() {
  string zip = apexpages.currentpage().getparameters().get('zip');
  return zip;
 }

 public string getaddress() {
  string address = apexpages.currentpage().getparameters().get('street');
  return address;
 }

 public string getcity() {
  string city = getmarket().name;
  return city;
 }

 public string getstate() {
  string state = getmarket().primary_state__c;
  return state;
 }

 public boolean getmarketexists() {
  string zip = getzip();
  integer count = [select count() 
  from ma__c 
  where primary_zip_code__c = :zip 
  limit 1];
  boolean marketexists = count > 0 ? true : false;
  return marketexists;
 }

 public boolean getmarkethascoverage() {
  string zip = getzip();
  integer count = [select count() 
  from ma__c 
  where primary_zip_code__c = :zip 
  and (status__c = 'Partial Coverage' or status__c = 'Full Coverage') 
  limit 1];
  boolean getmarkethascoverage = count > 0 ? true : false;
  return getmarkethascoverage;
 }

 public ma__c getmarket() {
  if (getmarketexists() == true) {
   string zip = getzip();
   market = [select id, name, primary_zip_code__c, primary_state__c, status__c 
   from ma__c 
   where primary_zip_code__c = :zip 
   limit 1];
  } else {
   market = new ma__c();
  }
  return market;
 }

}
 
My Visualforce Page is:
Code:
 <apex:form id="scrform">
  <apex:inputfield id="scrfirstname" value="{!scr.first_name__c}" />First Name<br />
  <apex:inputfield id="scrlastname" value="{!scr.last_name__c}" />Last Name<br />
  <apex:inputfield id="scremail" value="{!scr.email_address__c}" />Email<br />
  <apex:inputfield id="scrstreet1" value="{!scr.street_1__c}" />Street Address 1<br />
  <apex:inputfield id="scrstreet2" value="{!scr.street_2__c}" />Street Address 2<br />
  <apex:inputfield id="scrcity" value="{!scr.city__c}" />City<br />
  <apex:inputfield id="scrstate" value="{!scr.state__c}" />State<br />
  <apex:inputfield id="scrzip" value="{!scr.zip_code__c}" />Zip Code<br />
  <apex:commandbutton action="{!save}" value="Sign Me Up" /><br />
 </apex:form>

When the "Sign Me Up" button invoking the save() method is clicked, I receive the exception. Any help would be greatly appreciated. Thanks!
  • October 21, 2008
  • Like
  • 0
Hi,
I have an problem. I have used EnhancedList Component of Salesforce but it does not work with USER object


Code:
<apex:page standardController="User" tabstyle="User">
<apex:enhancedList type="User" height="750" rowsPerPage="25" id="UserList"></apex:enhancedList>
</apex:page>

Can anyone Help me to find out my problem solution ?


Hello all. I'm having an issue with my Visualforce controller code and was wondering if anyone had any ideas for me.

The error I'm receiving is:
Code:
System.Exception: DML currently not allowed

Class.maExtension.save: line 18, column 13
External entry point
 
My controller code is:
Code:
public class maExtension {

 public scr__c scr {get; private set;}

 public maExtension() {
  scr = new scr__c();
 }

 public pagereference save() {
  scr.market_area__c = getmarket().id;
  system.debug('Market ID: ' + scr.market_area__c);
  system.debug('First Name: ' + scr.first_name__c);
  system.debug('Last Name: ' + scr.last_name__c);
  system.debug('Full Address: ' + scr.street_1__c + 
   ' ' + scr.street_2__c + ' ' + scr.city__c + 
   ' ' + scr.state__c + ' ' + scr.zip_code__c);
  try {
   insert scr;
   system.debug('Insert Successful');
  } catch(System.DmlException e) {
   system.debug(e.getDmlMessage(0));
  }
  return null;
 }

 ma__c market;

 public string getzip() {
  string zip = apexpages.currentpage().getparameters().get('zip');
  return zip;
 }

 public string getaddress() {
  string address = apexpages.currentpage().getparameters().get('street');
  return address;
 }

 public string getcity() {
  string city = getmarket().name;
  return city;
 }

 public string getstate() {
  string state = getmarket().primary_state__c;
  return state;
 }

 public boolean getmarketexists() {
  string zip = getzip();
  integer count = [select count() 
  from ma__c 
  where primary_zip_code__c = :zip 
  limit 1];
  boolean marketexists = count > 0 ? true : false;
  return marketexists;
 }

 public boolean getmarkethascoverage() {
  string zip = getzip();
  integer count = [select count() 
  from ma__c 
  where primary_zip_code__c = :zip 
  and (status__c = 'Partial Coverage' or status__c = 'Full Coverage') 
  limit 1];
  boolean getmarkethascoverage = count > 0 ? true : false;
  return getmarkethascoverage;
 }

 public ma__c getmarket() {
  if (getmarketexists() == true) {
   string zip = getzip();
   market = [select id, name, primary_zip_code__c, primary_state__c, status__c 
   from ma__c 
   where primary_zip_code__c = :zip 
   limit 1];
  } else {
   market = new ma__c();
  }
  return market;
 }

}
 
My Visualforce Page is:
Code:
 <apex:form id="scrform">
  <apex:inputfield id="scrfirstname" value="{!scr.first_name__c}" />First Name<br />
  <apex:inputfield id="scrlastname" value="{!scr.last_name__c}" />Last Name<br />
  <apex:inputfield id="scremail" value="{!scr.email_address__c}" />Email<br />
  <apex:inputfield id="scrstreet1" value="{!scr.street_1__c}" />Street Address 1<br />
  <apex:inputfield id="scrstreet2" value="{!scr.street_2__c}" />Street Address 2<br />
  <apex:inputfield id="scrcity" value="{!scr.city__c}" />City<br />
  <apex:inputfield id="scrstate" value="{!scr.state__c}" />State<br />
  <apex:inputfield id="scrzip" value="{!scr.zip_code__c}" />Zip Code<br />
  <apex:commandbutton action="{!save}" value="Sign Me Up" /><br />
 </apex:form>

When the "Sign Me Up" button invoking the save() method is clicked, I receive the exception. Any help would be greatly appreciated. Thanks!
  • October 21, 2008
  • Like
  • 0
I get "Sforce is not defined" error message on the following code for mass completing tasks. The button is executing Javascript.

If the user has "View all Data" permission, I do not get the error message.

I read somwhere that AJAX libray is not being loaded and including {!REQUIRESCRIPT("/soap/ajax/12.0/connection.js")}; should fix it. But it doesn't.

Interesting, "mark complete" code for single task works okay - so i suspect there is an interaction with GETRECORDIDs.

Code:
/* This code allows the javascript to access the API and push data into the Org.*/
{!REQUIRESCRIPT("/soap/ajax/12.0/connection.js")};
sforce.connection.session = "{!$Api.Session_ID}";

function massCompleteTasks( )
{
var taskArray = {!GETRECORDIDS( $ObjectType.Task )};
if (taskArray == null || taskArray.length == 0) {
    alert("Please select the tasks you wish to close.");
    return;
} else {
   var newTaskArray = new Array();
  for (var i = 0; i < taskArray.length; i++) {
     var newTask = new sforce.SObject("Task");
     newTask.Id = taskArray[i];
     newTask.Status = "Completed";
     newTaskArray.push(newTask);
  }
}
var callCompleted = false;  
try
{
   var result = sforce.connection.update(newTaskArray);
   callCompleted = true;
} catch(error) {
   alert("Failed to update Tasks with error: " + error);
}
if (callCompleted) {
    for (var i = 0; i < result.length; i++) {
         if (!result[i].getBoolean("success")) {
            alert("Task (id='" + newTaskArray[i] + "') could not be updated with error: " + result[i].errors);
         }
    }
    window.location.reload(true);
}

}

massCompleteTasks();

 
This code for markcomplete works okay:

Code:
* This code allows the javascript to access the API and push data into the Org.*/
{!REQUIRESCRIPT("/soap/ajax/12.0/connection.js")};
sforce.connection.session = "{!$Api.Session_ID}";

function updateTask( )
{
 try
    {
  var task = new sforce.SObject("Task");
  task.Id = "{!Task.Id}";
     task.Status = "Completed";
     var result = sforce.connection.update([task]);
     if (result[0].getBoolean("success") == false ) {
   alert(result[0].errors.message);
   return;
  }    
  window.top.location.href=window.top.location.href;
 } 
 catch (e) {
  alert(e);
 } 
}

updateTask();