Open topic with navigation

TIBCO Business Studio™ - BPM Edition 5.4.0

# Multiple Instances of a BOM Class in a Process Data Field

If a process data field is flagged as being an Array Field when the process data field is created (or its properties are changed later), then instead of referring to a single Business Object, the process data field refers to multiple instances of the Business Object. This is done using a JavaScript array.

Let us consider a process data field that holds an Array of Customer objects called `custList`. The properties sheet for `custList` has Array selected and is of type Customer:

Array fields with a multiplicity greater than 1 are implemented using a JavaScript array.

The Array objects do not need to be created. They are created by default as empty Arrays. If you want to associate a particular Customer with the `custList` variable, you can assign the `cust` field with a single instance field. This is shown below.

    data.cust = factory.com_example_data.createCustomer();
    data.cust.name = "Fred Blogs";
    data.cust.custNumber = "C123456";

Another way is to add the new customer to the `custList`. We can add multiple customers to an Array as well, as shown below:

    // Using cust variable created above (Fred Blogs / C123456) is
    // added to the Array custList:
    data.custList.push(data.cust);
    // Now add a second customer to the Array (John Smith):
    data.cust2 = factory.com_example_data.createCustomer();
    data.cust2.name = "John Smith";
    data.cust2.custNumber = "C123457";
    data.custList.push(data.cust2);

This can be pictured as follows:

WARNING: If, after you used the script above, you then used the following script to add a third customer to the list, this would go wrong on two accounts.

    data.cust2.name = "Clint Hill";
    data.cust2.custNumber = "C123458";
    data.custList.push(data.cust2);

First, a new Customer instance has not been created for Clint Hill, so the first two lines above modify the John Smith Customer instance to refer to Clint Hill. Then when the `push()` method is called for the third time, it attempts to add a second reference to the same Customer instance. However, adding a second reference fails because the array type used does not allow the same object to be included more than once. So, the list ends up containing the Fred Blogs and Clint Hill Customer instances but not John Smith:

Instead, a Customer instance must be created using the factory method for each Customer to be added to the list. If references to the individual customer `cust` are not required outside of the script then local script variables can be used in place of the process variable `cust`. The example below shows two script local variables `c1` and `c2` being used to correctly add two Customers to a `custList`:

    // Create first customer instance
    var c1 = factory.com_example_data_createCustomer();
    c1.name = "Fred Blogs";
    c1.custNumber = "C123456";
    data.custList.push(c1);
    // Create second customer instance
    var c2 = factory.com_example_data_createCustomer();
    c2.name = "John Smith";
    c2.custNumber = "C567890";
    data.custList.push(c2);

It is not necessary to use different variable names for the two Customer instances, variable `c` could have been used throughout the script in place of `c1` and `c2`, but then the word `var` would have to be removed from the line that contains the second call to the `createCustomer()`:

    var c = factory.com_example_data_createCustomer();
    c.name = "Fred Blogs";
    c.custNumber = "C123456";
    data.custList.push(c);
    c = factory.com_example_data_createCustomer();
    c.name = "John Smith";
    c.custNumber = "C567890";
    data.custList.push(c);

Another way of creating the second and subsequent instances would be to make copies of the first. This can be useful if there are a lot of attributes with the same value, for example:

    // Create first customer instance
    var c1 = factory.com_example_data_createCustomer();
    c1.name = "Fred Blogs";
    c1.custNumber = "C123456";
    c1.isRetail = true;
    c1.dateAdded = new Date.now();
    data.custList.push(c1);
    // Create second customer instance by copying the first
    var c2 = bpm.scriptUtil.copy(c1);
    c2.name = "John Smith";
    c2.custNumber = "C567890";
    data.custList.push(c2);

We can use the same `var` to store the new customer once the first customer has been added to the Array. We do not need to keep a reference to it any longer:

    // Create first customer instance
    var cust = factory.com_example_data.createCustomer();
    cust.name = "Fred Blogs";
    cust.custNumber = "C123456";
    cust.isRetail = true;
    cust.dateAdded = new Date.createDate();
    data.custList.push(cust);
    // Create second customer instance by copying the first
    cust = bpm.scriptUtil.copy(cust);
    cust.name = "John Smith";
    cust.custNumber = "C567890";
    data.custList.push(data.cust);

Did you find this helpful?

Yes No
Great!
Thanks for taking the time to give us some feedback.
Sorry about that

Why wasn't this helpful? (check all that apply)

Could not find what I was looking for
Instructions confusing or unclear
Instructions did not work

Want to tell us more?

Please select at least one option or provide details about the issue!!

Error occurred while submitting feedback.

Error2 occurred while submitting feedback.

TIBCO Business Studio™ - BPM Edition

Copyright © 2015-2023. Cloud Software Group, Inc. All Rights Reserved.