wiki/knowledge/salesforce/quarra-apex-trigger-task-comments.md · 741 words · 2026-04-05

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: [1]


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:


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


Sources

  1. 2026 04 02 Quarra Salesforce Working Call
  2. Quarra Opportunity Modal Duplication
  3. Index
  4. Iterative Prompting Patterns