Tag: apex

Apex: Converting Lists to Sets, and Sets To Lists

As many Salesforce Apex programmers know, in Salesforce it’s pretty much an unchallenged best practice that all code should be able to handle bulk inserts, updates, etc. When adhering to this best practice, it’s really common to use sets, or lists as parameters in functions. Sometimes, there’s a need to convert between lists, or sets, or maybe even maps. Continue reading

How to read cookies set by JavaScript in a Salesforce Apex Class

How to read cookies set by JavaScript in a Salesforce Apex Class

I recently had a requirement to have JavaScript create a cookie and then read it in later by an Apex class. You would think you could simply create a cookie in JavaScript and then read it in Apex using the exact same name as the cookie, but that’s not exactly the case.

Cookies that need to be read by an Apex Class need to start with apex__ or they won’t be readable by Apex. One other thing to keep in mind is that if apex changes the cookies values they will be url encoded. For example, the @ symbol will have it’s value changed to the percentage sign and the hexadecimal version.

For this example, we’re assuming you want to set a cookie called “settings” to true as soon as the javascript loads. I’m using a standard pattern I’ve documented previously about using jQuery in Visualforce.


function j$(function() {

function setCookie(cookieName, value){
    var cookieString = cookieName + "=" + value;
    document.cookie = cookieString;

To access the values in apex, we would probably have some sort of functions like the below examples.

 	private string savedSettings = null;
 	public PageReference OnLoad()
 		savedSettings = getCookie('settings');

// Private method used to set the cookie value
	private void setCookieString(String cookieName, String value)
		Cookie ck = ApexPages.currentPage().getCookies().get(cookieName);
		ck = new Cookie(cookieName, String.valueOf(value),null,-1,false);
		ApexPages.currentPage().setCookies(new Cookie[]{ck});
	// Private method used to retrieve the cookie value
	private String getCookie(String cookieName)
		Cookie ck = ApexPages.currentPage().getCookies().get(cookieName);
	    return ck != null ? ck.getValue() : '';

Apex How to Get a List of Fields Set on an sObject

I recently had a requirement to dynamically determine what fields had been set or queried on a sObject, so that a comparison could be done on what was actually stored in Salesforce’s Datastore.

Surprisingly, there’s no way method on the sObject that let’s you tell what’s actually been queried or set. So without further ado, here’s the method that I decided to use because it’s much faster than doing a describe and catching every exception.

public class Utils {
	//Takes in an sObject and determines what values have been queried or set on the page.
	public static Set<String> getFieldsSet(sObject obj)
		if (obj == null)
			return new Set<String>();
		Map<String, Object> fieldValues = (Map<String, Object>) JSON.deserializeUntyped(JSON.serialize(obj));
		if (fieldValues.containsKey('attributes'))
		return fieldValues.keySet();