This is a script that will only set a field value if it is currently set to an empty value or is set to a specified default value. This is useful for blindly setting a value on a field without having to do the check work yourself.
If you use my ‘Script Manager’ form, I call this one ‘setIfEmpty’ and you load it as:
document.getFormNamed('Script Manager').runScriptNamed('setIfEmpty');
// ========== setIfEmpty Start ========== //
// NAME: setIfEmpty
// VERSION: 1.0
/**
* Set a field if it is currently empty or matches the default value.
*
* target: The record to use (TFFormEntry object)
* fieldId: The field ID (e.g. <code>fld-hash</code>) to set.
* value: The value to set in the field.
* defaultValue: The default value of the field.
*
* return: boolean true if set or boolean false if unset.
*/
function setIfEmpty(target, fieldId, value, defaultValue)
{
var current = target.getFieldValue(fieldId);
if ((!current || current == defaultValue) && current != value)
{
console.log('setIfEmpty passed for ' + fieldId + ', setting to: ' + value);
target.setFieldValue(fieldId, value);
return true;
}
else
{
console.log('setIfEmpty failed for ' + fieldId + ', skipping.');
return false;
}
}
// ========== setIfEmpty End ========== //
Simple example of how to use it:
setIfEmpty(record, 'fld-39ca9564ef2347ac93f933bc9a2316ac', result.fields.title, null);
setIfEmpty(record, 'fld-39379cdff743496f9a1ccbdc1ae56297', result.fields.quantity, 1);
The first has a default of null (TapForms’ default default value) and the second has a default of 1 which obviously is a configured value for a number field.