Outbound Mapping Guide

Outbound mapping is the term to mean the outbound data flow from Salesforce to an external system. The outbound mapping process can begin with a user button click, an Apex trigger, or a Record-Triggered Flow. This guide will show how to create an outbound mapping process using a Screen Flow. The same steps apply to a Record-Triggered Flow.

 

This guide will show how to map an opportunity and its added products to an SAP BAPI for creating a sales order.

 

Guide

  1. Prerequisites
  2. Mapping Configuration
  3. Mapping Orchestration

Appendix

  1. Manually Create Input Records

The final flow with error handling and a user message.

Prerequisites

  1. A mapping specification created by the Salesforce and SAP domain experts.
  2. All custom fields and permissions added to the standard and custom objects. Permissions should be given to the technical/integration user too.
  3. An Integration Scenario created and activated for the SAP create sales order BAPI. (The create sales order BAPI requires the Commit Work Scenario option checked.) The examples in this guide will use "SalesOrderCreate".

The integration process is separated into two parts, what to map and when to map it: What fields are mapped and when should the mappings be executed. These are laid out in the sections Mapping Configuration and Flow Orchestration, respectively.

 

Mapping Configuration

For each Salesforce object to BAPI table/structure pair, we must create an outbound mapping configuration.

  1. From the Data Synchronization Scenario detail screen, click the New Object Mapping button.
  2. Specify a name for the mapping. This name is used to generate and deploy an Apex class specific to the mapping.
    When naming mapping configurations, it is recommended to apply a naming convention so that all generated Apex classes are given a similar name so they are grouped and thus easily identifiable and manageable. I.e., For the outbound mapping on the Opportunity object, it would be named "SalesOrderCreateOutOpp", which becomes "Mapping_SalesOrderCreateOutOpp.cls". This groups well with another one named "SalesOrderCreateOutProd".
  3. Set the direction to Outbound.
  4. Select the object to map from.
  5. Select the BAPI object to map to.
  6. Click Load Lists.
  7. Complete the field mapping. The mapping specification should be specified by the domain experts.
  8. The target foreign key field should not be specified because the BAPI records are not updated and are temporal.
  9. Active should be checked.
  10. Save the mapping and after the page reloads the generated mapping class should be available for any customizations with code.

 

Now create an inbound mapping configuration to handle the response from the BAPI.

  1. Specify the name like "SalesOrderCreateResponse".
  2. Set the direction to Inbound.
  3. Set the source object as the integration object named "BAPI_SALESORDER_CREATEFROMDAT2". This contains the sales document number for a successfully created order. (Because there is a character limit for custom object names, this name may be slightly different from the one generated for your Scenario.)
  4. Set the target object to Opportunity.
  5. Click Load Lists.
  6. Map the following two fields
    1. OutboundRecordId -> Id. (OutboundRecordId is a virtual field. It is the record ID of the original record that triggered the outbound mapping process.)
    2. SALESDOCUMENT__c -> The custom field for tracking the SAP document number.
  7. Save the mapping.
  8. After the new page reloads, it is recommended to add preprocessing code to this mapping to throw an exception if the BAPI returned any business errors.

 

Mapping Orchestration

Now with our mapping configurations specified, we can orchestrate when to run the mappings.

This guide assumes that you have mappings configured for at least Opportunity and Opportunity Products, so it will document how to run those mappings to demonstrate how to configure the Actions for running mappings on the current object and on objects related to the current object.

 

Go to Salesforce Setup -> Process Automation -> Flows -> New Flow -> Screen Flow -> Freeform.

  1. Create a resource for the record ID. This is set by the Flow framework when the Flow starts.
    1. Resource Type: Variable
    2. Name: recordId (Must match case exactly)
    3. Data Type: Text
    4. Default Value is not set. (For Record-triggered flows, set {!$Record.Id} )
    5. Availability Outside the Flow: Available for input is checked
  2. Add an Action to run the mapping for Opportunity.
    1. Drag Action onto the workspace.
    2. Click the Overcast category.
    3. Select Run outbound mapping.
    4. Label: Map Opportunity
    5. API Name: Map_Opportunity
    6. Scenario Name: SalesOrderCreate
    7. Scenario Input: Auto

  1. Add an Action to run the mapping for Opportunity Products. Use these steps for mapping other objects that are related to Opportunity.
    1. Select Run outbound mapping for related records.
    2. Label: Map Opportunity Products
    3. API Name: Map_Opportunity_Products
    4. Scenario Name: SalesOrderCreate
    5. Scenario Input: Auto
    6. Related Object: OpportunityLineItem
    7. Related Object Lookup: OpportunityId

  1. Add an Action to run the Integration Scenario.
    1. Select Run Data Synchronization Scenario.
    2. Label: Run scenario
    3. API Name: Run_scenario
    4. Scenario Name: SalesOrderCreate
    5. Operation: Read (SAP BAPIs only use Read, even though the BAPI saves data.)
    6. Scenario Input: Auto

 

  1. Connect the Actions with Run scenario last.

  1. Next, we will add a little error handling so as to not upset the user.
  2. Create a resource for holding the location of where the error occurred.
    1. Resource Type: Variable
    2. Name: ErrorLocation
    3. Data Type: Text
    4. Default Value is not set.
    5. Availability Outside the Flow is not set.
  3. Create an assignment for each Action, specifying the name of the Action

  1. Create a Screen to notify the user that the process completed. It will report any errors too.
    1. Drag a Screen onto the workspace.
    2. Give it a name.
    3. Add a Display Text component for the success message.
      1. The process to create the sales order in SAP started successfully.
      2. Set visibility to
        1. Resource: {!$Flow.FaultMessage}
        2. Operator: Is Null
        3. Value: {!$GlobalConstant.False} (This avoids a warning message)
    4. Add a Display Text component for the failure message.
      1. Starting the process to create the sales order in SAP failed.

        Error message: {!$Flow.FaultMessage}

        Error location: {!ErrorLocation}

      2. Set visibility with

        1. Custom Logic: NOT (1)

        2. Resource: {!$Flow.FaultMessage}

        3. Operator: Is Null

        4. Value: {!$GlobalConstant.False} (This avoids a warning message)

  2. When a fault occurs and the Scenario is not run, the records created in the mapping actions that preceded the fault will remain. It is a good idea to clean those up. We will use the Clear Data Synchronization Data Set Action for this.
    1. Drag an Action onto the workspace.
    2. Select Clear Data Synchronization Data Set.
    3. Select the Scenario Input output property from the first mapping operation in the flow, Map_Opportunity. The text should be {!Map_Opportunity.scenarioInput}.
    4. Enter all the API names of the custom objects that are used in this outbound mapping, separated by commas.
      *Note about the connections in the final image. The connections flowing into this Action do not include the Fault path for Map_Opportuntiy because if Map_Opportunity fails, there are no records created and, more importantly, {!Map_Opportunity.scenarioInput} will be NULL so it will cause this path to fail. Inputs into Actions must not be NULL.

  1. Complete the connections by creating a Fault Connector from each Action to each respective Assignment and connecting all assignments to the Screen. A Fault Connector is created by creating a second connection from the Action.

The connections flowing into Clear data set Action do not include the Fault path for Map_Opportuntiy because if Map_Opportunity fails, there are no records created and, more importantly, {!Map_Opportunity.scenarioInput} will be NULL so it will cause this path to fail. Actions inputs must not evaluate to NULL at run time.
The Fault path for the Run Data Synchronization Scenario Action will only be taken when the Scenario fails to start. Errors that occur during the Scenario run do not get raised here. They are logged in the Scenario run and an error notification will be sent when the Scenario run finishes.
  1. Save the Flow and test it.

Test it with the Debug button using an Opportunity record that is set with good test data for your SAP system.

If the Flow completes successfully, you should see the screen message, and in the Overcast Monitoring tab, you should see a Run/Mapping record completed and a Run/Read record started. Otherwise, the error message and location will be shown to the user.

After the Scenario Run completes, the inbound mapping SalesOrderCreateResponse should run.

 

This completes the outbound mapping configuration guide.

 

Appendix

Manually Create Input Records

Sometimes you may want to create records outside of the Scenario Object Mapping configuration if the records are fixed or unrelated to the data. For example, the BAPI or API for the Scenario needs some constant values. You could create an Object Mapping that targets the Integration Object and assign constant values to the fields. Alternatively, you could create records from the Flow using the standard Create Records element.

 

To create Scenario Input records using the Create Records element, the Name field must be set to the same value as Scenario Input in the Run Data Synchronization Scenario Action.

Run Outbound Mapping Only Action's output value should appear in the Value field's popup list. If it doesn't appear, check that the Action's Advanced checkbox, Manually assign variables, is not checked.