Defaulting Fields with Hard Coded and Dynamic Values
This demonstrates defaulting a text field to a hardcoded value, as well as querying a lookup field value for defaulting another field when creating new records.
Type: JavaScript
/**
* In Edit mode, set the default Sales Org to 'AT01', and the Agent to the Soldto Account's Agent on the Quote object
*/
jq(document).ready(function(){
if (readOnlyGrid==false) {
var simpleFieldsToDefault = {'Sales_Org__c': 'AT01'},
soldToAccountApiName = 'Soldto_Account__c',
agentApiName = 'Agent__c',
// key = field api name, value = column name
fieldsForAgentDefault = {'Soldto_Account__c':-1, 'Agent__c':-1};
for (var gridInfoKey in gridInfoMap) {
var gridInfoObj = gridInfoMap[gridInfoKey];
if (gridInfoObj.gridApiName == 'eQuote__c') {
var 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 (simpleFieldsToDefault[thisCol.fieldName]) {
// set the new default value
setDefaultValueForField(thisCol, gridInfoObj, simpleFieldsToDefault[thisCol.fieldName]);
} else if (fieldsForAgentDefault[thisCol.fieldName]) {
// set the field's column name
fieldsForAgentDefault[thisCol.fieldName] = thisCol.fieldId;
}
}
}
}
// setup the default behavior on new records for the Agent lookup field, which is based on the Soldto Account's Agent field
jq('#gbMainTable').on('change', 'tr.nr td[name="'+fieldsForAgentDefault[soldToAccountApiName]+'"] input', function() {
var thisInput = jq(this),
accountId = thisInput.attr('name'),
thisRow = thisInput.parents('tr.nr:first'),
agentInput = thisRow.find('td[name="'+fieldsForAgentDefault[agentApiName]+'"] input');
if (accountId !== undefined && agentInput.val().length == 0) {
// Soldto Account has been specified and Agent has not been set
// get the default Agent based on the Account
// note, this must be a JSONP request due to cross-domain security issues, since the GridBuddy package is on (gblite.visual.force...) and the custom vf is on (c.visual.force...)
jq.ajax({
url: getAjaxResponderURL() + '?reqtype=AccountAgent&accountId='+accountId+'&rowName='+thisRow.attr('name')+'&colName='+fieldsForAgentDefault[agentApiName],
dataType: 'jsonp',
jsonp: 'callback',
jsonpCallback: 'handleJsonpCallback'
});
}
});
}
function setDefaultValueForField(pMetaCol, pGridInfo, defaultValue) {
// get the name of the table we're in
var defaultValueTable = jQuery('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.showTextInput()) {
var defaultValueInput = defaultCol.find('input[type="text"]');
if (defaultValueInput.length > 0) {
defaultValueInput.val(defaultValue);
}
}
}
}
});
function getAjaxResponderURL() {
var currentLocation = window.location.href,
// replace the namespace with 'c', the namespace of custom vf pages
ajaxURL = currentLocation.replace('gblite.','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;
}
// public function called by the GridAjaxResponder VF
function handleJsonpCallback(data) {
if (!data) return;
var agentId = data.agentId || '',
agentName = data.agentName || '',
agentInput = jq('#gbMainTable').find('tr.nr[name="'+data.rowName+'"] td[name="'+data.colName+'"] input');
// set the lookup value on the Agent
agentInput.val(agentName).attr('name', agentId).change();
}