In this post, we will review how to use Global Variables and scripting in SmartConnect.com to create document level after integration tasks.
In my case, for each record I was processing I needed to update a field in the source system when the record was successfully integrated into the destination. If this was a SmartConnect on premise integration, I would have used a Run Integration task after document success. However, this is not an option in SmartConnect.com.
I created a global variable to store document numbers of the records that had errors. Then, I used an after integration Run Integration task to update the source records. If the document number of the source record appears in the global variable, then we do not update that document in the source system.
Step 1: Create a global variable
- On the SmartConnect menu, select Maintenance > Global Variables
- Click +Create Global Variable
- Give the global variable a name.
- In my example, I am calling mine GBL_FAILED_CUSTOMERS because the process I am using it in is creating customer records in the target solution. The actual name you use doesn’t matter, but you will need to adjust the scripts later in the post to use whatever name you use here.
- In my example, I am calling mine GBL_FAILED_CUSTOMERS because the process I am using it in is creating customer records in the target solution. The actual name you use doesn’t matter, but you will need to adjust the scripts later in the post to use whatever name you use here.
- You do not need to enter a value in the Value field or Secure checkbox.
- Click Save
Step 2: Catch the documents with errors
Back on the original process, we need to add a task that will update our global variable with the ID of any records that fail. In my case, this is the Customer Number. Because we are using one variable to potentially capture multiple broken records, we need a delimiter between each ID. I have chosen a pipe delimiter (|) as I know that will not appear in my data. You can choose a different delimiter if you like, but just make sure that whatever you use won’t appear in your data naturally.
Note that I am actually creating 2 tasks here. The first will ensure that the global variable from above is set to an empty string. Technically this step is not required as we saved the variable as an empty string, but I like to do this step just in case.
- Open your integration process.
- Click the Tasks tab.
- Click on +Add Task.
- Give your task a name. In my example, I called it RESET_FAILED_RECORDS.
- Select Run Script as the Type.
- Select Integration Pre Tasks as the Stage.
- Enter the following code in the JavaScript Code box:
//Script to clear variable before processing this.GBL_FAILED_CUSTOMERS = ''; return true;
- Click Save.
- Click Add Task.
- Give your task a name. In the example, I called mine UPDATE_FAILED_RECORDS.
- Select Run Script as the Type.
- Select Document Post Failure Tasks as the Stage.
- Enter the following script in the JavaScript Code box.
//Script to concatenate failed customers this.GBL_FAILED_CUSTOMERS = this.GBL_FAILED_CUSTOMERS + this._companyName + '|' return true;
- Click Save.
- Click Save on the integration process to save your changes.
At this point, any time that a record fails, we will record its ID into a pipe delimited list in a global variable.
Step 3: Create a SmartConnect Process to update the source
In my example, I was updating a checkbox on the customer record in my source system to reflect that the integration had succeeded. This meant that I was able to create a process that used the exact same data source as my original integration, and then used the customer entity from that same system as the destination. This is important for the last step of the post.
Step 4: Create a dynamic Restriction
For the process you created in step 3, we need to stop it from processing records that had an error. To do this, we are going to be using the Restrictions feature.
- Within your process, select the Integration Tab.
- Select the Restriction subtab.
- Enter the following script in the JavaScript Code box:
//Restrict out records that failed to import //If the customer is not in the global variable it will not update the record in the target var customerArray = this.GBL_FAILED_CUSTOMERS.split("|"); if (customerArray.includes(this._companyName)) { return false; } else { return true; }
- Click Save on the integration to save your changes.
Step 5: Call the second process from the first
The last thing we need to do is call the process we just added from the original process. Because we want the update to run whether there is a problem or not, we need to add run integration tasks for both success and failure.
- In the original process, select the Tasks tab.
- Click +Add Task.
- Give the task a Name.
- In my example, I used NOTIFY_NS_OF_SUCCESS_S.
- Select Run Integration as the Type.
- Select Integration Post Success Tasks as the Stage.
- In the Integration to Run field, select the process that you created in Step 3.
- If your second process uses the same data source as the original process, click the Use data from parent checkbox. Do not check this if you used a different data source in your second process.
- Check the Use Parent Variables box. This will ensure that your list of error records is passed to the second process.
- Click Save.
- Click +Add Task.
- Repeat the above steps, except:
- Use a different name for this task. I used NOTIFY_NS_OF_SUCCESS_F.
- Select Integration Post Failure Tasks in the Stage field.
- Click Save.
- Click Save on the integration to save your changes.
Summary
In order to use a run integration task at the document level in SmartConnect.com we:
- Created a global variable to store the documents we did NOT want to run the second integration for.
- Updated the global variable using an after document task. In our case, we needed after document failure.
- Created the second integration we wanted to run.
- Added a restriction to the second integration that reads the global variable and stops records where the ID exists in the global variable from processing.
- Called the second map from the first as both an integration success and integration failure task.