Object

◷ Reading Time: 7 minutes

new

This is a global function allowing the creation of a new instance from a registered and defined non-generic type. The type must be registered in context and must have a default constructor. Please note that if the Type is generic, newOf must be used.

 new (Type)
Let's assume a type of Book is registered. 

Example: b = new (Book)
Result: assigns a new instance of Book to parameter named b

When Type is not provided, then a FlexRule dynamic object is created.

 new ()

The dynamic object allows the building of nested values and structure simply by specifying the name of its properties using the |key. Dynamic object automatically creates a nested structure as new nested properties are assigned.

 new (properties)

When a list of properties are passed to a new function as an array then an object is created with all the properties and a null value.

Example: new(['Name','Family']) 
Result: creates a new instance of object with 2 properties that are null.

newOf

This is similar to new, but it creates an object from a generic type.

 newOf (Type, ArgType1, ArgType2...)

For example, let’s assume type Book is registered, and we also have registered a System.Collections.Generic.List<> as a GenericList name.

Example: newOf (GenericList, Book)
Result: creates a new instance of List<Book>. A generic list that can hold Book objects.

toJson

Creates JSON object from a source input.

 |toJson (defaultNamespace, autoPrefix)
  • source (object): string, XML or any other object type (Mandatory)
  • defaultNamespace: the prefix of default namespace to discover and register namespaces automatically (Optional)
  • autoPrefix: true to use xpath prefixed with default namespaces, default is false (Optional)
Example:
Source:= {
    "book" : [
        {
            "author" : "Gambardella, Matthew",
            "title" : "XML Developer\u0027s Guide",
            "discount" : "0"
        },
        {
            "author" : "Ralls, Kim",
            "title" : "Midnight Rain",
            "discount" : "0"
        }
    ]
}

source|toJson () 
Result: Converts the string to JSON and returns a JSON object as follows.
{
    "book" : [
        {
            "author" : "Gambardella, Matthew",
            "title" : "XML Developer\u0027s Guide",
            "discount" : "0"
        },
        {
            "author" : "Ralls, Kim",
            "title" : "Midnight Rain",
            "discount" : "0"
        }
    ]
}

proxy

A Proxy object allows access to members (property and Methods) of objects with automatic translation between required types and native complex objects.

 proxy (instance)
  • instance (required): The instance of an actual object

Proxy is a smart object that knows how to translate between native complex values and static types.

Example: 

target := {
   message1: "hello",
   message2: "hi"
 }

handler:= proxy (target)

handler.message1

Result: hello

soapClient

It creates a Proxy based on the WSDL address.

 soapClient (name, wsdlUrl, serviceEndpointUrl)
  • name (required): name of the service. This is used to cache the client (Proxy).
  • wsdlUrl (required): address of the service that provides WSDL information
  • serviceEndpointUrl: When service operation endpoint Url is different from the WSDL, it can be set using this parameter. Or the Url property of the client can be set separately.
  • returns: It returns a proxy instance to communicate to service. The proxy object has the attributes shown below:
    • Url [string]: endpoint address of the service to call
    • Timeout [int]: number of milliseconds for service call timeout (default 100,000)

Example:

p = soapClient ('CheckQuantity', 'http://www.services.com/api?wsdl', 'http://www.services.com/api/v2')

products = p.GetProducts(['p1', 'p2'])

hasProperty

Determines whether an object has a property or attribute. It returns a boolean value.

 |hasProperty (properties)
  • properties (String): A property or a list of properties (Mandatory)
Example: 

Students := [
     {
         "ID" : 3,
         "Name" : "Clark",
         "Family" : "Kent"
     },
     {
         "ID" : 2,
         "Name" : "Peter",
         "Family" : "Parker"
     }
 ]

 " fn="|hasProperty" right="('Name')"]

Return: True
Example: 

Students := [
     {
         "ID" : 3,
         "Name" : "Clark",
         "Family" : "Kent"
     },
     {
         "ID" : 2,
         "Name" : "Peter",
         "Family" : "Parker"
     }
 ]

 " fn="|hasProperty" right="('Name', 'ID')"]

Return: True
Example: 

Teacher := { "ID" : 3, "Name" : "Clark"}

Teacher|hasProperty ('Subject')

Return: False

property

Reads the value of an object’s property.

 |property (property)
  • property (string): the property/attribute name
Example: 

Teacher := { "ID" : 3, "Name" : "Clark"}

Teacher|property ('Name')

Return: Clark
Example: 

Groups := { 1: 'A', 2: 'B', 3: 'C'}

Groups|property (2)

Return: B

properties

Reads all object’s property.

 |properties ()
  • properties ( ): the number of properties
Example: 

People := { "Name" : 'James', "age" : 45}

People|properties ()

Return: 2

setProperties

Sets the values of object’s properties and returns the object.

property |setProperties  (list)
  • property (Object): Prperty name (Mandatory)
  • list (Required): list of name:value separated with comma (Mandatory)
Example: 

person := {}

person |setProperties (Name:'Arash', Email:'test@test.com') 

Return:
{
     "Name" : "Arash",
     "Email" : "test@test.com",
}
Example: 

person := {
     "Name" : "Arash",
     "Email" : "test@test.com",
} 

person |setProperties (ID:12345, Membership:true) 

Return:

{
     "Name" : "Arash",
     "Email" : "test@test.com",
     "ID" : 12345,
     "Membership" : true
 }

attribute

Reads the value of an object’s property and returns its value.
(Similar to functions/object#proprty)

object |attribute (attribute)
  • object: Name of the object (Mandatory)
  • attribute: the attribute of the object to get the value from (Mandatory)
Example: 

person := {
     "Name" : "Alex",
     "Age" : 26
 } 

person |attribute (Age) 

Return: 26

attributes

Check properties.

setAttributes

Sets the values of object’s attributes.
(Similar to functions/object#setproperties)

 |setAttributes  (list)
  • list (Required): list of name:value separated with comma (Mandatory)
Example: 

person := {}

person |setAttributes  (Name:'Arash', Email:'test@test.com') 

Return:
{
     "Name" : "Arash",
     "Email" : "test@test.com",
} 
Example: 
person := {
      "Name" : "Alex",
      "Age" : 26
  } 

person |setAttributes  (ID:12345, Membership:true) 

Return:

{
     "Name" : "Alex",
     "Age" : 26,
     "ID" : 12345,
     "Membership" : true
 }  

maybe

Allows conditional access to an object’s members. If the object is null, the result would be null or a default value that can be set.

 |maybe (localName, expression, defaultIfNull)
  • localName: local variable (Mandatory)
  • expression: Expression to be checked (Mandatory)
  • defaultIfNull: A default value to be returned id the property value is null (Optional)
Example: 

people := 
[
     {
         "Title" : null,
         "Age" : 26
     },
     {
         "Title" : "Mr",
         "Age" : 32
     }
 ]

people |first() |maybe (x, x.Title, 'This is null')   

Return: This is null 


This returns the Title of the first item in people object. As Title is null, it will return the message 'This is null'.
Example: 

people := 
[
     {
         "Title" : null,
         "Age" : 26
     },
     {
         "Title" : "Mr",
         "Age" : 32
     }
 ]

people |first() |maybe (x, x.Age>10)   

Return: True 

merge

Merges multiple objects to a one object.

 |merge ()
Example: 

PersonalData := 
{
   "Name" : "Alex",
   "Age" : 32
}
 
EducationData :=
{
   "Highest Education": "Grade 12",
   "Professional Qualifications": "Not Recorded"
}

PersonalData |merge (EducationData)   

Return: 

{
    "Name" : "Alex",
    "Age" : 32,
    "Highest Education" : "Grade 12",
    "Professional Qualifications" : "Not Recorded"
} 

This returns an object merging all the items in both objects into one object.

remove

Removes an item from an object.

 |remove (item)
Example: 

PersonalData := 
{
   "Name" : "Alex",
   "Score": "A",
   "Age" : 32
}
 
PersonalData |remove ('Score')   

Return: 

{
    "Name" : "Alex",
    "Age" : 32
} 

This returns the object removing the specific item.

Updated on November 29, 2022

Was this article helpful?

Related Articles