Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
[This article is prerelease documentation and is subject to change.]
Important
This is a preview feature.
Preview features aren't meant for production use and might have restricted functionality. These features are subject to supplemental terms of use, and are available before an official release so that customers can get early access and provide feedback.
This article is primarily intended for product owners, domain experts, consultants, and developers who want to experiment with AI capabilities in Business Central using the Agent playground.
The Tasks AL API allows you to trigger agent tasks programmatically from AL code. This enables integration with UI actions, business events, and custom workflows. You can also simulate email-based triggers for testing scenarios from the UI. Learn more in Run a playground agent (preview).
Get information about the available playground agents
The first step to configure agents is to retrieve their user ID. The agent playground provides an API to retrieve all available agents.
local procedure GetAllAgents()
var
CustomAgent: Codeunit "Custom Agent";
TempAgentInfo: Record "Custom Agent Info" temporary;
begin
// Retrieve all custom agents
CustomAgent.GetCustomAgents(TempAgentInfo);
// Iterate through the agents
if TempAgentInfo.FindSet() then
repeat
// Use the agent information
// TempAgentInfo."User Security ID" contains the agent's user ID
// TempAgentInfo."User Name" contains the agent's user name
Message('Agent: %1 (ID: %2)', TempAgentInfo."User Name", TempAgentInfo."User Security ID");
until TempAgentInfo.Next() = 0;
end;
Using the Agent Task Builder API
The Agent Task Builder codeunit provides the primary interface for creating agent tasks. Here are common integration patterns:
Trigger from a page action
Add a page action that creates an agent task when clicked:
action(CreateAgentTask)
{
Caption = 'Send to Agent';
Image = Robot;
trigger OnAction()
var
AgentTaskBuilder: Codeunit "Agent Task Builder";
AgentTask: Record "Agent Task";
begin
AgentTask := AgentTaskBuilder
.Initialize(AgentUserSecurityId, 'Review Sales Order')
.SetExternalId('SO-' + Rec."No.")
.AddTaskMessage('Sales Team', 'Please review sales order ' + Rec."No." + ' for customer ' + Rec."Sell-to Customer Name")
.Create();
end;
}
Trigger from business events
Respond to system events like document posting:
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", 'OnAfterSalesInvHeaderInsert', '', false, false)]
local procedure OnAfterSalesInvoicePost(var SalesInvHeader: Record "Sales Invoice Header")
var
AgentTask: Record "Agent Task";
AgentTaskBuilder: Codeunit "Agent Task Builder";
AgentTaskMessageBuilder: Codeunit "Agent Task Message Builder";
begin
// Only create task for high-value invoices
if SalesInvHeader.Amount > 10000 then begin
AgentTaskMessageBuilder
.Initialize('System', 'New high-value invoice posted: ' + SalesInvHeader."No." + ' for customer ' + SalesInvHeader."Sell-to Customer Name" + '. Amount: ' + Format(SalesInvHeader.Amount))
.SetSkipMessageSanitization(true);
AgentTask := AgentTaskBuilder
.Initialize(AgentUserSecurityId, 'Review High-Value Invoice')
.SetExternalId('INV-' + SalesInvHeader."No.")
.AddTaskMessage(AgentTaskMessageBuilder)
.Create();
end;
end;
Simulate email-based triggers
For testing email integration scenarios, format the task message to include email metadata:
local procedure CreateEmailSimulationTask(FromAddress: Text; Subject: Text; Body: Text)
var
AgentTaskBuilder: Codeunit "Agent Task Builder";
AgentTaskMessageBuilder: Codeunit "Agent Task Message Builder";
MessageText: Text;
begin
MessageText := StrSubstNo('EMAIL FROM: %1\nSUBJECT: %2\n\n%3', FromAddress, Subject, Body);
AgentTaskMessageBuilder
.Initialize(FromAddress, MessageText)
.SetSkipMessageSanitization(true);
AgentTaskBuilder
.Initialize(AgentUserSecurityId, Subject)
.SetExternalId('EMAIL-' + Format(CurrentDateTime))
.AddTaskMessage(AgentTaskMessageBuilder)
.Create();
end;
Add attachments to tasks
When your task needs to include files for the agent to process:
local procedure CreateTaskWithAttachment()
var
AgentTaskBuilder: Codeunit "Agent Task Builder";
AgentTaskMessageBuilder: Codeunit "Agent Task Message Builder";
TempBlob: Codeunit "Temp Blob";
InStr: InStream;
begin
// Prepare your file content in InStr
TempBlob.CreateInStream(InStr);
AgentTaskMessageBuilder
.Initialize('User', 'Please process this document')
.AddAttachment('document.pdf', 'application/pdf', InStr);
AgentTaskBuilder
.Initialize(AgentUserSecurityId, 'Process Document')
.AddTaskMessage(AgentTaskMessageBuilder)
.Create();
end;
Add messages to existing tasks
Continue conversations by adding new messages to existing agent tasks:
local procedure AddMessageToExistingTask(ExternalId: Text; MessageText: Text)
var
AgentTaskRecord: Record "Agent Task";
AgentTask: Codeunit "Agent Task";
AgentTaskMessageBuilder: Codeunit "Agent Task Message Builder";
begin
// Get the existing task by its external ID
AgentTaskRecord := AgentTask.GetTaskByExternalId(AgentUserSecurityId, ExternalId);
// Add a new message to the task
AgentTaskMessageBuilder
.Initialize('User', MessageText)
.SetRequiresReview(true)
.AddToTask(AgentTaskRecord);
// Restart the task if it's completed or stopped
if AgentTask.CanSetStatusToReady(AgentTaskRecord) then
AgentTask.SetStatusToReady(AgentTaskRecord);
end;
This pattern is useful for:
- Providing additional information after the initial task creation
- Continuing multi-turn conversations with your agent
- Simulating email thread continuations
Manage task lifecycle
Monitor and control running tasks:
local procedure ManageAgentTask(var AgentTask: Record "Agent Task")
var
AgentTaskCU: Codeunit "Agent Task";
begin
// Check task status
if AgentTaskCU.IsTaskRunning(AgentTask) then
Message('Task is still running');
if AgentTaskCU.IsTaskCompleted(AgentTask) then
Message('Task completed successfully');
// Restart a stopped task
if AgentTaskCU.CanSetStatusToReady(AgentTask) then
AgentTaskCU.SetStatusToReady(AgentTask);
// Stop a running task
AgentTaskCU.StopTask(AgentTask, true);
end;
Best practices for API integration
- Use meaningful external IDs: Set unique external IDs that connect tasks to your business records (for example email thread IDs)
- Include context in messages: Provide all the specific details the agent needs to complete the task
- Handle task creation errors: Wrap task creation in error handling to manage exceptions gracefully
- Test with realistic data: Use actual business scenarios to validate your integration
- Monitor task completion: Implement follow-up logic based on task status changes
This API-based approach allows you to build automated workflows during the prototyping phase, which can later be refined when migrating to production features.
Related information
Overview (preview)
Agent playground (preview)
Create and activate (preview)
Write effective instructions (preview)
Instruction keywords (preview)
Set up permissions and profiles (preview)
Run a playground agent (preview)
Iterate and manage (preview)
Best practices (preview)
Create a Sales Validation Agent (preview)
Transparency note: Business Central agent playground