All insights

Agentforce

What Winter '27 adds to Agent Script: else-if, JSON, and string functions

The Winter '27 release closes two of Agent Script's sharpest gaps: multi-way branching with else if, and reading an action's JSON response without leaving the script. The new functions, and the patterns they unlock.

A few days ago I wrote up the Agent Script rules that stop your first file compiling. Two of the sharpest edges were missing pieces: no multi-way branch, and no way to read an action’s JSON output in the script itself. Winter ‘27 closes both.

The release adds else if chains and five expression functions: lower(), upper(), to_json(), from_json(), and json_path(obj, path, default). None of them change what Agent Script is, a deterministic language where behaviour is written in rules rather than coaxed out of a prompt. What they change is how much you can do before you have to call out to a Flow or an Apex action to handle structure the script couldn’t touch.

If you only adopt one thing, make it the JSON pair. The else if is a convenience. Being able to parse an action’s response and branch on a field, without a round trip through Apex, is the one that collapses whole subagents down to a few lines.

else if: the branch the language was missing

Agent Script gave you if and else, and a third outcome meant stacking separate if statements. Winter ‘27 adds else if, so a multi-way decision reads as one block instead of a column of unrelated conditions.

after_reasoning:
   if @variables.order_status is "shipped":
      transition to @subagent.tracking
   else if @variables.order_status is "cancelled":
      transition to @subagent.refund
   else if @variables.order_status is "pending":
      transition to @subagent.hold
   else:
      transition to @subagent.general

The win is more than fewer lines. Separate if statements each evaluate, so two of them could both fire if their conditions overlap, and you had to reason about that. An else if chain stops at the first branch that matches, which is the behaviour you wanted when you reached for a third branch in the first place. Route a case on its status, triage on a priority band, pick a path by channel: these were all awkward before and are now one readable structure.

from_json and json_path: reading an action’s answer in the script

This is the change with teeth. An Apex or External Services action hands back a value, and often that value is a JSON string: an order object, an API response, a nested result. Before, the script couldn’t look inside it. You either exposed every field as its own typed output on the action, or you wrote more Apex to pull the one value you needed. Both are work you did to route on a field.

from_json() parses a JSON string into an object, and json_path(obj, path, default) reads a value out of it with a fallback when the path isn’t there. Together they let the script treat an action’s raw response as data.

subagent order_lookup:
   description: "Look up an order and route on its status"
   actions:
      fetch_order:
         description: "Get the order as JSON"
         inputs:
            id: string
         outputs:
            raw: string
         target: "apex://FetchOrderAction"
   reasoning:
      instructions:->
         run @actions.fetch_order
            with [email protected]_id
            set @variables.order = from_json(@outputs.raw)
         set @variables.order_status = json_path(@variables.order, "$.status", "unknown")
         set @variables.carrier = json_path(@variables.order, "$.shipment.carrier", "none")

The default argument on json_path is the part to lean on. A missing field returns your fallback instead of blowing up the turn, so an action that sometimes omits shipment doesn’t strand the conversation. Set the status to "unknown" when it’s absent and let the else branch catch it, rather than assuming the field is always there.

A caution worth stating plainly: the exact path syntax and the edge cases live in the release notes and in Salesforce’s open-source Agent Script repository, which carries the current grammar, linter, and compiler. Check a nested-array path or an unusual key against the repo before you ship it, because a path expression that reads fine can still return your default for a reason you didn’t expect.

to_json: building the payload you hand an action

to_json() goes the other way, serialising an object into a JSON string. The use for it is assembling a structured payload in the script and passing it to an action as one input, instead of threading a dozen scalar inputs through the action’s interface.

set @variables.payload = to_json(@variables.order)
run @actions.log_event
   with [email protected]

It also helps when an action, a webhook, or a logging step wants a JSON body and you’d rather build it from variables you already hold than hand-concatenate a string. Keeping the serialisation in one function call is less error-prone than assembling braces and quotes by hand, and it keeps the action’s interface narrow.

lower and upper: comparing strings people don’t type the same way

The two string functions are small and you’ll use them constantly. People don’t type consistently, so a comparison against a raw user value fails on case alone. Normalising first fixes it.

after_reasoning:
   if lower(@variables.user_reply) is "yes":
      set @variables.confirmed = True
   else if lower(@variables.user_reply) is "no":
      set @variables.confirmed = False

Without lower(), “Yes” and “YES” slip past a check for “yes”, and you end up widening the condition or pushing the normalisation into an action. One function call at the comparison is cleaner and keeps the decision where you can read it.

What this release doesn’t change

The grammar is still strict and still deterministic. Indentation is structural, booleans are capitalised True and False, and the two transition syntaxes are still distinct. The new functions are additions to the expression language, not a loosening of the rules.

It also didn’t add arithmetic. There’s still no multiply, divide, or modulus operator, so real math belongs in a Flow or Apex action you call and read back, the same as before. The JSON functions make it easier to read a computed result out of that action’s response, which is the useful half of the pairing: do the math in Apex, return it as JSON, parse it in the script.

Two more Winter ‘27 items are worth a line for anyone building at scale. Multi-agent orchestration reached general availability in this release, and moving agents between orgs got simpler, which matters if you’ve been authoring through Agentforce DX and promoting from a sandbox. Both sit alongside the rest of the Winter ‘27 changes landing this month.

Where I’d use these first

Go find the subagent you built only to work around the JSON gap, the one that exposes ten outputs on an action because the script couldn’t read a nested field. Collapse it. from_json plus json_path turns that whole pattern into two set lines and a narrower action interface, and the action stops carrying a wide output contract you have to maintain.

Then replace your stacked if statements with else if where the outcomes are mutually exclusive, so the routing stops depending on conditions not overlapping by accident. Keep the compiler close while you do it, through DX, so a bad path expression or a mistyped function fails in seconds. The grammar is still unforgiving. It’s just doing more for you now.

Understanding the basics

Does Agent Script support else if now?

Yes. The Winter ‘27 release adds else if chains, so you can branch on more than two conditions in one block. An else if chain stops at the first matching branch, which is cleaner and safer than stacking separate if statements that could each fire when their conditions overlap.

How do I read an action’s JSON response in Agent Script?

Use from_json() to parse the JSON string into an object, then json_path(obj, path, default) to pull a value out of it with a fallback. The default argument returns your chosen value when the path is missing, so an action that sometimes omits a field doesn’t break the turn. Confirm the exact path syntax against Salesforce’s open-source Agent Script repository.

Can Agent Script do math in Winter ‘27?

No. The release added string and JSON functions, not arithmetic operators, so there’s still no multiplication, division, or modulus. Do the calculation in a Flow or Apex action, return the result, and parse it back with from_json and json_path.

What are the new Agent Script functions in Winter ‘27?

Five expression functions: lower() and upper() for case normalisation, to_json() to serialise an object to a JSON string, from_json() to parse a JSON string into an object, and json_path(obj, path, default) to read a value from a parsed object with a fallback. The release also added else if chains.


Porting a prompt-tuned agent into a deterministic script you can version and test, or collapsing the workaround subagents an older Agent Script version forced on you, is day-to-day work for our Agentforce practice. If your team is building pro-code agents and wants the patterns right, talk to us.

Keep reading

All insights