Retrieving and Setting Fields and Entities

  • Updated

This article contains the following sections:

Retrieving an Entity from the RuleSession

Prerequisites A valid RuleSession, A valid Entity
Namespaces InRule.Runtime
Classes RuleSession, Entity
See Also Retrieving a Rule Application, Creating a RuleSession, Creating Entities

To retrieve an Entity from the RuleSession, use the GetEntity() method. The input to GetEntity() may contain either the Element ID or the Instance ID, if one has been assigned.

If no Entity is found, GetEntity() returns null.


Obtain the Element ID

// Obtain the Element ID to lookup the Entity from the RuleSession at a later point
string elementId = ruleSession.CreateEntity("Invoice").ElementId;

Retrieve the Entity using the Element ID

// Get the Entity from the RuleSession using the Element ID
Entity entity = ruleSession.GetEntity(elementId);
if (entity == null)
{
    // Entity was not found
}

Retrieve the Entity using an Instance ID

// Get the Entity from the RuleSession using the Element ID
Entity entity = ruleSession.GetEntity("Invoice175941");
if (entity == null)
{
    // Entity was not found
}

Retrieving Fields

Prerequisites A valid Entity
Namespaces InRule.Runtime
Classes Field, Collection, Entity
See Also  Retrieving a Rule Application, Creating a RuleSession, Creating Entities

Retrieving a field from the entity

//  Retrieve the CustomerID field from the Invoice entity.
Field customerId = invoiceEntity.Fields["CustomerID"];

Retrieving a field from a collection

// Retrieve the ProductID of the first LineItem in the LineItems collection.
Field productId = invoiceEntity.Collections["LineItems"][0].Fields["ProductID"];

Collection indexing is 0-based through the SDK and 1-based for referencing from within a rule application.


Retrieving a field to set a typed variable

//  Retrieve the CustomerID field value from the Invoice entity.        
int customerId = invoiceEntity.Fields["CustomerID"].Value.ToInt32();

Retrieving all fields in an Entity

// Show all the fields in the Entity
foreach (Field field in invoiceEntity.Fields)
{
 Console.WriteLine(string.Format("Found {0} {1}", field.Name, field.Value.ToString()));
}

Setting Fields

Prerequisites A valid Entity
Namespaces  InRule.Runtime
Classes  Field, Collection, Entity
See Also  Retrieving a Rule Application, Creating a RuleSession, Creating Entities Retrieving Fields

Setting a field in the entity

// Set the CustID field to 2
invoiceEntity.Fields["CustomerID"].Value = 2;

Setting a field from a collection

// Set the ProductID field of the first LineItem in the LineItems collection.
invoiceEntity.Collections["LineItems"][0].Fields["ProductID"].Value = 6;

Collection indexing is 0-based through the SDK and 1-based for referencing within rules.

Was this article helpful?

0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.