Paul’s Salesforce Blog

What’s new with Salesforce?

Archive for the ‘Salesforce Development’ Category

Retrieve Picklist Values from a Salesforce Field

Posted by paulovery on March 2, 2014

In many cases, when you want to display an Object’s picklist, you can just use APEX:outputField on your Visualforce page.  In some cases, though, you may not be able to use APEX:outputField, resulting in an HTML SELECT object which has to be modified every time the picklist choices are changed.

The solution?   Extract the picklist values from the object for use on your Visualforce page!

This APEX code will allow your administrator to add picklist entries to a field, without requiring corresponding changes to the Visualforce Page.  The example shows how to retrieve the picklist values, and it adds them to a SelectOption list, but you should be able to modify the code to meet your own needs.

private void setPicklistOptions(){
// Retrieve the picklist values
Schema.DescribeFieldResult dfr = CustomObject__c.PicklistField__c.getDescribe();
List<Schema.PicklistEntry> picklistEntries = dfr.getPicklistValues();

// Add picklist values to a SelectOption list
productsSelect = new List<SelectOption>();
productsSelect.add(new SelectOption(‘All’, ‘All’));
for (Schema.PicklistEntry ple : picklistEntries){
productsSelect.add(new SelectOption(ple.getValue(), ple.getValue()));
}
}

Hope you find this useful!

Posted in Salesforce Development | 2 Comments »

Import Object Data Directly into a Map

Posted by paulovery on January 29, 2014

Here’s a neat trick that I found.  You can use it to import object data directly into a Map, indexed by the Object’s ID.

Here is an example:

Map<ID,Contact> contactMap = new Map<ID,Contact>([select id, name, phone, email from contact]);

 

This will import the results of the SOQL query directly into the Map!

 

I hope you find this as useful as I do.

Posted in Salesforce Development | Tagged: , | 1 Comment »

New Releases of Force.com Cookbook and Force.com Fundamentals

Posted by paulovery on November 22, 2009

Salesforce.com has released new versions of both the Force.com Cookbook and Force.com Fundamentals.  Blog entries can be found on the Salesforce Community site at the links, below.

Force.com Cookbook Blog Entry

Force.com Fundamentals Blog Entry

You can download a .pdf version of these documents if you have a developer account.  Developer accounts are available for free by clicking the “Join Now” link at the top of either of the blog pages listed.

Posted in Salesforce Development | Leave a Comment »

Watch Force.com Developer Courses on iTunes!

Posted by paulovery on September 1, 2009

Great news!  Salesforce.com has made their developer courses available on iTunes!

To see their blog post, follow this link:

http://blog.sforce.com/sforce/2009/08/watch-forcecom-developer-courses-on-itunes.html

Posted in Salesforce Development | Leave a Comment »

Formatting Time in APEX

Posted by paulovery on August 27, 2009

Every time I need to display the date and time someplace, I have to go digging through old code to find examples of how I’ve formatted  it in the past.  When I encountered this problem today,  I decided to post a blog on it.  In the future I’ll be able to come here to see how to do it, and hopefully others will benefit from it, as well:

So here’s the goal of this blog:  I simply want to create a string that displays the current time & date in some readable format, such as: Aug 26, 2009 02:45 AM.  This is actually quite easy to do, once you know how to retrieve the time and what the formatting characters are to display it properly.

Getting the current time is easy since there is a simple function that returns the current time into a DateTime variable:  datetime.now()

So that leaves formatting the result, which is the part that is difficult to memorize if you don’t use it frequently.  Formatting requires that you provide a character sequence that defines what the string-formatted version of the date/time will look like.  For example, to format a string to look like this: 08-26-09 02:45 AM, you would use a format string that looks like this: MM-dd-yy hh:mm a

Here’s a table of format characters and what piece of the Date/Time object they display:

Letter Date or Time Piece Examples
G Era G = AD
y Year yy = 09, yyyy = 2009
M Month MM = 08, MMM = Aug, MMMMM = August
w Week in year w = 35
W Week in month W = 3
D Day in year D = 235
d Day in month dd = 27
F Day of week in month F = 2
E Day in week E = Thu, EEEEE = Thursday
a AM/PM a = AM or PM
H Hour in day (0-23) HH = 23
k Hour in day (1-24) kk = 24
K Hour in am/pm (0-11) KK=11
h Hour in am/pm (1-12) hh = 12
m Minutes in hour mm = 30
s Second in minute ss = 55
S Millisecond in second SSS = 888
z Time zone z = EDT, zzzzz = Eastern Daylight Time
Z Time zone offset Z = -0400

So all that leaves to show is an example:

DateTime d = datetime.now();
timeStr = d.format(‘MMMMM dd, yyyy hh:mm:ss a’);

Which results in a value being assigned to timeStr that looks something like this: August 27, 2009 03:15:49 PM

Keep in mind that this doesn’t have to be used for the current time.  You can use this method to format any DateTime object, no matter where you retrieved it from.  For example, you could get the CreatedDate for any object and then use the format command to display it in a Visualforce page.

Good luck..  I hope you find this useful.

Posted in Salesforce Development | Tagged: , | 15 Comments »

Installing Force.com IDE

Posted by paulovery on March 31, 2009

My hard drive died on Friday, so I’ve been re-installing applications today.  I decided to document the steps that I took to install the Salesforce IDE (Eclipse) since I had a hard time figuring out what to install both the first time, and once again today.

So here we go:

First of all, Salesforce has a help page for this, but it lists specific versions of Eclipse and the JRE virtual machine.  I wanted a more generic set of instructions that would be useful (hopefully) a year from now.  The Salesforce help page for installing the Eclipse IDE can be found here.

My laptop is running Windows XP, so I may be unaware of any nuances to do with other Operating Systems.  Feel free to comment if you find anything.

The Eclipse IDE requires a Virtual Machine to run.  When I visited the eclipse.org site, they directed me to an IBM page for a virtual machine, but I couldn’t find any download links.  Instead I used Sun’s virtual machine, which you can find here.  I downloaded and installed JRE 6, Update 13.

The latest Eclipse downloads can be found at http://www.eclipse.org/downloads/.  On this page, download “Eclipse IDE for Java Developers”.  I didn’t see any indication of what the latest release was, but the file name was eclipse-java-ganymede-SR2-win32.zip.  After downloading, I was able to find a version in the eclipse/readme/readme_eclipse.html file.  This release was 3.4.2 with a last revised date of Jan 22, 2009.

Extract the folder onto your system someplace.  There really isn’t an ‘install’.  You simply copy the eclipse folder to where ever you want eclipse to live.  I put mine under “C:\”, but “C:\Program Files” wouldn’t have been a bad choice either.

Now that the Eclipse IDE is “installed”, navigate to the eclipse folder and run eclipse.exe.  Now you’re ready to install the Salesforce specific package…

Eclipse allows you to support various environment types.  Salesforce took advantage of this by creating a version for their development environment.  To download and install it, follow these steps:

1. Click Help->Software updates then click the “Available Software” tab.
2. Click “Add Site“.  Set the URL to “http://www.adnsandbox.com/tools/ide/install/&#8221;.  Click OK.  This will add the provided URL to the list of sites.
3. Click the + to the left of the site that you just added(http://www.adnsandbox.com/tools/ide/install/).  You should see Force.com IDE listed.  Select the checkbox next to Force.com IDE.
4. Click the + to the left of Web Tools (WTP) Update Site,  then click the + to the left of Web Tools Platform (WTP) 3.0.4 and Select the checkbox next to “Web Developer Tools” (Note: The Salesforce instructions are to install the Web Standard Toos (WST) Project, but this would not work for this version of Eclipse.  I found Web Developer Tools through some trial and error)
5. Click Install.  This will download and install the specified software.

When the download/installation is done, it will ask you to restart Eclipse.  Click Ok to do so.

Now go to Windows->Open Perspective->Other and you should see Force.com listed.  Select it and click OK.

You should be all set!
Click File->New->Force.com Project, then enter the requested information and you’ll be ready to start coding!

Posted in Salesforce Development | Leave a Comment »

SObject type does not allow triggers: CaseComment

Posted by paulovery on March 24, 2009

I can’t create a workaround using triggers, since you can’t make a trigger for the CaseComment object.

I’ve submitted a bug to Salesforce, but I’m still trying to come up with something else.

Posted in Salesforce Administration, Salesforce Development | Leave a Comment »