Skip to main content
Build the future with Agentforce at TDX in San Francisco or on Salesforce+ on March 5–6. Register now.

Calculate Values in Variables and Formulas

Learning Objectives

After completing this unit, you’ll be able to:

  • Concatenate two text values in a flow.
  • Add and subtract numerical and date values in a flow.
  • Create a formula to calculate dynamic values in a flow.
  • Explain why you might use a formula in a flow.
Note

This badge is one stop along the way to Flow Builder proficiency. From start to finish, the Build Flows with Flow Builder trail guides you through learning all about Flow Builder. Follow this recommended sequence of badges to build strong process automation skills and become a Flow Builder expert.

Manipulate Data with the Assignment Element

The Assignment element can also change existing variable data instead of fully replacing it.

Combine Text

When the data type of a variable is text, you can select Add as the Assignment element’s operator. The Add operator lets you add text to the end of the current value.

The Set Shipping Address flow is working pretty well, but now Flo wants to know how current the shipping address is. In the Copy Address assignment element, let’s add a row that appends a note to the account’s Description field, indicating the date when the shipping address was added to the account. We don’t want to replace what’s already in the Description field, just add a note to the end. However, keep in mind that Salesforce can’t combine text variables with non-text resources, such as date variables or number fields. To add non-text data, create a formula that uses the TEXT() function to convert the non-text value to text.

First, let’s create a formula that contains the added text.

  1. Click Toggle Toolbox to display the Toolbox.
  2. Click New Resource.
  3. For Resource Type, select Formula.
  4. For API Name, enter frmChangeDateNote.
  5. For Data Type, select Text. (Even though we’re adding a date, the end result has to be the Text data type so we can add it to a text field.)
  6. Enter this text in the Formula field:
    " Shipping Address added on " & TEXT()
  7. Place the cursor within the parentheses ( ).
  8. In the Insert a resource… field, select $Flow > Current Date.
  9. Click Check Syntax.
    The syntax checker is especially helpful spotting errors in long, complex formulas with nested functions.
    The formula field corresponding to the preceding steps
  10. Click Done.

Before we move on to the next step, let’s pause and take a look at a few things in that formula. 

" Shipping Address added on " & TEXT({!$Flow.CurrentDate})
  • Notice the spaces at the beginning and end of the phrase in quotation marks. The spaces separate the new phrase from text that’s already in the Description field and from the date that follows. For example, without a space at the end, the flow would set the description to “Shipping Address added on03-02-2023.
  • The & tells the formula to combine the text before and after, similar to what we’re doing with the assignment’s Add operator.
  • The TEXT() function converts what’s inside it to text so that it can be used in a text field.

Next, add the text addition line to the Copy Billing Address assignment.

  1. Click the Copy Billing Address element and select Edit Element.
  2. Click + Add Assignment.
  3. In the Variable field, select Account from Get Account Data, then select Account Description. We set the account variable’s Description field here because it’s the variable that’s changing.
  4. In the Operator field, select Add.
  5. In the Value field, select frmChangeDateNote. We select the formula here because it contains the text value we’re adding to the description.
    The variable, operator, and value corresponding to the preceding steps.
  6. Save the flow.

Now, when the Update Records element updates the account, the update includes the altered Description.

Note

If you’re using an assignment just to combine two texts without any conversion, you don’t need a formula to add a space between the old and new text. You can manually add a space before the new text in the Value field. For example, with a variable named “Answer”, you can enter “ {!Answer}” directly in the Value field.

Perform Simple Calculations

You can also use the Add operator to add to a variable with the number or currency data type. However, this performs actual math; if you combine the number values 4 and 2, you get 6, not 42. You can also use the Subtract operator to subtract from number and currency variable values.

For example, this Assignment element subtracts a discount from an opportunity’s amount. The opportunity variable’s Amount goes in the Variable field, because that’s the value that’s changing. The Operator is set to Subtract. Finally, the variable with the discount amount goes in the Value field.

The New Assignment side panel corresponding to the preceding and following descriptions.

But check out that second line: it’s adding to a date field. You can also use the Add and Subtract operators to add or subtract days from a date value. This Assignment element adds 2 days to the opportunity’s close date.

Calculate Changes Automatically with Formulas

When you created the frmChangeDateNote formula, the & symbol combined two texts, functioning the same way the Add operator works in an assignment. So why didn’t we use the & symbol to attach the date directly to the Description field? The & symbol works only in formulas, and Description is a long text field. In flow formulas, Salesforce doesn’t allow long text fields because they can easily exceed the calculation limit for a formula.

This restriction is an example of the limits and complications with formulas in flows. Flow formulas have limited calculation time and a limited number of characters, which makes complex formulas inherently risky. Plus, unlike variables, formulas can’t be changed mid-flow. So when is it a good idea to use a formula?

Think of formulas like those other screwdrivers that come with your toolkit, the ones with the tips in shapes you’ve never seen before. You may not use them much, but when you actually encounter a screw with one of those tips, you’re glad you have that special tool! In other words, as a general rule, you should use a formula when you’re in a situation that only a formula can resolve.

Flo Smith holding an oddly shaped screwdriver with a quizzical look.

The frmChangeDateNote formula is a great example that follows that rule. We can’t use just an Assignment element to attach the date to the description because the date has to be converted to a text value first. We need the formula’s TEXT() function to do the conversion.

Resources

在 Salesforce 帮助中分享 Trailhead 反馈

我们很想听听您使用 Trailhead 的经验——您现在可以随时从 Salesforce 帮助网站访问新的反馈表单。

了解更多 继续分享反馈