Set a default record type in new records
There are two files required for this to function. Please review the comments at the top of each code file. Modifications are required for this to function correctly.
Type: Javascript
/* GLOBAL FILE - DEFAULT 'RECORD TYPE' FIELD VALUE ON A GRID (EMBEDDED/STANDALONE)
* This is one of the 2 extension (custom code) files the grid needs to have applied for this feature to be available.
* Please make this file type - Global Javascript. Apply this file as is and edit the other local file before applying on the grid
* Defaults lookup field searches to run on all Searchable Fields vs just the Name field
* This needs to run immediately, not on document ready. This is a global JS file you need to have one more local JS file to make it work
*/
(function defaultLookupSearchType() {
if (gridInfoMap) {
for (key in gridInfoMap) {
var thisGridInfo = gridInfoMap[key];
for (var i=0; i<thisGridInfo.metaColumns.length; i++) {
var thisCol = thisGridInfo.metaColumns[i];
if (thisCol.isTypeReference()) {
thisCol.searchFieldType = 'all';
}
}
}
}
})();
/**
* Helper module for defaulting values, called from multiple grids
*/
var GBDefaultValueHelper = (function() {
function setDefaultRecordTypeForObject(objectApiName, recordTypeApiName, recordTypeLabel) {
for (recordTypeId in objectRecordTypeIdToNames) {
if (objectRecordTypeIdToNames[recordTypeId] == recordTypeApiName) {
// use this record type id as the default
defaultRecordTypeIds[objectApiName] = recordTypeId;
break;
}
}
var objectToFieldMap = {};
objectToFieldMap[objectApiName] = { 'RecordTypeId': recordTypeLabel, 'RecordTypeIdValue': recordTypeId };
setFieldDefaults(objectToFieldMap);
}
function setFieldDefaults(mapOfObjectToFields) {
///// DON'T CHANGE ANYTHING BELOW THIS LINE /////
for (var gridInfoKey in gridInfoMap) {
var gridInfoObj = gridInfoMap[gridInfoKey];
if (mapOfObjectToFields[gridInfoObj.gridApiName]) {
var fieldsWithDefaultValues = mapOfObjectToFields[gridInfoObj.gridApiName],
thisCol;
// loop through the meta cols now and set the default for the specified fields
for (var i=0; i < gridInfoObj.metaColumns.length; i++) {
thisCol = gridInfoObj.metaColumns[i];
if (fieldsWithDefaultValues[thisCol.fieldName]) {
var defaultValue = fieldsWithDefaultValues[thisCol.fieldName],
recordTypeIdValue;
if (thisCol.fieldName == 'RecordTypeId') {
recordTypeIdValue = fieldsWithDefaultValues['RecordTypeIdValue'];
}
_setDefaultValueForField(thisCol, gridInfoObj, defaultValue, recordTypeIdValue);
}
}
}
}
}
// private function
function _setDefaultValueForField(pMetaCol, pGridInfo, defaultValue, defaultRecordTypeId) {
// get the name of the table we're in
var defaultValueTable = jq('table[name="new_'+pGridInfo.gridId+'"]');
if (defaultValueTable.length > 0) {
// get the column with the matching name
var defaultCol = defaultValueTable.find('td[name="'+pMetaCol.fieldId+'"]');
if (pMetaCol.isTypeReference() && pMetaCol.isRecordTypeField()) {
// record type
// in certain cases we need to set the record type id as the value, otherwise the default may not work
defaultCol.find('select option:selected').text(defaultValue).val(defaultRecordTypeId);
} else if (pMetaCol.isTypePicklist()) {
// for picklists
// special handling for the Currency (CurrencyIsoCode) field
if (pMetaCol.fieldName == 'CurrencyIsoCode' && defaultCol.find('select').length == 0) {
// salesforce doesn't expose the currency field on standard create new, so we need to create a picklist for it for defaulting
// otherwise the below is not necessary for any other picklist fields
// the None option is required by the GB js logic which assumes there will be None option generated by Salesforce and checks if the total options are greater than 1
defaultCol.append('<select><option>--None--</option><option selected="true"></option></select>');
}
defaultCol.find('select option:selected').val(defaultValue);
} else if (pMetaCol.showTextInput()) {
var defaultValueInput = defaultCol.find('input[type="text"]');
if (defaultValueInput.length > 0) {
defaultValueInput.val(defaultValue);
}
}
}
}
return {
setFieldDefaults: setFieldDefaults,
setDefaultRecordTypeForObject: setDefaultRecordTypeForObject
}
})(); // end GBDefaultValueHelper
/**
* GridBuddy Tracking code
*/
jq(document).ready(function() {
//contain these four and the functions and the call functions
trackGridView();
trackGridSave();
trackGridExport();
trackReorderHideColumns();
// tracks the grid view for this session
function trackGridView() {
var trackingURL = getAjaxResponderURL() + '?reqType=trackGridView&gname='+getParamValue('gname');
makeTrackingRequest(trackingURL);
}
// submits a tracking request when the user clicks the Save button and the form successfully submits
function trackGridSave() {
if (readOnlyGrid==false) {
var gbForm = document.forms[jq('form.gbForm').attr('name')];
jq(gbForm).submit(function() {
if (btnClick == 'save') {
if (jq.browser.mozilla) {
// the delay fixes a FF issue where the ajax call doesn't get submitted during the form submit
setTimeout(makeGridSaveTrackingRequest, 25);
} else {
makeGridSaveTrackingRequest();
}
}
});
}
}
// submits a tracking request when the user clicks the Export action
function trackGridExport() {
jq('div.gbPage').on('click', 'li.exportItem', function() {
var trackingURL = getAjaxResponderURL() + '?reqType=trackGridExport&gname='+getParamValue('gname');
makeTrackingRequest(trackingURL);
});
}
// submits a tracking request when the user clicks the Reorder/Hide Columns action
function trackReorderHideColumns() {
jq('div.gbPage').on('click', 'li.reorderColsItem', function() {
var trackingURL = getAjaxResponderURL() + '?reqType=trackReorderColumns&gname='+getParamValue('gname');
makeTrackingRequest(trackingURL);
});
}
function makeTrackingRequest(trackingURL) {
jq.ajax({
url: trackingURL,
dataType: 'jsonp',
jsonp: 'callback',
jsonpCallback: 'handleJsonpCallback'
});
}
function makeGridSaveTrackingRequest() {
var trackingURL = getAjaxResponderURL() + '?reqType=trackGridSave&gname='+getParamValue('gname')+'&fieldNames='+JSON.stringify(getModifiedFields());
makeTrackingRequest(trackingURL);
}
// returns an object mapping of Object API Name to an array of Field API Names, only for modified fields
function getModifiedFields() {
var modifiedFields = {},
tempObjectToFields = {};
for (var rowId in modData) {
var rowData = modData[rowId],
fullyQualifiedObjectName = rowData['nm'],
gridInfo = (getParentObjectName() == fullyQualifiedObjectName ? getParentGridInfo() : getChildGridInfo(fullyQualifiedObjectName));
for (var key in modData[rowId]) {
if (isFieldCell(key)) {
var metaCol = gridInfo.getMetaColByFieldId(key),
fieldName = metaCol.fieldName;
if (!tempObjectToFields[fullyQualifiedObjectName]) {
tempObjectToFields[fullyQualifiedObjectName] = {};
modifiedFields[fullyQualifiedObjectName] = [];
}
if (!tempObjectToFields[fullyQualifiedObjectName][fieldName]) {
tempObjectToFields[fullyQualifiedObjectName][fieldName] = true;
modifiedFields[fullyQualifiedObjectName].push(fieldName);
} else {
// this field has already been added, skip it to avoid dupes
}
}
}
}
return modifiedFields;
}
function getAjaxResponderURL() {
var currentLocation = window.location.href,
// replace the namespace with 'c', the namespace of custom vf pages
ajaxURL = currentLocation.replace('gblite.','c.').replace('gblitesandbox.', 'c.');
// changed from ajaxURL.indexOf('Grid?') so that tracking still works on embedded grids that don't have the right case in the URL for "grid"
ajaxURL = ajaxURL.substring(0, ajaxURL.toLowerCase().indexOf('grid?'))
+ 'GridAjaxResponder';
return ajaxURL;
}
function getParamValue(paramName) {
var urlParams = window.location.search,
paramValue = '';
if (urlParams) {
var paramArray = urlParams.split('&');
jq.each(paramArray, function(i, item) {
if (item.match(paramName + '=')) {
paramValue = item.substring(item.indexOf('=') + 1);
return false;
}
})
};
return paramValue;
}
});
// public function called by the GridAjaxResponder VF
function handleJsonpCallback(data) {
// no op
}
This is the second file required for the Default Record Type extension to function. Modifications are required for this to function correctly.
Type: Javascript
/**
* (objectApiName, recordTypeApiName, recordTypeLabel)
*/
jq(document).ready(function() {
GBDefaultValueHelper.setDefaultRecordTypeForObject('Opportunity', 'US_Cement', 'US Cement');
});