Last Updated on November 17, 2023 by Sudhir
Salesforce themselves use the following code to convert the label that the user types when defining a filed to the API Name.
Given the following field HTML for the Label field:
<input id="MasterLabel" maxlength="40" name="MasterLabel" onblur="DeveloperNameInputElement.setName(this, document.getElementById('DeveloperName'), 'Field1');" size="20" type="text">
The JavaScript that’s executed by the onblur event handler looks like this:
function DeveloperNameInputElement() {
}
/**
* @param {string} text
* @param {Object} elem
* @param {string} val
* @return {?}
*/
DeveloperNameInputElement.setName = function(text, elem, val) {
text = text.value;
/** @type {string} */
var name = "";
/** @type {boolean} */
var e = false;
/** @type {boolean} */
var value = false;
//check to make sure that:
//elem (target field that is the API Name field) is not null,
//there is no text already present in elem
//there is a label defined in the master label field.
if (null !== elem && (0 == elem.value.length && 0 < text.length)) {
/** @type {number} */
//loop through each character of the label field value.
for (i = 0;i < text.length;i++) {
var ch = text.charAt(i);
//check if the current character is a letter or number
if ("a" <= ch && "z" >= ch || ("A" <= ch && "Z" >= ch || "0" <= ch && "9" >= ch)) {
// if current char is a number and this is at the beginning of the label, replace it with X
if (!e) { //only replace with X if it's a number AND it's the first character of the label
if ("0" <= ch && "9" >= ch) {
name += "X";
}
}
// if not a number, build the name
name += ch;
/** @type {boolean} */
// set e to true so that we don't replace any subsequent numbers with X
e = true;
/** @type {boolean} */
value = false;
}
else { //if the character is anything other than letter or number, do this. Example: space, !, #, @, etc.
if (e) {
if (!value) { //only add _ if value = false.
name += "_";
/** @type {boolean} */
value = true; //this ensures that consecutive special characters or spaces are not replaced with _ every time.
}
}
}
}
if (e) {
if (elem.maxLength) { //check if maxLength attribute is defined for the api name field
if (0 < elem.maxLength) { //check to make sure it's not 0
/** @type {string} */
name = name.substr(0, elem.maxLength); //truncate the string to max length
}
}
/** @type {string} */
// if value = true that means we ran into a non alpha-numeric character, so cut that out fo the API name
elem.value = value ? name.substring(0, name.length - 1) : name;
} else {
/** @type {string} */
//update the api name field
elem.value = val;
}
}
return true;
};
And here’s the version I personally use:
function setApiName(value) {
let _value = value;
if (apiNamePrefix && apiNamePrefix.length > 0) {
_value = apiNamePrefix + "_" + _value;
}
// remove non-alhpanumberic characters
_value = _value.replace(/[^\w\s\d]/gi, "_");
// replace 1 or more spaces with an underscore
_value = _value.replace(/\s+/g, "_");
//if field label starts with a number, replace with X
if (!isNaN(_value.charAt(0))) {
//remove first char and add X
_value = "X" + _value.substring(1);
}
// make sure there is only one underscore present.
_value = _value.replace(/_{2,}/g, "_");
// check to make sure the last character is not an underscore. If it is, replace it with nothing.
_value = _value.replace(/[_]$/, "");
// append C and give back to the text field.
_value += "__c";
return _value;
}