How to execute tasks with the ECHO Assistant (v10.10)?
It is possible to execute tasks outside of the ERP process with the ECHO Assistant. These tasks can be scheduled automatically or by using operations in the ERP. The task execution allows to create ECHO messages that can be used, for example, to report the end of the execution to the user. This way, it is possible for the user to execute a specific task "on request" in consequence of an action in the ERP, or automatically, during the night or at the weekend, for example. This approach frees resources from the ERP and from the workstation, considering that tasks are always executed on the server. The ECHO task execution is limited by the availability of resources on the server. That is, the task can be delayed, in case the server is too limited in terms of disk, memory, CPU, and resources on the SQL Server. This management is performed automatically. To create tasks, it is necessary to first create a topic. You can view the article "How to extend the Echo Assistant (v10.10 SR1)?" for more information on its creation. The previous point is adapted to the normal reality of producing topics and actions that interact with the ERP and generate messages at the end. The handlers list can be totally customized by the integrator and include, for example, handlers that use the information of other sources. Right now, the ECHO Assistant already knows how to execute its task. This task can be scheduled manually, but will also be scheduled automatically, in the defined calendar. If you remove the "Schedule", the task will only be scheduled manually by invoking the platform function: "PSO.Bot.CriaTarefa" using PEX or "Plataforma.Bot.CriaTarefa" using an external integration. Task and pipeline created in the previous points (the definition and detail in each column is present in the example SQL file on GitHub): Next, let's implement the task: In the topic project, please create a new class with the name you prefer and the suffix "Handler", ex.: "TaskExecutionExampleHandler.cs". If you wish to perform SQL queries to the database, you must initialize the connection string, as shown previously: If necessary, use the ERP engine, it is available using the reflection and can be used as follows: Using "ErpHelper" inside of the "using" ensures that the "dispose" pattern is correctly applier and that the engine connection is destroyed as soon as the code block finishes. In case it is necessary to create messages as a consequence of the execution, you can do it the following way: Note: All possibilities to create messages will be approached in a more complete way in the following article. With the information currently in this article, it is possible to execute any lengthy task, automatically or upon request, by integrating with the ERP engines, or by directly executing the SQL code, as well as its successful (or unsuccessful) report for the final user, in the ERP. The code detailed in this article is available on the PRIMAVERA GitHub.Create tasks
<Handler Id="ErpReadConfigHandler" Order="1" Behavior="Reader" Type="Primavera.Platform.HurakanHandlers.ErpReadConfig" ConfigStr="instanceIdFilter=%%InstanceId%%;userFiltler=%%UserFilter%%;enterpriseFilter=%%EnterpriseFilter%%"/>
<Handler Id="TaskExecutionExampleHandler" Order="2" Behavior="Reader" Type="Primavera.Bot.DevelopersNetworkTopic.Handlers.TaskExecutionExampleHandler" ConfigStr="topicId=%%TopicId%%;taskId=%%TaskId%%"/>
<Handler Id="CreateUserBotMessagesHandler" Order="3" Behavior="Reader" Type="Primavera.Hurakan.BotHandlers.CreateUserBotMessages" ConfigStr=""/>
<Handler Id="SaveBotMessagesHandler" Order="4" Behavior="Reader" Type="Primavera.Hurakan.BotHandlers.SaveBotMessages" ConfigStr=""/>
INSERT INTO Bot.BotTasks(NaturalKey, DescriptionId, TopicId, Importance, ScheduleConfig, MessageConfig, PlatformsConfig, PipelineConfig, [System], Active, CreatedBy, ModifiedBy, AllowConfigReceivers, ReceiverOption, [Version])
VALUES(
'TaskExecutionExample',
'Task_ExecutionExample',
@TopicId,
1,
'{"Active":true,"Execute":""Daily"","StartAt":1,"StartTolerance":31}',
'{"Scope":"Instance","ExpireDays":365}',
'[{"Version":"V100", "platform":"Executive"},{"Version":"V100", "platform":"Professional"}]',
'
',
0, 1, 'MyUser', 'MyUser', 1, -1, 1)
Implement tasks
[Export(typeof(IHandler))]
[Export(typeof())]
this.TopicId = "";
this.TaskId = "";
this.Initialize(message as IntegrationMessage);
- If the scope is "Instance" you will have one of the instances available with all productive companies and users.
- If the scope is "Instance|Enterprise" you will have one of the instances, one of the companies and all users.
- If the scope is "Instance|Enterprise|User" you will only have one of the possible combinations.foreach (Instance instance in this.Instances)
{
foreach (Enterprise enterprise in instance.Enterprises)
{
(...)
this.BuildConnectionString(instance.ServerSql, instance.LoginSql, instance.PasswordSql, instance.Database);
(...)
}
}
using (ErpHelper erpHelper = new ErpHelper())
{
// edit a customer and change something...
erpHelper.SetErpConnectionString(instance, enterprise.Code);
dynamic customerObject = erpHelper.Erp.Base.Clientes.Edita(customerId);
customerObject.Nome = "Nome";
erpHelper.Erp.Base.Clientes.Actualiza(customerObject);
}
protected List BotMessages { get; set; };
List instanceMessages = new List();
BotMessage companyMessage = this.InitializeNewMessage(instance, EmptyUserCodePlaceHolder, "A tarefa 'TaskExecutionExample' foi concluída com sucesso.");
companyMessage.CompanyId = enterprise.Code;
instanceMessages.Add(companyMessage);
this.BotMessages.Add(instance.Id, instanceMessages);
return this.BuildIntegrationMessage(this.BotMessages);
login para deixar a sua opinião.