This is a place to list code samples - collections of pieces of code that you think will be useful to our community. These are typically small (a few pages of Apex, or Apex and a few Visualforce pages, or a trigger or two) and one-off. If you want to instead create a collaborative coding project, check out Code Share

Contents

Samples

Style your Visualforce Pages

Now add styles to your Visualforce Pages, make them more appealing and user friendly... Use CSS files as static resources and refer them in your Visualforce Pages.. Learn how --Click here

Customized Database Search

There has always been a need to customize the search feature of salesforce. Use the example provided in the link below, customize it as per your needs and make as much use of Visualforce benefits as possible...

Customized Search and Selection - Click here

Dynamic Picklist using Visualforce

Add values to a picklist dynamically using the UI rather than your administrator going to field level details and adding values..

Developed using Visualforce..

Screenshots and Code available here

Flex and Force.com

This is the "Last 5 Contacts" sample shown during the recent webinar about building apps using Force.com and Adobe's Flex development framework. Integrate into Visualforce with the following:

<apex:page > 
<apex:flash src="{!$Resource.ContactDemoSWF}" height="300" width="300" 
            flashvars="sessionId={!$Api.Session_ID}&serverUrl={!$Api.Partner_Server_URL_160}"/> 
</apex:page> 

Download the ZIP - Ryan Marples

Visualforce Sample - PDF Quotes and Email Templates

This code sample illustrates how to use Visualforce to define email templates

Visualforce EmailQuote2PDF

Lead Mass Change Status For PRM

This VF page will allow you to add the Mass Change Lead Status to a Partner Portal List View layout. By default, Salesforce removes this button from PRM Lead View pages, which is present in normal Salesforce Lead Views. To install, copy the code from the zip file and create a new VF page with it. Make sure you set the security for your PRM users to access the page. Create a custom button with the following parameters: - Display Type: List Button - Check "Display Checkboxes (for Multi-Record Selection)" - Behavior: Display in existing window with sidebar - Content Source: Visual Force page - Content: Select the page you created with the downloaded code Then, add your button to the Search Layout for Leads List View for your PRM users.

How To Deploy A Trigger To Production - Contact Update Trigger with Test Class

This trigger automatically updates the owner of a Contact record to the Account Owner on save. Code included has a hard coded value of Owner and Account of the environment where it is deployed.

To Deploy Follow:

  • Edit Test Class included and change the OwnerID and AccountID with your values in your environment.
  • Open Eclipse.
  • Copy the files in the zip to the appropriate trigger and class folders in your project
  • Select The TRIGGER file TestClass File in Tree View of the Project Explorere in Eclipse, using CONTROL KEY, you will be able to Select ,Multiple Files in Tree View of Eclipse.
  • Right Click -> Force.com -> Deploy to Server.
  • Provide your Deployment Environment credentials.
  • Click Next and Again Next, now Click Finish o deploy the Test Class and Trigger to production.
  • The trigger as well as test class will be test fired.

NOTE: I've made the class @isTest, so that No Actual Modifications to Production data is done, the Test Class execution will be Fired in a Simulated Environment. Test Class has been Commented to help make an understanding of what is being done using the trigger.

jQuery DatePicker Component for Selecting Date in VisualForce Page

This Component When used will place a TextInput with a calendar icon adjacent to the input control, so as to Popup a Calendar and have selected Date inserted in the input control, just like the Date Control of Salesforce, which is currently Bounded to be Used only with Data Bounded Fields.

So this way, you simple place a call like: <c:jQueryPickDate pickDateLabel="Pick Date:" pickDateField="selectDate" value="{!selectedDate}" />

And you will have a Date Control on you VisualForce page.

This control is created using jQuery's DatePicker plugin.

Parse a CSV with APEX

There are many tools to import CSV files, but if you need to work on multiple objects from the same file, you might need to do it in Apex. Here is a CSV parsing function that returns an array (lines) of string arrays (fields). Does not handle newlines in the content though.

	public static List<List<String>> parseCSV(String contents,Boolean skipHeaders) {
		List<List<String>> allFields = new List<List<String>>();

		// replace instances where a double quote begins a field containing a comma
		// in this case you get a double quote followed by a doubled double quote
		// do this for beginning and end of a field
		contents = contents.replaceAll(',"""',',"DBLQT').replaceall('""",','DBLQT",');
		// now replace all remaining double quotes - we do this so that we can reconstruct
		// fields with commas inside assuming they begin and end with a double quote
		contents = contents.replaceAll('""','DBLQT');
		// we are not attempting to handle fields with a newline inside of them
		// so, split on newline to get the spreadsheet rows
		List<String> lines = new List<String>();
		try {
			lines = contents.split('\n');
		} catch (System.ListException e) {
			System.debug('Limits exceeded?' + e.getMessage());
		}
		Integer num = 0;
		for(String line : lines) {
			// check for blank CSV lines (only commas)
			if (line.replaceAll(',','').trim().length() == 0) break;
			
			List<String> fields = line.split(',');	
			List<String> cleanFields = new List<String>();
			String compositeField;
			Boolean makeCompositeField = false;
			for(String field : fields) {
				if (field.startsWith('"') && field.endsWith('"')) {
					cleanFields.add(field.replaceAll('DBLQT','"'));
				} else if (field.startsWith('"')) {
					makeCompositeField = true;
					compositeField = field;
				} else if (field.endsWith('"')) {
					compositeField += ',' + field;
					cleanFields.add(compositeField.replaceAll('DBLQT','"'));
					makeCompositeField = false;
				} else if (makeCompositeField) {
					compositeField +=  ',' + field;
				} else {
					cleanFields.add(field.replaceAll('DBLQT','"'));
				}
			}
			
			allFields.add(cleanFields);
		}
		if (skipHeaders) allFields.remove(0);
		return allFields;		
	}