> For the complete documentation index, see [llms.txt](https://triton.pharos.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://triton.pharos.ai/methods-reference/apex-methods-reference/tritonbuilder.md).

# TritonBuilder

The TritonBuilder class provides a comprehensive, fluent API for creating structured log records in Salesforce.

## Overview

`TritonBuilder` is the fluent builder behind every Triton log. It wraps the managed [`pharos.LogBuilder`](https://triton.pharos.ai/methods-reference/apex-methods-reference/logbuilder.md) with a friendlier, strongly-typed API for setting category, type, area, level, related objects, spans, limits, and other metadata. Obtain one with [`Triton.makeBuilder()`](https://triton.pharos.ai/methods-reference/apex-methods-reference/triton.md#makebuilder) or [`Triton.t`](https://triton.pharos.ai/methods-reference/apex-methods-reference/triton.md#t), then hand it to a [logging method](https://triton.pharos.ai/methods-reference/apex-methods-reference/triton.md#logging-methods).

{% code lineNumbers="true" %}

```apex
Triton.log(Triton.makeBuilder()
    .type(TritonTypes.Type.Backend)
    .area(TritonTypes.Area.Accounts)
    .summary('Process completed')
    .info());
```

{% endcode %}

### Constructor

#### TritonBuilder()

* **Description**: Creates a new builder, seeding the created timestamp and request ID. In practice, prefer `Triton.makeBuilder()`, which also defaults the category to `Apex`.
* **Usage**:

  <pre class="language-apex" data-line-numbers><code class="lang-apex">TritonBuilder builder = new TritonBuilder();
  </code></pre>

## Classification

#### category(TritonTypes.Category c)

* **Description**: Sets the log category from the [`Category` enum](https://triton.pharos.ai/methods-reference/apex-methods-reference/tritontypes.md#category).
* **Returns**: `TritonBuilder`
* **Usage**: `builder.category(TritonTypes.Category.Apex);`

#### type(TritonTypes.Type t)

* **Description**: Sets the log type from the [`Type` enum](https://triton.pharos.ai/methods-reference/apex-methods-reference/tritontypes.md#type).
* **Returns**: `TritonBuilder`
* **Usage**: `builder.type(TritonTypes.Type.Backend);`

#### type(String t)

* **Description**: Sets the log type to a raw string. Used when the type comes from an exception or a Flow.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.type('CalloutException');`

#### area(TritonTypes.Area a)

* **Description**: Sets the functional area from the [`Area` enum](https://triton.pharos.ai/methods-reference/apex-methods-reference/tritontypes.md#area).
* **Returns**: `TritonBuilder`
* **Usage**: `builder.area(TritonTypes.Area.Community);`

#### area(String a)

* **Description**: Sets the functional area to a raw string. Used from Flows.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.area('CustomArea');`

#### level(TritonTypes.Level l)

* **Description**: Sets the log level from the [`Level` enum](https://triton.pharos.ai/core-concepts/log-levels.md).
* **Returns**: `TritonBuilder`
* **Usage**: `builder.level(TritonTypes.Level.INFO);`

## Level Shorthands

Each level has a no-argument form that sets the level, and a single-string overload that also sets the summary. `error()` additionally requests an issue. That request follows the final level: if a later `.level(...)` or `.warning()` — or a `Triton.warning(...)` call — leaves the log below ERROR, the issue is withdrawn at build time.

{% code lineNumbers="true" %}

```apex
public TritonBuilder error()   // + an issue, while the level stays ERROR
public TritonBuilder error(String summary)
public TritonBuilder warning()
public TritonBuilder warning(String summary)
public TritonBuilder info()
public TritonBuilder info(String summary)
public TritonBuilder debug()
public TritonBuilder debug(String summary)
public TritonBuilder fine()
public TritonBuilder fine(String summary)
public TritonBuilder finer()
public TritonBuilder finer(String summary)
public TritonBuilder finest()
public TritonBuilder finest(String summary)
```

{% endcode %}

* **Returns**: `TritonBuilder`
* **Usage**:

  <pre class="language-apex" data-line-numbers><code class="lang-apex">builder.error('Payment gateway timeout');
  builder.info();
  </code></pre>

## Content

#### summary(String value)

* **Description**: Sets the log summary. Values longer than 255 characters are truncated for the Summary field; the full text is appended to Details automatically at build time.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.summary('Log Summary');`

#### details(String value)

* **Description**: Sets the log details.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.details('Detailed log information');`

#### stackTrace(String stack)

* **Description**: Sets the stack trace. When omitted, one is captured automatically at log time.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.stackTrace('Stack trace details');`

#### operation(String operation)

* **Description**: Sets the operation (Apex Name). When omitted, it is derived from the stack trace.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.operation('AccountService.recalculate');`

#### action(String action)

* **Description**: Sets the action name — e.g. a performance mark, backend call, or user-interaction name. Writes to the `Action__c` field.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.action('loadAccounts');`

## Exceptions & DML Results

#### exception(Exception e)

* **Description**: Configures the builder from an exception: sets the type to the exception's type name (falling back to `Backend`), level to `ERROR`, summary to the message, details and stack trace from the exception, and requests an issue — withdrawn if the final level ends up below ERROR. No-op if `e` is null.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.exception(e);`

#### dmlResults(List\<Object> dmlResults)

* **Description**: Inspects a list of DML result objects. When any row failed, configures the builder as a `DMLResult` log at `ERROR` — with a summary of the failure count, all error messages in details, the failed record IDs as related objects, and an issue request (withdrawn if the final level ends up below ERROR). When every row succeeded, or the list is null or empty, it marks the builder as suppressed, and whichever method you pass it to (`Triton.log`, `logNow`, `error`, `warning`, …) emits nothing. Set the area via template or explicitly before calling; set any custom summary **after** it, since a failure overwrites the summary.
* **Parameters**: `dmlResults` — a `List` of `Database.SaveResult`, `DeleteResult`, `UpsertResult`, `UndeleteResult`, or `MergeResult`
* **Returns**: `TritonBuilder`
* **Usage**:

  <pre class="language-apex" data-line-numbers><code class="lang-apex">List&#x3C;Database.SaveResult> results = Database.insert(accounts, false);
  Triton.log(Triton.t
      .area(TritonTypes.Area.Accounts)
      .dmlResults(results));
  </code></pre>

#### isSuppressed()

* **Description**: `true` when `dmlResults(...)` found nothing to report — every row succeeded, or the list was null or empty. `Triton` checks it and emits nothing for the builder.
* **Returns**: `Boolean`

## Tracing & Spans

#### spanId(String spanId)

* **Description**: Sets the span ID (`Span_Id__c`) identifying this unit of work. When not set, the request ID is used as a fallback for Apex logs — inside an open [performance mark](https://triton.pharos.ai/common-apex-usage-patters/performance-marks.md) the log gets its own span id instead. A value you set here always wins over either default.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.spanId(mySpanId);`

#### parentSpanId(String parentSpanId)

* **Description**: Sets the parent span ID (`Parent_Span_Id__c`), linking this log to its parent span in a trace hierarchy. Left unset, it stays null — except inside an open [performance mark](https://triton.pharos.ai/common-apex-usage-patters/performance-marks.md), where the enclosing mark's span is stamped on it automatically. A value you set here is never overwritten.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.parentSpanId(parentSpanId);`

#### traceable(Boolean traceable)

* **Description**: Enables (or disables) trace-point capture for this builder. When set to `true`, an issue is also requested explicitly, so it stands at any level.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.traceable(true);`

#### tracePoint()

* **Description**: Records a Pharos trace point. Only has an effect when the builder is `traceable`.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.tracePoint();`

## Context

#### apexExecutionContext()

* **Description**: Captures the current Salesforce execution context and appends it to details at build time. For triggers: operation type, sObject, and batch size. For async contexts (batch, queueable, scheduled, future): the context type. Applied automatically for Apex-category logs during logging.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.apexExecutionContext();`

#### userId(Id userId)

* **Description**: Sets the user ID associated with the log.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.userId(UserInfo.getUserId());`

#### relatedObject(Id objectId) / relatedObject(String objectId)

* **Description**: Sets the related record to this single ID, **replacing** any related records already on the builder, including a template's. A null or invalid ID is ignored and leaves the existing set in place.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.relatedObject(myAccount.Id);`

#### relatedObjects(List\<Id> / List\<String> / Set\<String> / Set\<Id>)

* **Description**: Sets the related records to these IDs, **replacing** any already on the builder, including a template's — pass every ID in one call. Invalid string IDs are filtered out.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.relatedObjects(new Set<Id>{a.Id, b.Id});`

#### transactionId(String transactionId)

* **Description**: Sets the transaction ID for cross-context correlation. Usually applied automatically from `Triton.TRANSACTION_ID`.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.transactionId('12345-transaction-id');`

#### createIssue()

* **Description**: Flags the log so Pharos creates (or associates) an Issue. Custom logs create no issue by default. An explicit `createIssue()` stands at any level, unlike the issue implied by `error()`, `exception(e)`, or a failed `dmlResults(...)`.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.createIssue();`

#### createdTimestamp(Double timestamp) / createdTimestamp()

* **Description**: Sets the created timestamp. The no-argument form uses the current time (called automatically in the constructor).
* **Returns**: `TritonBuilder`
* **Usage**: `builder.createdTimestamp(Double.valueOf(System.now().getTime()));`

#### duration(Decimal duration)

* **Description**: Sets the duration (milliseconds) for the log.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.duration(123.45);`

## Flow & Integration

#### interviewGuid(String guid)

* **Description**: Sets the Flow interview GUID.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.interviewGuid('interview-guid');`

#### flowApiName(String apiName)

* **Description**: Sets the Flow API name. Use the **plain** API name: component-scoped `Log_Level__mdt` rules match it in full, so a version-suffixed value like `My_Flow-4` won't match a rule naming `My_Flow`. The version goes in [`flowVersion()`](#flowversion-decimal-version).
* **Returns**: `TritonBuilder`
* **Usage**: `builder.flowApiName('My_Flow');`

#### flowVersion(Decimal version)

* **Description**: Sets the Flow version, written to `pharos__Flow_Version__c`. The managed field is Text, so the number is stored in its canonical string form. Pairs with [`flowApiName()`](#flowapiname-string-apiname), which should carry the plain API name — see [Logging in Flow](https://triton.pharos.ai/flows.md#flow-api-name-and-flow-version).
* **Returns**: `TritonBuilder`
* **Usage**: `builder.flowVersion(4);`

#### integrationPayload(HttpRequest request, HttpResponse response)

* **Description**: Serializes an outbound HTTP request/response pair onto the log for integration logging.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.integrationPayload(request, response);`

#### integrationPayload(RestRequest request, RestResponse response)

* **Description**: Serializes an inbound REST request/response pair onto the log.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.integrationPayload(restRequest, restResponse);`

#### postProcessing(TritonHelper.PostProcessingControlsBuilder postProcessingBuilder)

* **Description**: Attaches Pharos post-processing controls built with [`PostProcessingControlsBuilder`](https://triton.pharos.ai/methods-reference/apex-methods-reference/tritonhelper/postprocessingcontrolsbuilder.md).
* **Returns**: `TritonBuilder`
* **Usage**:

  <pre class="language-apex" data-line-numbers><code class="lang-apex">builder.postProcessing(Triton.makePostProcessingBuilder().userInfo(true));
  </code></pre>

## Limit Info

#### limitInfo()

* **Description**: Captures current governor usage and limits as attributes. Captures essential limits (SOQL, DML, CPU, heap, callouts); when [`withFullLimitInfo()`](#withfulllimitinfo) was called, also captures extended limits (aggregates, SOSL, future calls, queueable jobs, email invocations, push notifications, batch jobs, publish-immediate DML). No-op when limit info is disabled. Applied automatically during logging.
* **Returns**: `TritonBuilder`

#### limitInfoLite()

* **Description**: Captures only the essential governor limits.
* **Returns**: `TritonBuilder`

#### withFullLimitInfo()

* **Description**: Requests that extended limits be captured in addition to essential limits when `limitInfo()` runs.
* **Returns**: `TritonBuilder`

#### withoutLimitInfo()

* **Description**: Disables limit-info capture for this log.
* **Returns**: `TritonBuilder`

## Low-Level

#### attribute(String name, Object value)

* **Description**: Sets any field on the underlying log record directly. Use the [field-name constants](#field-name-constants) for well-known fields.
* **Returns**: `TritonBuilder`
* **Usage**: `builder.attribute(TritonBuilder.ACTION, 'loadAccounts');`

#### cloneBuilder()

* **Description**: Returns a deep copy of the builder (populated fields plus configuration flags). Used internally by templates.
* **Returns**: `TritonBuilder`

#### prepareForLogging(String transactionId)

* **Description**: Enriches the builder just before buffering — sets the transaction ID, auto-captures stack trace and operation if missing, records a trace point, captures limit info, and appends Apex execution context. Called internally by `Triton.log()`; you rarely call it directly.
* **Returns**: `TritonBuilder`

#### build()

* **Description**: Builds and returns the final `pharos__Log__c` record, applying defaults (category, summary carryover, execution context).
* **Returns**: `pharos__Log__c`
* **Usage**: `pharos__Log__c logRecord = builder.build();`

## Field-Name Constants

Public constants for the log fields set by the builder. Pass them to [`attribute()`](#attribute-string-name-object-value) when setting a field manually.

| Constant                  | Field API Name                       |
| ------------------------- | ------------------------------------ |
| `APEX_NAME`               | `pharos__Apex_Name__c`               |
| `ACTION`                  | `Action__c`                          |
| `CREATED_TIMESTAMP`       | `pharos__Created_Timestamp__c`       |
| `DURATION`                | `pharos__Duration__c`                |
| `INTERVIEW_GUID`          | `pharos__Interview_GUID_External__c` |
| `LOG_LEVEL`               | `Log_Level__c`                       |
| `RELATED_ID`              | `pharos__Related_Id__c`              |
| `RELATED_OBJECTS`         | `pharos__Related_Objects__c`         |
| `STACKTRACE_PARSE_RESULT` | `pharos__Stacktrace_Parse_Result__c` |
| `USER_ID`                 | `pharos__User_Id__c`                 |
| `FLOW_API_NAME`           | `pharos__Flow_API_Name__c`           |
| `FLOW_VERSION`            | `pharos__Flow_Version__c`            |
| `DO_NOT_CREATE_ISSUE`     | `pharos__Do_Not_Create_Issue__c`     |
| `REQUEST_ID`              | `pharos__Request_Id_External__c`     |
| `SPAN_ID`                 | `Span_Id__c`                         |
| `PARENT_SPAN_ID`          | `Parent_Span_Id__c`                  |
