logo

Example of working with objects in scripts

For this example, we've created an object named City with the Name property of the String type. Let's see how cities are filtered by partial name. The variable context.CityName is the search string. The list of found cities is written in the context.Cities variable.

Script with PublicAPI

Note
Documentation on PublicAPI is available here
Attention
The script below is relevant for ELMA up to 3.12.1 inclusive.
Namespace:
using EleWise.ELMA.API;

Variant 1: Using object and filter manager

Script text:

var items = PublicAPI.Objects.UserObjects.UserCity.Filter().Name(context.CityName).Find();
context.Cities.AddAll(items);

Variant 2: Loading object instance by ID

Script text:

var sprav = PublicAPI.CRM.Contractor.LoadOrNull(id);     //- contractor manager is used, but any manager can be used
                                                         //- if there is no instance in the object, the LoadOrNull method will return null
var sprav = PublicAPI.CRM.Contractor.Load(id);         // - in this case, if there is no object instane, an exception appears

Script without PublicAPI

You need the following namespaces:

using EleWise.ELMA.ConfigurationModel;
using EleWise.ELMA.Model.Managers;
using EleWise.ELMA.Model.Services;
using EleWise.ELMA.Runtime.Managers;
using EleWise.ELMA.Runtime.NH;
using EleWise.ELMA.Services;

Variant 1: Using an object and filter manager

// The filter(CityFilter class) is created if the Generate Filter option is selected for this object.
         // The property is available in the filter (filter.Name)if for the Name propety in the object's settings the Participates in search (filter) option is selected.
         var manager = EntityManager<City>.Instance;
         var filter = InterfaceActivator.Create<CityFilter>();
         filter.Name = context.CityName;
         var items = manager.Find(filter, null);
         context.Cities.AddAll(items);
In the manager, a set of methods is available for working with this object (for example search, loading by ID, saving, deleting).
To see the list of methods, type "manager." in the script editor. A list of available methods with comments will appear.

Variant 2: Loading an object instance by Id

var sprav = ContractorManager.Instance.LoadOrNull(id); //- contractor manager is used, but any manager can be used
                                                       //- if there is no instance in the object, the LoadOrNull method will return null
var sprav = ContractorManager.Instance.Load(id); // - in this case, if there is no object instance, an exception appears

Variant 3: Using NHibernate queries

var session = Locator.GetServiceNotNull<ISessionProvider>().GetSession("");
var items = session.CreateQuery("from City where Name like :Name")
.SetParameter("Name", "%" + context.CityName + "%")
.List<City>();
context.Cities.AddAll(items);
The type of the Cities variable must be the same as the object where the search is performed (in our example it is the City object), but with the list link type. After the script is executed, this variable will store a list of object instances that satisfy the search conditions. It is best to show the search results in task forms as "read only".