You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ServiceNow中实现跨表列的数据获取?

Hey there! Let's break down how to pull data from one ServiceNow table column to another, tailored to your specific use case with Incidents, KA numbers, and the m2m_kb_task table. Here are the most practical methods you can use:

Method 1: Business Rules (No-Code/Low-Code, ServiceNow Native)

This is the go-to approach for automatic cross-table data syncing within ServiceNow. Let's set one up to pull Incident data into m2m_kb_task whenever a new record is created:

  1. Navigate to System Definition > Business Rules and click New.
  2. Configure the basic settings:
    • Name: Pull Incident Data to m2m_kb_task
    • Table: Knowledge Applied to Tasks (m2m_kb_task)
    • When: before insert (runs right before the record saves)
    • Condition: task IS NOT EMPTY (ensures we only run this when an Incident is linked)
  3. Switch to the Advanced tab, and paste this script into the Script field:
    // Fetch the linked Incident record
    var incidentRecord = new GlideRecord('incident');
    if (incidentRecord.get(current.task)) {
        // Copy fields from Incident to m2m_kb_task (adjust field names to match your setup)
        current.u_incident_number = incidentRecord.number;
        current.u_incident_short_desc = incidentRecord.short_description;
        current.u_incident_priority = incidentRecord.priority;
    }
    
  4. Save the rule, then test it by creating a new Incident and linking a KA—your m2m_kb_task record will auto-populate with the Incident data.
Method 2: Extend Your Existing Python Script

Since you're already using a Python script to assign KA numbers, you can modify it to fetch Incident data first, then include it when creating the m2m_kb_task record. Here's a quick example using the ServiceNow REST API:

import requests
from requests.auth import HTTPBasicAuth

# Your ServiceNow instance credentials
INSTANCE = "your-instance-name"
USERNAME = "your-service-now-username"
PASSWORD = "your-service-now-password"

# Assume you already have these values from your existing workflow
incident_sys_id = "abc123xyz..."  # Sys ID of the target Incident
kb_article_sys_id = "def456uvw..."  # Sys ID of the assigned KA

# Step 1: Fetch data from the linked Incident
incident_url = f"https://{INSTANCE}.service-now.com/api/now/table/incident/{incident_sys_id}"
headers = {"Accept": "application/json"}
incident_response = requests.get(
    incident_url,
    auth=HTTPBasicAuth(USERNAME, PASSWORD),
    headers=headers
)
incident_data = incident_response.json()["result"]

# Step 2: Create the m2m_kb_task record with Incident data included
m2m_payload = {
    "task": incident_sys_id,
    "kb_knowledge": kb_article_sys_id,
    "u_incident_number": incident_data["number"],
    "u_incident_short_desc": incident_data["short_description"]
}

m2m_url = f"https://{INSTANCE}.service-now.com/api/now/table/m2m_kb_task"
m2m_response = requests.post(
    m2m_url,
    auth=HTTPBasicAuth(USERNAME, PASSWORD),
    headers=headers,
    json=m2m_payload
)

if m2m_response.status_code == 201:
    print("m2m record created successfully with Incident data")
else:
    print(f"Failed to create m2m record: {m2m_response.text}")
Method 3: Auto-Populate via Reference Field Configuration

If you just need fields to fill in automatically when selecting an Incident in the m2m_kb_task form (no background sync), use this no-code setup:

  1. Go to System Definition > Tables, find the m2m_kb_task table, and open its table designer.
  2. Locate the task field (the reference to Incident), open its dictionary entry.
  3. Switch to the Auto-populate tab, click New.
  4. Configure the auto-populate rule:
    • Name: Populate Incident Short Description
    • Source field: short_description (from the Incident table)
    • Target field: u_incident_short_desc (your custom field in m2m_kb_task)
  5. Save the rule—now whenever you select an Incident in the task field, the target field will auto-fill with the Incident's short description.

Quick Notes:

  • Make sure you have the necessary permissions (read access to Incident table, write access to m2m_kb_task table).
  • Create custom fields in the target table first (like u_incident_short_desc) if they don't exist already.

内容的提问来源于stack exchange,提问作者Pratik Haware

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:12:47