Salesforce Query Functions

Oct27
  • Share/Bookmark

Difficulty: ★★★☆☆

These handy Salesforce functions I used a lot, for doing Salesforce database queries.
You can use these functions in your S-Controls:

/**
 * Get an array with all the possible fields names from a object
 * @param objectType object to get the fields from.
 * @return array with all the fieldnames of the given object
 */
function getFields(objectType) {
	var describeSObjectResult, fields, i;

	describeSObjectResult = sforce.connection.describeSObject(objectType);
	fields = [];
	var length = describeSObjectResult.fields.length;
	for (i = 0; i < length; i++)
	fields[i] = describeSObjectResult.fields[i].name;

	return fields;
}

/**
* Result handler, forwards you to the detail page,
* if there is an error give alert message.
* @param result - can be a (sf query) object, or a textmessage string.
* @param idVal - the id of the detailpage to forward to.
*/
function resultHandler(result, idVal){
	var msg = "";

	if (typeof(result) == "object") {
		if (result[0].getBoolean("success")) {
			msg = "";
		} else {
			msg = "Error: " + result[0];
		}
	} else {
		msg = result;
	}
	if (msg != ""){
		alert(msg);
	}
	self.location.href = "/" + idVal;
}


Leave a comment