How to execute tasks with the Echo Assistant?
This article focuses on the task execution possibility, outside of the ERP process, with ECHO Assistant. These tables can be scheduled automatically or by using operations in the ERP. The table 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. The advantage of this approach is that it 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?" 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: 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 lenghty 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 in the PRIMAVERA GIT page (https://github.com/PrimaverabssDeveloper)Create tasks
<Handler Id="ErpReadConfigHandler" Order="1" Behavior="Reader" Type="Primavera.Hurakan.Handlers.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="SaveBotMessagesHandler" Order="3" Behavior="Reader" Type="Primavera.Hurakan.BotHandlers.SaveBotMessages" ConfigStr="/>
<Task id="TaskExecutionExample" pipelineId="TaskExecutionExamplePipeline">
<Schedule Execute="Monthly" StartAt="25" StartTolerance="5"/>
<Message ExpireDays="365" Scope="Instance|Enterprise|User"/>
<SupportedPlatforms>
<SupportedPlatform Version="V100" platform="Executive"/>
<SupportedPlatform Version="V100" platform="Professional"/>
</SupportedPlatforms>
</Task>
<Pipeline Id="TaskExecutionExamplePipeline">
<Handlers>
<Handler Id="ErpReadConfigHandler" Order="1" Behavior="Reader" Type="Primavera.Hurakan.Handlers.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="SaveBotMessagesHandler" Order="3" Behavior="Reader" Type="Primavera.Hurakan.BotHandlers.SaveBotMessages" ConfigStr="/>
</Handlers>
</Pipeline>
Implement tasks
[Export(typeof(IHandler))]
[Export(typeof())]
this.TopicId = ";
this.TaskId = ";
this.Initialize(message as IntegrationMessage);
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 Dictionary<string, list<botmessage=">> BotMessages { get; set; }</string,>
List instanceMessages = new List();
BotMessage companyMessage = this.InitializeNewMessage(instance, user.Code, "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);