How-toEpicor KineticEpicor Functions / BPM

Epicor Function: How to Call a BAQ from C# (Functions and BPM)

Question
epicor function call baq

Also searched as

  • call baq from epicor function c#
  • execute baq in bpm code epicor
  • epicor dynamicquery executebyid

Short answer

To run a BAQ from an Epicor Function or a BPM code block, call the Dynamic Query service: get the query's execution parameters by BAQ ID, set any parameter values, run it with the execute-by-ID method, and read rows from the results table. Declare the service in the function so the platform can resolve it.

Applies to: Epicor Kinetic Epicor Functions and BPM custom code. Service and type names have changed across versions, so verify them in your function editor's IntelliSense.

Call a BAQ from a function

  1. 1Create or open the function in Epicor Function Maintenance and add the request and response parameters you need.
  2. 2Add the Dynamic Query service contract to the function's services so CallService can resolve it.
  3. 3In the code, call the service with CallService and inside the callback get the execution parameter set for your BAQ ID.
  4. 4Set parameter values on the matching parameter rows, using the exact parameter names defined in the BAQ.
  5. 5Call the execute-by-ID method and read the Results table of the returned dataset.
  6. 6Loop the rows, map them to your response parameter, and return.
  7. 7Test in the function's test area with real parameters, then check security for the user or the API key calling the function.

Working pattern

The following sketch shows the common shape. Type names for the execution parameter dataset and result columns depend on your version, so let IntelliSense confirm them rather than copying blindly.

BAQ IDs and parameter names are case sensitive in practice, and a mismatch is the most frequent cause of a function that runs but returns nothing.

// Inside an Epicor Function code block (names may vary by version)
var result = new System.Collections.Generic.List<string>();
this.CallService<Ice.Contracts.DynamicQuerySvcContract>(dq =>
{
    var qeds = dq.GetQueryExecutionParametersByID("MyJobsBAQ");
    // set a parameter value on the parameter row named in the BAQ
    var p = qeds.ExecutionParameter.NewExecutionParameterRow();
    p.ParameterID = "JobNum";
    p.ParameterValue = jobNum;
    p.ValueType = "nvarchar";
    p.IsEmpty = false;
    qeds.ExecutionParameter.AddExecutionParameterRow(p);
    var ds = dq.ExecuteByID("MyJobsBAQ", qeds);
    foreach (var row in ds.Results.Rows)
        result.Add(row["JobHead_JobNum"].ToString());
});

BAQ or direct data access

Calling a BAQ is useful when the query already exists, when business users maintain it, or when you want its security. For simple lookups inside server code, direct LINQ over the Db context in recent versions may be shorter and faster, since it avoids the dataset round trip. For anything report-like and reusable, keep the BAQ.

Remember that a BAQ called from a function runs with the calling user's rights. Calls from an API key or service account need that account to have access to the BAQ.

Errors you will meet

A not found error on the BAQ ID means a typo or the query is not generated or not in the current company scope. Empty results with no error usually mean parameters did not bind, so check names and the value type. A permission error points at security on the BAQ or the calling account.

From a BPM, the same call works inside a custom code block, though heavy queries in a synchronous directive slow down the user's save, so move them to a post-processing or data directive design.

Try it here, free

Epicor BAQ Performance Tuning Checklist

Open full tool

Work through a practical list of checks when a BAQ, dashboard or BPM lookup runs slowly in Epicor Kinetic.

Common pitfalls

  • !Parameter ID typos, which bind nothing and return all or no rows.
  • !Calling a heavy BAQ from a synchronous BPM and slowing every save.
  • !Forgetting to generate the BAQ after editing it.
  • !Assuming the function runs with administrator rights.

How an ERP-grounded AI assistant handles this

An assistant that knows your BAQ catalog can suggest which existing query to call instead of writing a new one, and draft the function wrapper for a developer to review. Netray builds such developer-assist tools to run privately against your Epicor environment, including on-prem installs.

Frequently asked questions

How do I run a BAQ from C# in Epicor?

Call the Dynamic Query service from the function or BPM code, set the parameters on the execution parameter set, then use execute-by-ID and read the Results table of the returned dataset.

Can I call a BAQ from a BPM directive?

Yes, in a custom code block with the same service call. Avoid heavy queries in pre-processing directives since they block the user's action.

Why does my function return no rows?

Usual causes are a wrong parameter ID, a wrong value type, the BAQ not generated, or criteria excluding everything. Test the BAQ with the same parameters in the Query Builder first.

Should I use a BAQ or LINQ on Db?

Use a BAQ for existing, shareable and user-maintained queries. For simple, performance sensitive lookups in server code, direct LINQ can be simpler, if your version supports it.

Related

How-to

How to Create a BAQ in Epicor Kinetic

A BAQ (Business Activity Query) is a saved query in Epicor Kinetic that you build in the Query Builder without writing SQL. Choose the tables, define joins, select display fields, add criteria and calculated fields, test the result, then use the BAQ in dashboards, reports, BPMs or the REST API.

Advanced

Epicor Function Custom Code: C# Signature, Parameters and Calling It

An Epicor Function is a reusable server-side routine with typed request and response parameters. Its body is C# you write in the function editor, either as a custom code block or as widgets. Define the inputs and outputs, write the code, test it, then call it from a BPM, from Application Studio, from another function or through the REST API.

Advanced

How to create and call an Epicor Function

Open Function Studio from the main menu (Customization > Function Studio) or from inside a BPM designer's Call Context widget, define a Library and Function with typed inputs/outputs, write the logic in the C# widget, then compile and test with the built-in test harness before calling it from a BPM directive, a dashboard, or another function.

How-to

How to Create a BPM Method Directive in Epicor Kinetic

In Epicor Kinetic, open Business Process Management > Method Directive Maintenance (the BPM Workbench), pick the business object and method, choose Pre-Processing, Base Processing or Post-Processing, then add a condition and actions such as Show Message or Set Field. Enable it and test in a pilot company before production.

Error fix

Fixing Epicor BusinessObjectException: BPM Directive Errors

A BusinessObjectException that says "A Business Process Management (BPM) directive has raised the following error" means a directive on that business object stopped the transaction, either deliberately via a Raise Exception widget or accidentally via an unhandled .NET error in Custom Code. Expand the InnerException on the error dialog to see the real message and the directive name, then open BPM Designer for that object and method to find the widget that fired.

How-to

Fixing a Slow Epicor BAQ (Business Activity Query)

A slow BAQ in Epicor is usually caused by unindexed join columns, a subquery or calculated field forcing a table scan, or the BAQ pulling far more rows than the dashboard actually displays before filtering client-side. Fix it by reading the SQL Server execution plan Epicor generates, moving filters into the BAQ criteria instead of the dashboard filter panel, and replacing subqueries with joins where possible.

AI for ERP

AI for Epicor Kinetic, Beyond What Prism Covers

Add AI to Epicor Kinetic beyond Prism: private LLM over BAQs, BPM data, and REST v2, on-prem or private cloud, with honest guidance on when Prism already covers you.

Stuck on Epicor Kinetic?

Talk to engineers who work inside Epicor Kinetic every week, and who build private AI that answers these questions from your own ERP data.

Keep exploring

Free tools

ERP fixes and how-tos

AI on your ERP

Integrations

Comparisons

Software