logo

Executing a script upon an event

This example shows how a script can be executed after creating a client.

Procedure: 

1. Create an object in the Designer. It will contain a script with the event override.
2. In the object, open the Scripts tab and create a script.
3. Add a class with the event override:
 
[Component]
public class ClassName : PostFlushEventListener
{
    //create an object
    public override void OnPostInsert(PostInsertEvent @event)
    {
        // Check @event.Entity for the required type and perform actions with it
    }
    //edit the object
    public override void OnPostUpdate(PostUpdateEvent @event)
    {
        // Check @event.Entity for the required type and perform actions with it
    }
}
4. Publish the object and restart the server. After that, when you create a contractor, the action described in the script will be performed.

An example of a script for starting a business process when creating a contractor:

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;

Script text:

[Component] 
    public class Contractor_proc_run : PostFlushEventListener
    {
        public override void OnPostInsert(PostInsertEvent @event)
        { 
            // Check @event.Entity for the required type and apply the necessary actions
            if (@event.Entity is IContractor)
            {
                var header = PublicAPI.Processes.ProcessHeader.Filter().Startable(true).Name("contractor").Find().FirstOrDefault();
                if (header == null) //write your process name 
                {
                    throw new Exception("Process is not found"); // If the process is not found, show an error
                }
                var process = header.Published;
                if (process == null)
                { 
                    throw new Exception("Process is not published"); // If the process is not published, show an error
                } 
                try
                { 
                    PublicAPI.Processes.WorkflowInstance.StartProcess(process, "_Name");//"_Name" - instance name
                } catch (Exception ex)
                {                         
                    // Logger.Error("Error starting the process", ex); // Process start error message
                    // Quit starting the process
                } 
            }    
        }
    }

Script without PublicAPI

Use the following namespaces:

using EleWise.ELMA.ComponentModel; 
using EleWise.ELMA.CRM.Models;
using EleWise.ELMA.Model.Common;
using EleWise.ELMA.Model.Entities;
using EleWise.ELMA.Model.Managers;
using EleWise.ELMA.Model.Types.Settings;
using EleWise.ELMA.Model.Services;
using EleWise.ELMA.Runtime.NH.Listeners;
using EleWise.ELMA.Security.Services;
using EleWise.ELMA.Services;
using EleWise.ELMA.Workflow.Managers;
using EleWise.ELMA.Workflow.Services;
using EleWise.ELMA.Workflow.Models;
using NHibernate.Event;

Script text:

[Component] 
    public class Contractor_proc_run : PostFlushEventListener
    {
        public override void OnPostInsert(PostInsertEvent @event)
        { 
            // Check @event.Entity for the required type and apply the necessary actions
            if  (@event.Entity is IContractor)
            {
                //var  header = PublicAPI.Processes.ProcessHeader.Filter().Startable(true).Name("contractor").Find().FirstOrDefault();
                var mng = ProcessHeaderManager.Instance;
                var filter = InterfaceActivator.Create<ProcessHeaderFilter>();
                filter.Name = "контрагент";
                filter.Startable = true;
                var header = mng.Find(filter, null).FirstOrDefault(); 
                if (header == null) //enter your process name
                {
                    throw new Exception("Process is not found"); // If the process is not found, show an error 
                } 
                var process = header.Published; 
                if (process == null)
                { 
                    throw new Exception("Process is not published"); // If the process is not published, show an error
                } 
                var workflowService = Locator.GetServiceNotNull<IWorkflowRuntimeService>(); 
                var instance = WorkflowInstanceManager.Instance.Create(); 
                instance.Process = process; 
                instance.Name = "_Name"; //process instance name
                instance.Initiator = AuthenticationService.GetCurrentUser<EleWise.ELMA.Security.Models.IUser>(); // Start on behalf of the current user
                try
                { 
                    workflowService.Run(instance);                                      
                } catch (Exception ex)
                {                         
                    // Logger.Error("Error starting the process", ex); // Process start error message
                    // Quit starting the process
                } 
            }    
        }
    }