Contact Us 1-800-596-4880

Add a Condition to Your Flow Logic

logo cloud IDE Cloud IDE

logo desktop IDE Desktop IDE

Open Beta Release: The cloud IDE and AsyncAPI implementation support are in open beta. Any use of Anypoint Code Builder in its beta state is subject to the applicable beta services terms and conditions, available from the IDE.

In this section of the tutorial, you learn how to add a condition to your flow to send an email to the regional leader if the Salesforce case is escalated, otherwise your Mule application sends a message to Slack.

Define the Behavior for When the Case Is Escalated

Use a Choice router to define behaviors based on specific conditions. In this case, choose to send an email if the case is escalated.

  1. In Anypoint Code Builder, open your new-case-salesforce.xml file.

  2. Click the (Add component) icon for the Logger component:

    add choice router slack escalation canvas
  3. Type choice and select Choice under Flow Control:

  4. Add the <when/> and <otherwise/> elements:

    sync api choice when
    <choice doc:name="Choice">
       <when doc:name="When" >
    
       </when>
       <otherwise doc:name="Otherwise" >
    
       </otherwise>
    
    </choice>

    For better readability of your Mule flow, update the name of the <choice/>, <when/>, and <otherwise/> elements:

    <choice doc:name="Is Case Escalated?">
      <when doc:name="Escalated" >
    
      </when>
      <otherwise doc:name="Not Escalated" >
    
      </otherwise>
    </choice>
  5. Define the when branch to execute if the variable casestatus equals Escalated:

    <choice doc:name="Is Case Escalated?">
      <when doc:name="Escalated" expression='#[vars.casestatus == "Escalated"]'>
    
      </when>
      <otherwise doc:name="Not Escalated" >
    
      </otherwise>
    </choice>

Add an Email Connector

To configure the <when/> element, you must first configure operations from the Anypoint Connector for Email (Email Connector):

  1. In Anypoint Code Builder, open your new-case-salesforce.xml file, and add a new line under your <salesforce:sfdc-config/> element.

  2. Type smtp and select email:smtp-config:

    <email:smtp-config name="Email_SMTP">
      <email:smtps-connection host="${email.host}" user="${email.username}" password="${email.password}">
       <tls:context>
         <tls:trust-store insecure="true" />
       </tls:context>
      </email:smtps-connection>
    </email:smtp-config>
  3. Click the (Add component) icon after the Escalated section:

    add email operation slack integration canvas
  4. Type send and select Send from the Email section:

    send email slack integration canvas
  5. Configure send using the following code sample:

    <email:send config-ref="Email_SMTP" doc:name="Send Escalation Email" subject='#["Case " ++ vars.casenumber ++ " was escalated"]'>
      <email:to-addresses>
        <email:to-address value="${email.username}" />
      </email:to-addresses>
      <email:body contentType="text/html" >
        <email:content ><![CDATA[#["Please handle this case. " ++ payload]]]></email:content>
    </email:body>
    </email:send>
  6. To be able to test the application, temporarily add a <logger/> element inside your <otherwise/> element:

    <logger doc:name="Temporary Logger" message='#["Please look into this new Case: " ++ payload]'/>
  7. Examine your full Choice router configuration:

    <choice doc:name="Is Case Escalated?">
      <when doc:name="Escalated" expression='#[vars.casestatus == "Escalated"]'>
          <email:send config-ref="Email_SMTP" doc:name="Send Escalation Email" subject='#["Case " ++ vars.casenumber ++ " was escalated"]'>
                 <email:to-addresses>
                   <email:to-address value="${email.username}" />
                </email:to-addresses>
                <email:body contentType="text/html" >
                       <email:content ><![CDATA[#["Please handle this case. " ++ payload]]]></email:content>
                     </email:body>
               </email:send>
      </when>
      <otherwise doc:name="Not Escalated" >
        <logger doc:name="Temporary Logger" message='#["Please look into this new Case: " ++ payload]'/>
      </otherwise>
    </choice>

Test Your Mule Application

  1. To expedite the test, remove the breakpoint for your Logger component.

  2. Select Run > Start Debugging (F5).

  3. After your application deploys successfully, log in to your Salesforce account.

  4. From App Launcher, select Service:

    salesforce select service
  5. Select Cases > New Case:

    salesfroce create new case
  6. Ensure that the Status of the new case is Escalated.

  7. After a few seconds, ensure that your configured email account received an email with the information configured in the case:

    Please handle this case. Case Number: 00001030, Origin: Phone, Case Type: , Priority: Medium, Status: Escalated
  8. Proceed to Configure Slack Integration to learn how to configure the Slack message for any case that was not escalated.