How to setup a default value within a field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2020 12:21 PM
I would like to setup a default value for a field using scripting the simpliest way i.e 1234T56790. For example, I would like to place a "T" in the 5th position of the Transaction Code, so when the user submits a request, the 5th position of the Transaction Code is already populated with a 'T". The max length for the Transaction code is 10.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2020 01:25 PM
Hi,
You can use an onchange() client script that runs when field has value entered as below.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gr = g_form.getValue('new'); //replace new with variable or field name
if(gr.length=='10')
{
var newPhone=gr.substr(0,4)+'T'+gr.substr(4,6); //1234T567890 as output
g_form.setValue('new',newPhone); //replace new with variable or field name
}
else
{
alert('Enter 10 digits');
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2020 01:30 PM
Hi,
You can write a onChange script:
if(newValue){
var abc = newValue.substr(0,3)+'T'+newValue.substr(4,8);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2020 01:47 PM
Hi,
Setting a default value would put the value on load, before any user input. So, if you want to have an initial static value, you can then utilize default field values, but, if you want to insert a character in the user input string, and then use that as the field value, you can write a before insert business rule, and then do string manipulation to insert 'T' at your desired position in the user input string, and assign the updated sting to the field value.
For example, if you'd want to set an indicative default value on a new record form load, you can put this javascript on the default value of the field definition:
javascript:current.transaction_code_field_name.setValue(['XXXXXXXXX'.slice(0, 4), 'T', 'XXXXXXXXX'.slice(4)].join(''));
This will show XXXXTXXXXX on the new record form load.
If you want to insert 'T' on top of user input, set the maxlength of the UI component to 9, or put a length validation, so that the users can only enter up to 9 characters, and then create a before insert business rule, and in there, you can put in the below script on the advanced tab, to modify the value:
(function executeRule(current, previous /*null when async*/) {
var transaction_code = current.getValue('transaction_code_field_name');
if(transaction_code) {
transaction_code = [transaction_code.slice(0, 4), 'T', transaction_code.slice(4)].join('');
current.setValue('transaction_code_field_name',transaction_code);
}
})(current, previous);
Thanks & Regards,
Rishabh Jha
Aavenir(https://www.aavenir.com/)