Fix Autotask ticket propagation to new runs

When a new run is created, Autotask tickets were not being propagated
if the associated internal ticket was resolved. This caused users to
have to manually re-link tickets on each new run.

The previous implementation relied on finding an open internal ticket
first, then using its ticket code to find a matching Autotask-linked run.
If the internal ticket was resolved, the Autotask propagation would fail.

This commit implements a two-strategy approach:
1. Strategy 1: Use internal ticket code (existing logic, improved error handling)
2. Strategy 2: Direct Autotask propagation - find most recent non-deleted
   Autotask ticket for the job, independent of internal ticket status

Now Autotask tickets remain linked across runs regardless of internal
ticket resolution status, matching the behavior of internal tickets.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Ivo Oskamp 2026-02-10 09:32:23 +01:00
parent 596fc94e69
commit f3b1b56b6a
2 changed files with 27 additions and 4 deletions

View File

@ -213,12 +213,11 @@ def link_open_internal_tickets_to_run(*, run: JobRun, job: Job) -> None:
except Exception:
pass
# Strategy 1: Use internal ticket code to find matching Autotask-linked run
try:
# Use the newest ticket code to find a matching prior Autotask-linked run.
newest_code = (rows[0][1] or "").strip()
if not newest_code:
return
newest_code = (rows[0][1] or "").strip() if rows else ""
if newest_code:
prior = (
JobRun.query.filter(JobRun.job_id == job.id)
.filter(JobRun.autotask_ticket_id.isnot(None))
@ -226,6 +225,25 @@ def link_open_internal_tickets_to_run(*, run: JobRun, job: Job) -> None:
.order_by(JobRun.id.desc())
.first()
)
if prior and getattr(prior, "autotask_ticket_id", None):
run.autotask_ticket_id = prior.autotask_ticket_id
run.autotask_ticket_number = prior.autotask_ticket_number
run.autotask_ticket_created_at = getattr(prior, "autotask_ticket_created_at", None)
run.autotask_ticket_created_by_user_id = getattr(prior, "autotask_ticket_created_by_user_id", None)
return
except Exception:
pass
# Strategy 2: Direct Autotask propagation (independent of internal ticket status)
# Find the most recent non-deleted Autotask ticket for this job and propagate it.
try:
prior = (
JobRun.query.filter(JobRun.job_id == job.id)
.filter(JobRun.autotask_ticket_id.isnot(None))
.filter(JobRun.autotask_ticket_deleted_at.is_(None))
.order_by(JobRun.id.desc())
.first()
)
if prior and getattr(prior, "autotask_ticket_id", None):
run.autotask_ticket_id = prior.autotask_ticket_id
run.autotask_ticket_number = prior.autotask_ticket_number

View File

@ -2,6 +2,11 @@
This file documents all changes made to this project via Claude Code.
## [2026-02-10]
### Fixed
- Fixed Autotask ticket not being automatically linked to new runs when internal ticket is resolved by implementing independent Autotask propagation strategy (now checks for most recent non-deleted Autotask ticket on job regardless of internal ticket status, ensuring PSA ticket reference persists across runs)
## [2026-02-09]
### Added