Overview

Skill Level: Beginner

In IBM BPM coachviews modify objects slightly as compared to a traditional JS object. In this tutorial, we will use a cleansing function to ensure proper transmission to serverside such as an AJAX call to ensure business objects are processed as expected.

Ingredients

  • BPM Instance
  • A CSHS with a Coachview
  • A Service Flow or AJAX Service that the coachview calls to run processing on the server side
  • A Business Object with at least one attribute or sub-attribute of type list

 

 

Step-by-step

  1. Initial Setup

    In the following tutorial I will walk you through a sample use case to demonstrate this functionality and a piece of recursive code to clean the object.

     

    1. Ensure proper objects are created that have some nesting and list complexities. Here is an example of the mapping/defaulting I do for my object

    var autoObject = {};
    autoObject.obj1 = {};
    autoObject.obj1.name = "Joe";
    autoObject.obj1.age = "24";
    autoObject.location = "USA";
    autoObject.obj2 = {};
    autoObject.obj2.food = "Sushi";
    autoObject.obj2.pet = "None";
    autoObject.obj2.obj1 = {};
    autoObject.obj2.obj1.name = "Bryan";
    autoObject.obj2.obj1.age = "23";
    autoObject.obj2.obj1L = [];
    autoObject.obj2.obj1L[0] = {};
    autoObject.obj2.obj1L[0].name = "Someone";
    autoObject.obj2.obj1L[0].age = "30";
    autoObject.obj3 = [];
    autoObject.obj3[0] = "SimpleString";

    autoObject.listCompArr = [];
    autoObject.listCompArr[0] = {};
    autoObject.listCompArr[0].food = "Pizza";
    autoObject.listCompArr[0].pet = "Dog";
    autoObject.listCompArr[0].obj1 = {};
    autoObject.listCompArr[0].obj1.name = "Sam";
    autoObject.listCompArr[0].obj1.age = "32";
    autoObject.listCompArr[0].obj1L = [];
    autoObject.listCompArr[0].obj1L[0] = {};
    autoObject.listCompArr[0].obj1L[0].name = "Wayne";
    autoObject.listCompArr[0].obj1L[0].age = "18";
    autoObject.listCompArr[0].obj1L[1] = {};
    autoObject.listCompArr[0].obj1L[1].name = "Jordan";
    autoObject.listCompArr[0].obj1L[1].age = "23";

    autoObject.listCompArr[1] = {};
    autoObject.listCompArr[1].food = "Salad";
    autoObject.listCompArr[1].pet = "Cat";
    autoObject.listCompArr[1].obj1 = {};
    autoObject.listCompArr[1].obj1.name = "Matt";
    autoObject.listCompArr[1].obj1.age = "27";
    autoObject.listCompArr[1].obj1L = [];
    autoObject.listCompArr[1].obj1L[0] = {};
    autoObject.listCompArr[1].obj1L[0].name = "Noah";
    autoObject.listCompArr[1].obj1L[0].age = "19";
    autoObject.listCompArr[1].obj1L[1] = {};
    autoObject.listCompArr[1].obj1L[1].name = "Diego";
    autoObject.listCompArr[1].obj1L[1].age = "23";


    tw.local.thing = autoObject;

    Screen-Shot-2019-02-17-at-12.36.08-AM

     

    2. Ensure that you are able to call a service. In my example this is a service call that we execute by passing a JSON string into. In this use case, I bind a tw object to a coachview of type data, retrieve that coachview in the onclick of a button, stringify it, and pass it into my service call which I execute

    Screen-Shot-2019-02-17-at-12.42.58-AM

     

    3. Please test a sample request to our service call that recieves a string on the serverside.

     

     

  2. Cleansing Code

    Now that we have proper connectivity, we can cleanse our object with a recursive function. If you look into the JSON string we send, lists are processed as complexes by IBM BPM. Below I will put the code I use to cleanse


    function listComplexCleanser (obj){

    //Loop through each attribute in the obj
    for(var propertyName in obj) {
    //if obj is complex
    if( typeof obj[propertyName] == 'object'){
    //loop through each attribute
    var attribute = obj[propertyName];
    log.info('Checking ' + propertyName);
    //If it's complex and has attribute items and items is a list, set the list equal to parent
    if(attribute && attribute.items && attribute.items.length != undefined){
    log.info('This attribute of ' + propertyName + ' is an array');
    obj[propertyName] = attribute.items;
    log.info(propertyName + ' has been cleansed');
    //loop through each object
    for(var i=0; i < obj[propertyName].length;i++){
    obj[propertyName][i] = listComplexCleanser(obj[propertyName][i]);
    }

    }
    else{
    //Recursive func through objects
    obj[propertyName] = listComplexCleanser(obj[propertyName]);
    }
    }
    }
    log.info('Cleansed Object');
    log.info(JSON.stringify(obj));

    return obj
    }

     This function above will cleanse our object (passed in as JS Object, not JSON), and resettle it as a proper JS Object. Please note that this function is recursive, PLEASE USE CAUTION AS THIS MAY CAUSE AN INFINITE LOOP.

    You may utilize a function like this to turn a Coachview JS Variable into a nice neatly formatted TWObject after sending it as a JSON String

    Screen-Shot-2019-02-17-at-12.51.40-AM

     

    Note here that I have uploaded the above function as a serverFile to call for reusability purposes. You may add the JS directly to the script and call the function directly.

Join The Discussion