Implementation

Estimated reading: 4 minutes 7 views

Introduction

The Context Helper implementation are simple methods to navigate through an Object Tree / Map<String, Object> context.

Get Value from Context

The main purpose of this module is to offer the capability of retrieving values from a specified Context by using a path notation. I.e: Contacts.1.Name

To retrieve a value from a Map<String, Object> you just need to invoke the getValueFromContext method from the ContextHelper class.

  Map<String,Object> context = new Map<String,Object>();
  context.put('property','test');
  context.put('list', new List<String>{'test1','test2'});
  context.put('map', new Map<String,String>{'key1' => 'value1','key2' => 'value2'});
  context.put('mapList', new List<Map<String,String>>{new Map<String,String>{'name' => 'hello','value' => 'wrongvalue'}, new Map<String,String>{'name' => 'test','value' => 'rightvalue'}});

  System.assertEquals('rightvalue', ContextHelper.getValueFromContext(context, 'mapList.[name=test].value'),  'List Map value is not correct');
  System.assertEquals('test', ContextHelper.getValueFromContext(context, 'property'), 'Property value is not correct');
  System.assertEquals('test1', ContextHelper.getValueFromContext(context, 'list.0'), 'List value is not correct');
  System.assertEquals('test2', ContextHelper.getValueFromContext(context, 'list.1'), 'List value is not correct');
  System.assertEquals('value1', ContextHelper.getValueFromContext(context, 'map.key1'), 'Map value is not correct');
  System.assertEquals('value2', ContextHelper.getValueFromContext(context, 'map.key2'),  'Map value is not correct');

Simple Properties

The simplest case is to retrieve a named property from the context. In this case, it’s enough to specify the full path to the desired property.

Examples:

  • ParentAccount.Address.City
  • Order.Number

Lists

In case you want to retrieve or transverse a specific item in a list, for instance the first Contact for an Account you must specify the index of the element to retrieve.

Examples:

  • Contacts.1
  • Contacts.0.Name

Filtering Single Record from Lists

The usual use case when you want to work with a list is not to retrieve a item from the list by its order in it, but retrieving an item that meets some specific criteria. For this, you can filter the list by setting the filter logic between brackets [] as a path item. It will return the first record meeting the specified criteria.

Examples:

  • Contacts.[Name=Jon]
  • Contacts.[Active=true]

It’s important to note that the first record that meets the criteria will be returned, so you must carefully set the filter to avoid any unexpected behaviour.

Filtering Multiple Records from Lists

It’s also possible to filter a list and retrieve all the different records that meets the specified criteria. For that, you can specify a filter between parenthesis.

When you filter a List this way you can no longer transverse the object. In other words, the resulting list will be retrieved without processing any other path segment after it.

Examples:

  • Contacts.(City=Huelva)

Examples

Type Example Description
Simple Property Name Retrieves the Name property from the root object
Transverse Property ParentAccount.Country It gets the Country property from the ParentAccount object related to the root object
Get List Account.Contacts Get all the items from the Contacts list from the Acccount property of the root record
Get List Item by Order Accounts.Contacts.0 Get the first contact
Get List Item property by Order Accounts.Contacts.0.Name Get the Name for the first Contact in the list.
Filter List Item Account.Contacts.[Primary=true] Get the Contact which Primary flag is true
Filter List Item and get Property Account.Contacts.[Primary=true].Name Get the name for the Primary Contact
Filtering Multiple Items Account.Contacts.[Country=Spain] Get all contacts with Country = Spain

Leave a Comment

       
Euphoria, forever till the end of times

Euphoria

Share this Doc

Implementation

Or copy link

CONTENTS