Using filters by object instances in scripts

Often you need to find object instances that satisfy certain parameters. The best way of solving this task is to use filters.

Assume you need to find all the users with the same last name, e.g. Smith. In this case, the script will be as follows.

Script without using PublicAPI.

Namespaces:

using EleWise.ELMA.Services;
using EleWise.ELMA.Security.Models;
 

Script:

1
2
3
4
5
6
7
var a = new InstanceOf<IUserFilter> { //create a filter
    New = {
     LastName = "Smith",   //search parameter
     }
    }.New;
    // write the results to the Users variable of the User type with the List link type
    context.Poljzovatelj.AddAll(EntityManager<User>.Instance.Find(a, null));
Creating a similar script with PublicAPI
Documentation on PublicAPI is available here

Namespaces:

using EleWise.ELMA.Services;
using EleWise.ELMA.Security.Models;
using EleWise.ELMA.API;
 

Script:

//create a filter by instances of the User object, by last name and write the results to the Users variable of the User type with the List link type
context.Users.AddAll(PublicAPI.Portal.Security.User.Filter().LastName("Smith").Find());
 

You can use filters in scripts for user objects too. Assume there is a user object Branch Office, which stores the list of company's branch offices and data on them. Using a filter, you need to find only those offices that are in Londan, i.e. the instances of the Branch Office object, whose string field City has the London value. To be able to use filters for custom objects in scripts, the following condition must be met:

 In the settings of the object property, which will be used for filtering, on the Advanced tab, check the Participates in search (filter) box:

 

The script will be like this.

Script without using PublicAPI.

Namespaces:

using EleWise.ELMA.Services;
using EleWise.ELMA.ConfigurationModel;
 

Script text:

var a = new InstanceOf<IFilialFilter> {  //create a filter
       New = {
          City = "London",      //search parameter
          }
       }.New;
       //write the results to the variable OfficesInLondon of the Branch Office type with the List link type
       context.OfficesInLondon.AddAll(EntityManager<Filial>.Instance.Find(a, null)); 
 
Creating a similar script with PublicAPI
Documentation on PublicAPI is available here

 

Namespaces:

using EleWise.ELMA.Services;
using EleWise.ELMA.ConfigurationModel;
using EleWise.ELMA.API;
 

Script text:

//create a filter by instancs of the Branch Office object, by the City field and write the results to the variable OfficesInLondon of the Branch Office type with the List link type
context.OfficesInLondon.AddAll(PublicAPI.Objects.UserObjects.UserFilial.Filter().City("London").Find());