---
title: Salesforce Apex Trigger — Task Comment Timestamping
type: article
created: '2026-04-05'
updated: '2026-04-05'
source_docs:
- raw/2026-04-02-quarra-salesforce-working-call-135070080.md
tags:
- salesforce
- apex
- ai-assisted-development
- quarra
- task-object
- triggers
layer: 2
client_source: null
industry_context: null
transferable: true
---

# Salesforce Apex Trigger — Task Comment Timestamping

## Overview

During a working session with the Quarra client, the team used an AI coding agent to build a custom Apex trigger that automatically prepends a timestamp and user initials to new entries in the Task object's Comments field. This article documents the approach, the platform limitations encountered, and the final workaround implemented.

**Client:** Quarra (Salesforce instance)
**Related meeting:** [[meetings/2026-04-02-quarra-salesforce-working-call]]

---

## Goal

When a user adds or edits a comment in the Task Comments field, the system should automatically:

1. Prepend the current date/time and the user's initials to the new comment text
2. Preserve all prior comments below a separator line
3. Present a clean editing experience so users know where to type

---

## Implementation

### Approach

An **Apex trigger** was deployed on the Task object. Apex is Salesforce's lowest-level proprietary language, sitting below declarative configuration and Flow. It fires server-side on record save, making it suitable for field manipulation that can't be done through the standard UI.

The trigger logic:
- Detects when the Comments field has been modified
- Identifies the genuinely new text added above the existing content
- Stamps that new text with `[date] [user initials]:` prefix
- Preserves prior entries below a separator

### Key Platform Constraint: Cursor Position

Salesforce Lightning's standard text area component controls cursor behavior at the browser level. **Apex cannot set cursor position.** When a user opens the edit form, the cursor always lands at the end of the field content — meaning users must manually scroll up and create a blank line before typing.

This is a hard platform limitation; no amount of trigger refinement can override Lightning's front-end behavior.

### Workaround: "Edit Comments" Quick Action Button

To improve UX and reduce confusion, a custom **Quick Action button** labeled "Edit Comments" was added to the Task page layout. This gives users a dedicated, clearly labeled entry point for adding comments, separate from the standard inline pencil-edit icon.

**Workflow for users:**
1. Click **Edit Comments**
2. Place cursor at the top of the field
3. Type new comment, press Enter once
4. Click **Save**

The trigger fires on save and stamps the entry automatically.

> **Action item:** Remove the default pencil edit icon from the Task layout to avoid confusion with the new Edit Comments button. (Owner: Mark Hope)

---

## AI-Assisted Development Notes

This feature was built entirely through iterative prompting of a Claude-based coding agent connected to the Salesforce API. Key observations:

- **Initial prompt was underspecified.** The first trigger treated the entire field value as new on every save, causing duplication and stacking of timestamps. Multiple refinement rounds were needed.
- **Iterative debugging is normal.** The agent tried several approaches, encountered Salesforce API versioning quirks, and self-corrected. Expect 30–45 minutes for a task like this even when it "should" be simple.
- **Be explicit upfront.** Specifying the desired behavior for *subsequent* edits (not just the first save) would have reduced iteration cycles.
- **Platform limits surface mid-build.** The cursor-positioning limitation only became apparent after the core trigger was working. Build in time to discover and work around constraints.
- **Cost vs. traditional dev:** Equivalent work via a contracted Salesforce developer (e.g., writing specs, sandbox testing, review cycles) would likely cost $5,000–$10,000 and take days. The AI agent completed it in under an hour at near-zero marginal cost.

---

## Salesforce Architecture Context

| Layer | Language/Tool | Used For |
|---|---|---|
| UI configuration | Setup / Object Manager | Fields, layouts, page assignments |
| Declarative logic | Flow / Process Builder | Conditional automation |
| Mid-level scripting | JSForce / Force.com | API-level data access |
| Low-level code | **Apex** | Triggers, complex logic, field manipulation |

Apex requires Salesforce to inspect and approve code before deployment. On heavily customized instances (like Quarra's), existing customizations can conflict with new triggers and slow deployment.

---

## Known Limitations & Open Items

- [ ] Remove pencil edit icon from Task layout to enforce the Edit Comments workflow (Mark Hope)
- [ ] User training needed: explain the "type at top, press Enter, save" convention
- [ ] The Opportunity modal duplication issue (separate from this trigger) was investigated in the same session but not fully resolved — Lightning pulls entire sections containing required fields, causing the duplicate display. See [[knowledge/salesforce/quarra-opportunity-modal-duplication]] if that article is created.

---

## Related

- [[clients/quarra/_index]]
- [[meetings/2026-04-02-quarra-salesforce-working-call]]
- [[knowledge/ai-assisted-development/iterative-prompting-patterns]]