Run Several Entity Flows on Records From a Query
This Groovy script runs one or more manual entity flows on every record that an SQL query returns. The records can be of different types: each one gets only the flows that are meant for its type.
Recommended pattern
This script is not tied to any customer's setup. You can paste it as it is and control it entirely through its parameters.
| Class name | EARunEntityFlowFromQueryMultiEntityType |
| Script | EARunEntityFlowFromQueryMultiEntityType.groovy |
| Runs on | Any record. Usually it is attached to a task schedule. |
| Uses | Parameter 2 and Parameter 3. Leave Parameter 1 empty. |
| Shipped as | Source to paste. It is not built into the system. |
When to use it
The built-in EARunEntityFlowFromQuery runs a single flow on a list of records. That is enough when every record is of the same type. This script covers the case where it is not.
Suppose a customer has a manual flow RECALC-INV that refreshes some fields on sales invoices, and a matching flow RECALC-RET for sales returns. They want both to run every night on the documents of the previous day. With the built-in action that takes two task schedules. With this script it takes one: give it both flow codes and one query that returns the invoices and the returns together. Each invoice gets RECALC-INV, each return gets RECALC-RET.
It is also more useful when something goes wrong. The built-in action reports only how many records failed. This script reports the actual error of every record that failed.
How it works
- It finds the flows. It looks up each code in Parameter 2. If any code is not found, it stops before touching a single record.
- It runs the query. The query in Parameter 3 must return the record's entity type in its first column and the record's id in its second.
- It handles each record on its own. Each record is processed in its own transaction, so a record that fails is rolled back without undoing the records before it.
- It picks the flows for that record. It tries the flows in the order you listed them. A flow is skipped when its Target Type is a different type and its Entity Type List does not include the record's type. A flow is also skipped when it does not apply to the record: it is inactive, its criteria or Apply When Query do not match, or its dimensions, book or term are different.
- It runs every flow that remains as a manual run. The flow's Requires Commit On Manual setting is respected. If a flow fails, that record is rolled back and the script moves on to the next record.
- It reports at the end. You get the messages of every record that failed. A record that none of the flows applied to is reported too.
While the script runs, its progress (record n of total) is listed under Monitor Current Tasks. Pressing Kill there stops the run before the next record, with the message Marked for kill. See When the System Is Slow.
The flows must have a Manual line
The script runs each flow the way a user would run it manually. A flow with no active element whose target action is Manual does nothing, and the run does not count that as a failure.
Setting it up as a task schedule
- Open Basic → Settings → Task Schedule and create a new task of type Action.
- On the Action tab, paste the whole script from the file above into Groovy Script. Leave Class Name empty: it is filled in when you save.
- Fill in the parameters:
| Parameter | Title shown after saving | What to enter |
|---|---|---|
| Parameter 1 | Ignore | Nothing. |
| Parameter 2 | Entity Flow Codes (csv) | The codes of the manual flows, separated by commas and with no spaces, for example RECALC-INV,RECALC-RET. |
| Parameter 3 | Query, eg: select entityType,id from SalesInvoice where dueDate = getdate() | An SQL query whose first two columns are the entity type and the id of each record to process. |
- Save the task. Class Name now shows
EARunEntityFlowFromQueryMultiEntityType, the description appears under Action description, and each parameter shows its title. - To test it, click Run Now and check the result before you set a schedule.
The same script also works as an element of an entity flow. Paste it into the Groovy Script column of the Details grid on Administration → Display Customization → Entity Flow. The parameters stay the same.
Example
This query processes the sales invoices and the sales returns dated yesterday:
select entityType, id from SalesInvoice where valueDate = cast(getdate() - 1 as date)
union all
select entityType, id from SalesReturn where valueDate = cast(getdate() - 1 as date)With Parameter 2 set to RECALC-INV,RECALC-RET, every invoice runs whichever of the two flows targets sales invoices, and every return runs the one that targets sales returns. A flow whose entity type list covers both types runs on both.
Messages you may see
None of these messages has an Arabic translation, so they appear in English on Arabic screens as well.
| Message | Why | What to do |
|---|---|---|
| Could not find entity flows with codes {0} | At least one of the codes in Parameter 2 does not exist. {0} is the whole parameter as you typed it. | Check the codes. Remove any spaces after the commas. |
| Could not find a flow for entity {0} | None of the listed flows applied to this record. Either its type did not match or its conditions did not. | Check the flows' target type, entity type list, criteria and dimensions against this record. |
| Marked for kill | Somebody pressed Kill on the task in Monitor Current Tasks. | Run it again. Records that were already processed stay processed, so narrow the query if the flows should not run twice. |
The script
package com.namasoft.infor.domainbase.util.actions;
import com.namasoft.bus.TransactionProvider;
import com.namasoft.common.utilities.*;
import com.namasoft.infra.domainbase.common.NaMaContext;
import com.namasoft.infra.domainbase.datafields.*;
import com.namasoft.infra.domainbase.datafields.enums.EntityTypeDF;
import com.namasoft.infra.domainbase.entity.base.*;
import com.namasoft.infra.domainbase.persistence.repos.*;
import com.namasoft.infra.domainbase.util.*;
import com.namasoft.modules.basic.domain.entities.EntityFlow;
import com.namasoft.modules.basic.domain.primitives.EntityTargetAction;
import java.util.*
import java.util.stream.Collectors;
public class EARunEntityFlowFromQueryMultiEntityType implements EntityAction<BaseEntity> {
@Override
public Result doAction(EntityTargetAction action, BaseEntity o, LongTextDF... parameters) {
List<String> codes = StringUtils.csvLineToList(parameters[1].getPrimitiveValue());
List<EntityFlow> flows = codes.stream().map { Persister.findByBusinessCode(EntityFlow.class, EntityCodeDF.fromString(it), FilterOnDimensions.No) }.collect(Collectors.toList());
if (flows.stream().anyMatch { ObjectChecker.isEmptyOrNull(it) })
return Result.createFailureResult("Could not find entity flows with codes {0}", parameters[1]);
Result result = Result.createAccumulatingResult();
List<Object[]> rows = Persister.prepareAndExecuteJDBCQuery(o, parameters[2].getPrimitiveValue());
for (int i = 0; i < rows.size(); i++) {
Object[] row = rows[i];
NaMaContext.addTaskProgress("Running flow for {0} of {1}", i + 1, rows.size());
if (NaMaContext.isCurentTaskMarkedForKill())
throw new NaMaFailureResultException("Marked for kill");
try {
TransactionProvider.get().doInNewTransaction({
Result inner = Result.createAccumulatingResult();
BaseEntity object = Persister.findByID(EntityTypeDF.fromString(ObjectChecker.toStringOrEmpty(row[0])),
UniqueIDDF.fromObject(row[1]));
boolean foundFlow = false;
for (final def flow in flows) {
def notSameEntity = ObjectChecker.areNotEqual(flow.getTargetType(), object.getEntityType())
def notSameEntityTypeList = flow.getEntityTypeList() == null || flow.getEntityTypeList().notContainsType(object.getEntityType());
if (notSameEntity && notSameEntityTypeList)
continue;
if (!object.checkIfFlowApplicable(flow))
continue;
object.runManualEntityFlow(flow, inner);
if (inner.failed())
throw new NaMaFailureResultException(inner);
foundFlow = true;
}
if (!foundFlow)
throw new NaMaFailureResultException("Could not find a flow for entity {0}", object);
return inner;
}).addToAccumulatingResult(result);
}
catch (Exception e) {
NaMaLogger.error(e);
if (e instanceof NaMaFailureResultException)
result.accumulate(((NaMaFailureResultException) e).getReason());
else
result.accumulate(Result.createTranslatedFailureResult(e.getMessage(), e.getMessage()));
}
}
return result;
}
@Override
public String describe() {
return "Runs entity flow with code specified in parameter 2, the entity flow must be manual. The flow runs on records specified in parameter 3";
}
@Override
public List<String> columnNames() {
return Arrays.asList("Ignore", "Entity Flow Codes (csv)", "Query, eg: select entityType,id from SalesInvoice where dueDate = getdate()");
}
}Related
- EAGroovyAction: how Groovy scripts are attached to entity flows and task schedules.
- EARunEntityFlowFromQuery: the built-in single-flow version.
- Scheduled Tasks