Updating XML-based information in entities

<< Click to Display Table of Contents >>

Navigation:  Bizagi Studio > Process wizard > Business Rules > Business rules examples > XML Helper object >

Updating XML-based information in entities

Overview

The XML Helper object is part of Bizagi's business rules API, and it provides data access for your processes when working with XML-structured information, as described at XML Helper object.

This section illustrates an example on how to use one of the methods featured by this object, the saveEntityAsString method, which is oriented to inserting or updating information directly in entities without having Xpath navigation.

This entails that such method's use is optimal when working with Parameter type entities.

 

Example

In the following sample process, assume we have a Parameter entity called Vendor.

Such entity has the following records and we would want to add one new record and update an existing one.

 

xmlhelper_ex1_vendors

 

This means exactly adding Samsung, and updating the name for Cisco to Cisco Systems.

 

xmlhelper_ex3_vendors2

 

To do this, it you should first look up the definition of the expected XML structure of your entity, by relying on the schema definitions created in Bizagi Studio:

 

xmlhelper_ex1_schema

 

For more information about predefining schemas, refer to Bizagi's data model XML schemas.

 

note_pin

You may rely on the XSD schema definition to generate an XML having the information while ensuring it is compliant, by relying on many available free services in the internet such as http://xsd2xml.com/.

However, consider that this is just a portion of what you need to send inside of the XML-structured string.

 

xmlhelper_ex3_xml

 

Wrap the previous portion of the Vendor XML content inside a <BizAgiWSParam> root element having an <Entities> sub-element inside:

<?xml version="1.0" encoding="utf-8"?><BizAgiWSParam><Entities>...</Entities></BizAgiWSParam>

 

This will ensure that you send out an input which complies to the same one used for the saveEntityAsString SOAP web method (when using Bizagi's web services API).

For more information about this expected structure and filtering options, refer to Save Entities.

 

Given that we will be updating one record in this entity, we will need to make sure that business keys definitions are set for that entity.

In other words, having defined in Bizagi Studio which attribute or attributes make up a unique constraint for each of the records.

In this example, Vendor records are identified uniquely by their Code:

 

xmlhelper_ex3_bizkeys

 

And for these records to be inserted and updated in the way shown in this section, it is advisable to unmark the checkbox in Bizagi Studio's configuration that enforces the use of business keys as a clause in XMLs:

 

xmlhelper_bizkeyEnforce

 

Now in order to save XML-compliant information into a given entity, you may rely on the CEntityXmlHelper.saveEntityAsString method.

Do this inside of an expression box, in a Bizagi business rule:

 

xmlhelper_ex3_rule

 

The overall expression shown in the image above is coded with the following:

var vendor_input = '<?xml version="1.0" encoding="utf-8"?><BizAgiWSParam><Entities><Vendor><Code>V006</Code>'
+ '<Name>Samsung</Name><VendorType>2</VendorType></Vendor>'
+ '<Vendor businessKey="Code=\'V005\'"><Code>V005</Code><Name>Cisco Systems</Name>'
+ '</Vendor></Entities></BizAgiWSParam>';
var vendor_result = CEntityXmlHelper.saveEntityAsString(vendor_input);
CHelper.trace("my_trace",vendor_result);

 

Notice that because a Vendor with Code V005 already exists and the Code attribute is that entity's definition of its business key, then such record will have its information updated (only what has been sent, which is the Name portion of information).

 

The CEntityXmlHelper.getEntitiesAsString method will return an XML-structured string with information having the IDs of newly created records or updated records, as presented below for this specific example:

 

xmlhelper_ex3_output

 

The sample response, as shown above, is:

<?xml version="1.0" encoding="utf-8"?>

<Entities>

 <Vendor>51</Vendor>

 <Vendor>52</Vendor>

</Entities>

 

note_pin

Notice in the example above, that CHelper.trace is being used as well (optionally), given that it will allow you to print out custom traces of values and responses in the execution of the rules.

For more information about the use of this tracing option, refer to Validating business rules.