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:

 
[Component]
public class Contractor_proc_run : PostFlushEventListener
{
   public override void OnPostInsert(PostInsertEvent @event)
   {
        // Check @event.Entity for the required type and perform actions with it
        if(@event.Entity is IContractor)
        {    
            var mng=ProcessHeaderManager.Instance.GetStartableProcesses().ToList();
            foreach(var i in mng)
            {
                if(i.Name=="contractor") //specify a process name
                {                    
                             var process = i.Published;
                    if (process == null)
                    {
                        throw new Exception(SR.T("The 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 the process as the current user
                    try
                    {
                        workflowService.Run(instance);                                     
                    }
                    catch (Exception ex)
                    {                        
                        // Logger.Error("Error starting the process", ex); // Error message when starting the process
                        // Quit trying to start the process
                    }
                }   
            }
        }
    }
}

Use the following namespaces:

 
using EleWise.ELMA.ComponentModel;
using EleWise.ELMA.ConfigurationModel;
using EleWise.ELMA.CRM.Managers;
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.Runtime.NH.Listeners; 
using EleWise.ELMA.Security.Services;
using EleWise.ELMA.Services;
using EleWise.ELMA.Workflow.Managers;
using EleWise.ELMA.Workflow.Services;
using NHibernate.Event;