Tuesday, May 28, 2019

Capitalize the First Letter of Strings in a Field by Workflow (Example of using Formulas on Client-Side vs. Server-Side Triggers)

Certain fields like the First Name and Last Name fields on entity records should always contain values starting with a capital letter. To ensure this a Workflow Set Field Value action can be used. If a value is entered with only lowercase characters, then the Workflow capitalizes the first character.

Depending on the used trigger an SQL formula or a SuiteScript/JavaScript formula can be used to achieve this result. If the Set Field Value action is executing on a client trigger, then the specified formula must be in JavaScript/SuiteScript. If the Set Field Value action is executing on a derver trigger, then the formula must be in SQL. More information about the Set Field Value action can be found in SuiteFlow (Workflow) : Workflow Core Concepts : Workflow Actions : Set Field Value.

In the following parts the two scenarios are separately described.

Client Triggers

In the event in which the capitalization should happen, right after the field is edited, the client trigger After Field Edit can be used. In this scenario the formula is in JavaScript/SuiteScript.

A possible solution is to use the following JavaScript String methods:

  • str.charAt()
  • str.toUpperCase()
  • str.slice()

An example of a formula to capitalize the first letter of the First Name is the following:

{firstname}.charAt(0).toUpperCase()+{firstname}.slice(1)

The setup of the workflow Set Field Value for this example is the following:

  • Type: Set Field Value
  • Trigger On: After Field Edit
  • Client Fields: First Name
  • Field: First Name
  • Formula: {firstname}.charAt(0).toUpperCase()+{firstname}.slice(1)

Server Triggers

If the change should be performed on a server trigger, like Before Record Submit, then a SQL formula must be used. The INITCAP() SQL function returns the letter of each word in uppercase.

The formula to capitalize the first letter of each word in the Last Name is the following:

INITCAP({lastname})

The action's setup for this example is:

  • Type: Set Field Value
  • Trigger On: Before Record Submit
  • Field: Last Name
  • Formula: INITCAP({lastname})

Note: The approach presented for client side capitalizes only the first letter of the first word, whereas the approach presented for server side capitalizes the first letter of every word.

No comments:

Post a Comment