如何在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:
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:
- Navigate to System Definition > Business Rules and click
New. - 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)
- Name:
- 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; } - Save the rule, then test it by creating a new Incident and linking a KA—your
m2m_kb_taskrecord will auto-populate with the Incident data.
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}")
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:
- Go to System Definition > Tables, find the
m2m_kb_tasktable, and open its table designer. - Locate the
taskfield (the reference to Incident), open its dictionary entry. - Switch to the Auto-populate tab, click
New. - 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 inm2m_kb_task)
- Name:
- Save the rule—now whenever you select an Incident in the
taskfield, 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_tasktable). - Create custom fields in the target table first (like
u_incident_short_desc) if they don't exist already.
内容的提问来源于stack exchange,提问作者Pratik Haware

