Vai al contenuto

Use Case Example

Starting from a use case, this document shows common operations involving multiple eLegere Script functions and classes methods.

Abstract

The document provides a use case while commenting the most frequent eLegere Script operations.

Warning

You should not use eLegere Script in every case where multiple scripts modify the same rows. eLegere does not guarantee the execution order of the eLegere scripts.

The Case

The document takes the following case as an example to show the most common operations Users can perform with eLegere Script.

There are two Applications and two Storages that must interact with one each other. The applications manage the assignment of employees to customers' projects.

Application: Employees

The User registers the list of employees through the Employees application. Also, it assigns Roles and Projects to the row of the employee.

The Employees application manipulates data from the Storages Employees and Roles.

Employees Storage includes the following fields:

  • Id_Employee The field identifying the row of each employee. It is an Identity type field.
  • Full_Name The name of the employee. It is a String type field.
  • Birth_Date The birth date of the employee. It is a Date type field.
  • Hourly_Cost The hourly cost of the employee's services. It is a Numeric type field
  • Id_Current_Project The field to build the Lookup relation with the Projects storage. It is a Numeric type field
  • Id_Current_Role The field to build the Lookup relation with the Projects storage. It is a Numeric type field.

Roles Storage includes the following fields:

  • Id_Role The field identifying the row of each Role. It is an Identity type field.
  • Role_Name The name of the employee. It is a String type field.

Application: Projects

The User tracks projects through the Projects application. Each row has a Detail table listing all the employees working on the project.

Projects registers the name of the project, beginning date, end date, and completition status.

Each row with the name of the project has a Detail table called Project Team.

Project Team takes data from the Storages Employees and Roles. The Detail table summarizes the Employee's Id, the Full Name, the Hourly Cost, and the Current Role within the project.

Besides taking data Employees and Roles, Projects manipulates data from the Storage Projects

Projects Storage includes the following fields:

  • Id_Project The field identifying the row of each project. It is an Identity type field.
  • Project_Name The name of the project. It is a String type field.
  • Start_Date The beginning date of the project. It is a Date type field.
  • End_Date The ending date of the project. It is a Date type field.
  • Completed The status tracker of the project. It is a Boolean type field.

The Necessity

Actions on the Project application must create or modify rows on the Detail table or the row in Employees automatically.

eLegere Script makes this process possible. Below, the document takes and examines some eLegere Script actions working under the hood of Projects.

Updating Detail Rows after Master Row Update

The User inserts or updates the Master Row of an Application. The action must create or update one or more Detail Rows of an Application. In the example below, we create 3 new rows in the "Team Members" detail and link them to a newly created row on the Projects application.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    const projectsApp = await elegere.getApplicationFromToken(context.ApplicationId, context.DomainId, context.Url, context.SessionToken)

    const detailRowsToCreate = [
        {
            FirstName: "Francesco",
            LastName: "Ramanujah",
            BirthDate: 19780505,
            Hourly_Cost: 400
            Id_Current_Project: context.NewItem.Id_Project
        },
        {
            FirstName: "Walter",
            LastName: "White",
            BirthDate: 19941015,
            Hourly_Cost: 200
            Id_Project: context.NewItem.Id_Project
        },
        {
            FirstName: "Aaron",
            LastName: "Swartz",
            BirthDate: 19880812,
            Hourly_Cost: 150
            Id_Project: context.NewItem.Id_Project
        }
    ]

    const saveResult = await projectsApp.saveManyDetail("Team Members",  context.NewItem.Id, detailRowsToCreate)

projectsApp refers to the Projects application. In order to create the object we need to pass to the elegere.getApplicationFromToken function:

  • context.ApplicationId The parameter identifies the Id of the Application in which the script writes the rows.
  • context.DomainId The parameter identifies the Domain of the Application.
  • context.Url The parameter identifies the Url of the eLegere environment.
  • context.SessionToken The parameter takes the SessionToken of the User's login for authentication.

context is the object that contains information about the environment inside which the script is executed.

detailRowsToCreate is an object containing the information to register as detail rows of the Projects application.

saveResult is the result of the saveManyDetail method.

The saveManyDetail method takes the Detail table name "Team Members", the Id of the Master Row, and creates several detail rows.

Updating Master Rows of Application A and Application B

The User inserts or updates the Master Row of Application A. The action must creare or update one or more Master Rows in Application B. In the example below, once a project is set to "Complete", we update all the rows on the app "Employees" to show that they are now Unassigned.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
    const employeesApp = await elegere.getApplicationFromToken(employeesAppId, context.DomainId, context.Url, context.SessionToken)

    const completedProjectId = context.NewItem.Id_Project
    const projectFilter = "Id_Current_Project eq " + completedProjectId
    const projectEmployees = (await employeesApp.getItems(projectFilter)).Items

    const updatedEmployees = projectEmployees.map(employee => ({
        Id: employee.Id,
        TIMESTAMP: employee.TIMESTAMP,
        Id_Current_Project: 1
    }))

    const saveResult = await employeesApp.saveMany(updatedEmployees)

employeesApp refers to the Employees application. In order to create the object we need to pass to the elegere.getApplicationFromToken function:

  • employeesAppId The parameter identifies the Id of the Application in which the script writes the rows.
  • context.DomainId The parameter identifies the Domain of the Application.
  • context.Url The parameter identifies the Url of the eLegere environment.
  • context.SessionToken The parameter takes the SessionToken of the User's login for authentication.

context is the object that contains information about the environment inside which the script is executed. context.NewItem is the completed project's row.

completedProjectId is the value of the Id_Project field for the row that was just updated.

projectFilter is the filter that selects the employees we want to update.

projectEmployees is the list of employees that are currently associated to the updated project.

updatedEmployees creates a representation of the Employee rows with the two required system fields, Id and TIMESTAMP, and the fields we want to modify (in this case only the Id_Current_Project field). We use the JavaScript .map method to transform every individual row.

The saveMany method finally updates the Employee rows that we want to dissociate from the project.

The User deletes a Master row in the application. The action must clear all the related Detail rows connected to that Master row. In the following example, we delete a row on the Project app, and then we delete all the Employee rows that are associated with that project.

1
2
3
4
5
6
    const projectApp = await elegere.getApplicationFromToken(context.ApplicationId, context.DomainId, context.Url, context.SessionToken)

    const deletedProjectId = context.OldItem.Id_Project
    const projectFilter = "Id_Current_Project eq " + deletedProjectId
    const rowsToDelete = (await projectApp.getDetails(“Project Team”, projectFilter), 100).Items
    const deleteResult = await projectApp.deleteManyDetail("Project Team", rowsToDelete) 

projectsApp refers to the Projects application. In order to create the object we need to pass to the elegere.getApplicationFromToken function:

  • context.ApplicationId The parameter identifies the Id of the Application in which the script writes the rows.
  • context.DomainId The parameter identifies the Domain of the Application.
  • context.Url The parameter identifies the Url of the eLegere environment.
  • context.SessionTokenThe parameter takes the SessionToken of the User's login for authentication.

context is the object that contains information about the environment inside which the script is executed.

deletedProjectId is the value of the Id_Project field for the row that was just deleted. Notice how we take this value from context.OldField, as since the row was just deleted, context.NewItem will be null.

projectFilter is the filter that selects the employees we want to delete.

rowsToDelete is the list of the employees that we want to delete. We obtain this list of employees through the getDetails method.

The deleteManyDetail method finally deletes the Employee rows that we want to remove from the app.