Dylan Andersen's DocsDylan Andersen's Docs
Working with AgentforceThe New Agentforce BuilderAgent Script

Conditional Expressions, Oh My!

Conditional Expressions let you deterministically specify what actions to take or which prompts to include based on the current context

Conditional Expressions

Conditional Expressions let you deterministically specify what actions to take or which prompts to include based on the current context. Agent Script supports if and else logic.

Conditional Expressions hero

Basic Syntax

if condition:
  -> action or statement
else:
  -> alternative action

Checking Variables

Simple Conditions

if @variables.is_verified:
  -> run @actions.get_account_details
  | Here are your account details.

if @variables.retry_count == 0:
  -> run @actions.send_welcome_message

Null Checks

if @variables.email_address is not None:
  -> run @actions.send_email
  | I've sent you an email.

if @variables.customer_id is None:
  | Please provide your customer ID to continue.

Comparisons

if @variables.age >= 18:
  -> set @variables.is_adult = True

if @variables.balance < 0:
  -> transition to @subagent.payment_required

if @variables.status != "complete":
  | Your request is still being processed.

If/Else Logic

if @variables.is_premium_member:
  -> set @variables.discount = 20
  | As a premium member, you get 20% off!
else:
  -> set @variables.discount = 0
  | Sign up for premium to get exclusive discounts.

Setting Variables Conditionally

if @variables.order_total > 100:
  -> set @variables.free_shipping = True
else:
  -> set @variables.free_shipping = False

Conditional Prompts

Include different prompts based on conditions:

reasoning:
  instructions:
    if @variables.is_new_customer:
      | Welcome! It looks like this is your first time here.
      | Let me help you get started with your account.
    else:
      | Welcome back! How can I help you today?

Compound Conditions

Use and, or, and not to combine conditions:

# AND condition
if @variables.is_verified and @variables.has_payment_method:
  -> run @actions.process_order

# OR condition  
if @variables.is_admin or @variables.is_manager:
  -> set @variables.can_approve = True

# NOT condition
if not @variables.terms_accepted:
  | Please accept the terms of service to continue.

# Combined
if @variables.is_verified and (not @variables.account_locked):
  -> transition to @subagent.account_management

Practical Examples

Verification Flow

reasoning:
  instructions:
    if @variables.verification_code is None:
      | Please enter the verification code sent to your email.
    else:
      -> run @actions.verify_code
      if @actions.verify_code.outputs.is_valid:
        -> set @variables.is_verified = True
        | Great! Your email has been verified.
      else:
        -> set @variables.attempt_count = @variables.attempt_count + 1
        | That code doesn't match. Please try again.

Retry Logic

reasoning:
  instructions:
    if @variables.attempt_count >= 3:
      | You've exceeded the maximum attempts.
      -> transition to @subagent.escalation
    else:
      | Please try again. You have {!@variables.remaining_attempts} attempts left.

Feature Access

before_reasoning:
  if @variables.subscription_tier == "free":
    -> set @variables.max_exports = 5
  else:
    -> set @variables.max_exports = 100
  
  if @variables.exports_used >= @variables.max_exports:
    -> transition to @subagent.upgrade_prompt

Limitations

No else if

Agent Script currently supports if and else, but does not support else if (elif) after an if statement.

Workaround for Multiple Conditions

Instead of else if, use nested or sequential if statements:

# Instead of else if, use multiple if statements
if @variables.status == "pending":
  | Your order is pending.
  
if @variables.status == "shipped":
  | Your order has shipped!
  
if @variables.status == "delivered":
  | Your order has been delivered.

Or use nested logic:

if @variables.status == "pending":
  | Your order is pending.
else:
  if @variables.status == "shipped":
    | Your order has shipped!
  else:
    | Your order has been delivered.

On this page