ServiceNow's incident-CI relationship has two distinct patterns that developers constantly confuse. The cmdb_ci field holds a single "primary" CI reference, while multiple CIs are stored in the task_ci many-to-many table. Most developers try to query cmdb_ci directly for "all CIs" and miss the M2M relationship entirely. The platform doesn't automatically sync these β an incident can have a primary CI that's not in the M2M table, or M2M CIs with no primary CI set.
When to use this pattern
- Building CMDB impact analysis from incident data in server-side scripts
- Populating CI details in email notifications or business rule logic
- Validating CI relationships during incident creation or update workflows
- Synchronizing incident CI data with external ITSM tools that need complete CI context
When NOT to use this pattern
- Don't use this in client scripts β use GlideAjax to call a Script Include that does the server-side query
- Don't query
task_ciinside loops over incidents β you'll create N+1 query performance problems - Don't use this for simple CI counts β use GlideAggregate with
addAggregate('COUNT')instead - Don't use this in scheduled jobs processing thousands of incidents β batch the queries using
addQuery('task', 'IN', incidentSysIds)instead
Key behaviors and gotchas
- The
task_citable extends to all task types β filter bytask.sys_class_nameif you need incident-specific logic - Always check
ci.isValidRecord()β deleted CIs leave orphanedtask_cirecords that return invalid references - Use
getRefRecord()to get CI fields in one query β don't create separate GlideRecord instances per CI - Domain separation applies to both
task_cirecords and CI records β you may get partial results in domain-separated instances - The
cmdb_cifield may not appear in thetask_ciresults β they're managed independently by different UI components - Performance degrades after ~100 linked CIs per incident β consider if your data model is appropriate at that scale
Never assume the primary CI (cmdb_ci field) appears in the task_ci M2M table. Users can set a primary CI without adding it to the affected CIs list, creating inconsistent data that will break logic expecting them to match.
Enjoying this? Get one deep-dive per week.
Join 1,000+ ServiceNow pros β scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.