KafkaProducerAPI - Scoped

concept australia Updated Sep 15, 2026 3 min read KafkaProducerAPIScoped.md
Raw file servicenow.com

The KafkaProducerAPI provides a method to produce messages from server-side scripts to an Apache Kafka topic.

To access this API, the ServiceNow IntegrationHub Action Step - Kafka Producer plugin (com.glide.hub.action_step.kafka) must be activated. This API runs in the sn_ih_kafka namespace.

Use the send() method to deliver a message to a Kafka topic. Before calling send(), a topic alias record for the target Kafka topic must exist in the Topic Alias [sys_sc_topic_alias] table.

Permission for sending messages to a given topic is enforced by access control on the topic alias record.

For more information about working with Kafka topics, see Using Stream Connect for Apache Kafka.

Parent Topic:Server API reference

KafkaProducerAPI - KafkaProducerAPI()

Instantiates a KafkaProducerAPI.

Name Type Description
None

Creates an instance of KafkaProducerAPI.

JavaScript
var kafkaProducerAPI = new sn_ih_kafka.KafkaProducerAPI();

KafkaProducerAPI - send(Object params)

Sends a message to a Kafka topic, optionally running a script include if an asynchronous send fails.

Before calling this method for asynchronous message delivery, create a script include to handle delivery failures. When the script include executes, an array of error objects (callbackErrors) is passed to the callback function. Each error object represents one message delivery failure.

Name

Type

Description

context

String

The value passed as params.context in the original send() call for the failed message. Data type: String

errorMessage

String

Error message describing the reason for failure. Data type: String

errorType

String

The fully qualified class name for the exception. Data type: String

producerId

String

Sys_id of the producer event associated with the failed send.Table: Kafka Producers [sys_kafka_producer]

Data type: String

Name

Type

Description

params

Object

Object containing parameters to use for sending the message.``` { context: "String", headers: {Object}, isSync: Boolean, key: "String", message: "String", onErrorScriptIncludeId: "String", topicSysId: "String" }

Text
</td></tr><tr><td>
params.context
</td><td>
String
</td><td>
Optional. Caller-supplied context string to pass to the script include if an asynchronous send fails. Used to identify the message that triggered the failure.
</td></tr><tr><td>
params.headers
</td><td>
Object
</td><td>
Optional. Key-value pairs for message headers. Any headers supported by Apache Kafka are valid. For example:```{   "source": "order_update_br",   "correlation_id": "8a1c3f2b1b3a10"}

params.isSync

Boolean

Optional. Flag that indicates whether to send the message synchronously. Possible values:- true: Send synchronously and block for the result.

  • false: Send asynchronously. An error callback can be specified in onErrorScriptIncludeId to execute when an asynchronous send fails.

Default: false

params.key

String

Optional. Message key for partitioning in Apache Kafka. Messages with the same key are sent to the same partition, ensuring ordering.Default: Empty string

params.message

String

Content of the message to send to the topic.

params.onErrorScriptIncludeId

String

Optional. Sys_id of the script include to run if an asynchronous send fails. Default: None

Table: Script Includes [sys_script_include]

params.schemaSysId

String

Optional. Sys_id of a registered schema record to use for encoding the message.Default: The message is sent as plain text.

Table: Stream Connect Schemas [stream_connect_schema]

params.topicSysId

String

Sys_id of the target Kafka topic alias record.Table: Topic Alias [sys_sc_topic_alias]

Type

Description

Object

Map containing delivery result information.For synchronous delivery, the method returns after the message is delivered. For asynchronous delivery, the method returns immediately. The delivery may later succeed or fail. Asynchronous delivery failure can be handled using the provided onErrorScriptIncludeId.

JavaScript
{   delivery: "String",   offset: Number,   partition: Number}

<Object>.delivery

Delivery type.Possible values:

  • async: The message will be sent asynchronously.
  • sync: The message was sent synchronously.

Data type: String

<Object>.offset

Unique ID assigned to the message within the topic partition. Only returned for sync delivery.Data type: Number

<Object>.partition

Partition that the message was delivered to. Only returned for sync delivery.Data type: Number

This business rule sends an order-updated event asynchronously, routing any delivery failure to the example `KafkaSendFailureHandler` script include. Note that `KafkaSendFailureHandler` is shown only as an example; you must create your own script include to handle delivery failures.
JavaScript
var kafkaProducerAPI = new sn_ih_kafka.KafkaProducerAPI();
var response = kafkaProducerAPI.send({    topicSysId: '8a1c3f2b1b3a1010e7cfc51b1ce0bc42', // sys_id for 'orders.updated'    key: current.getUniqueValue(),    message: JSON.stringify({        order_id: current.getUniqueValue(),        status: current.getValue('status')    }),    isSync: false,    headers: { source: 'order_update_br' },    onErrorScriptIncludeId: 'b2e4a1c3f2b1a1010e7cfc51b1ce0c99', // sys_id of KafkaSendFailureHandler    context: current.getUniqueValue()});
gs.info('Kafka send submitted, delivery mode: ' + response.delivery);// Output:// Kafka send submitted, delivery mode: async

This script include reads callbackErrors and logs each delivery failure.

JavaScript
// Script include: KafkaSendFailureHandler(function callback(callbackErrors) {
    gs.info("callback size: ", callbackErrors.length);
    for (var i = 0; i < callbackErrors.length; i++) {        gs.info("callback " + callbackErrors[i].errorMessage + " " +            callbackErrors[i].errorType + " " +            JSON.stringify(callbackErrors[i].context) + " " +            callbackErrors[i].producerId);    }
})(callbackErrors);
Search documentation
Search document titles and file paths on the australia branch