JavaScript examples for various use cases

1. How to get a single character to repeat n times in a calculated field.

Javascript: '0'.repeat(1396)

**2. Add a calculated field to display the Row number, and the length should be 9 characters padded with zeros.

**Javascript: String(rowNo()).padStart(9, '0')

3. Add a calculated field to display the Amount value, and the length should be 10 characters padded with zero. Additionally, if there are decimal values, remove the decimal point.

Javascript: ('0000000000' + Math.round({!Amount} * 100)).slice(-10)

4. Add a calculated field to display date in the format, 0YYDaynumber, example: July 30, 2025 should display as 025211

Javascript: ('0' + new Date({!Date}).getFullYear().toString().slice(-2) + ('000' + Math.floor((new Date({!Date}) - new Date(new Date({!Date}).getFullYear(), 0, 0)) / (1000 * 60 * 60 * 24))).slice(-3))

Note: Date is a query field.

5. Add a calculated field to display the Account name with empty spaces padded such that the total length of the field value is always 30.

Javascript:
function (s) { 

    s = (s || "").toString(); 

    var pad = 30 - s.length;

    if (pad > 0) return "\u00A0".repeat(pad) + s;

    return s.substring(0, 30);

}({!Account Name})

Note: Account Name is a query field.

**6. Display the table row count, and make the length of the field value 9 with additional zero padding, We have used Conditional logic in the word template to generate this output. Given, the count is not greater than 999.

**«IF Table1:Count < 10»00000000«Table1:Count»
«ELSE IF Table1:Count < 100»
0000000«Table1:Count»
«ELSE»000000«Table1:Count»
«ENDIF»

Note: Table1 is the record group name.