Set a default picklist value on new records
Customize the object, the field, and the value for this to function correctly in your grid.
Type: Javascript
/**
* 1. Defaults the value of a picklist
*/
jq(document).ready(function(){
if (readOnlyGrid==false) {
defaultTheNameField();
}
function defaultTheNameField() {
// object api name to map of field api name to default value, Sample Account Object here
var fieldsToDefault = {
'Account':{ 'Type':'Press'}
//Object_API Name : 'Picklist_API_Name' : 'Picklist Default Value'
};
///// DON'T CHANGE ANYTHING BELOW THIS LINE /////
for (var gridInfoKey in gridInfoMap) {
var gridInfoObj = gridInfoMap[gridInfoKey];
if (fieldsToDefault[gridInfoObj.gridApiName]) {
var fieldsWithDefaultValues = fieldsToDefault[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];
setDefaultValueForField(thisCol, gridInfoObj, defaultValue);
}
}
}
}
}
function setDefaultValueForField(pMetaCol, pGridInfo, defaultValue) {
// 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.showTextInput()) {
// for text inputs
var defaultValueInput = defaultCol.find('input[type="text"]');
defaultValueInput.val(defaultValue);
}else if (pMetaCol.isTypePicklist()) {
// for picklists
defaultCol.find('select option:selected').val(defaultValue);
}
}
}
});