Skip to content
Release: Australia · Updated: 2026-03-12 · Official documentation · View source

SegmentHandler API- Scoped

Script include providing a chainable API for building an IDoc payload for a specific operation.

This script include requires the Zero Copy Connector for ERP (com.sn_erp_integration) store application and is provided within the sn_erp_integration namespace. For more information, refer to Zero Copy Connector for ERP.

Role required: sn_erp_integration.erp_user

The SegmentHandler class:

  • Owns the root data object and the IDoc segment schema.
  • Enables creating top-level segments. For example, E1EDK01, E1EDP01, and so on.

For handling lower-level instances of a segment, such as a header or item, refer to SegmentHandle API - Scoped.

See also sn_erp_integration API - Scoped, Global.

Parent Topic:Server API reference

SegmentHandler - SegmentHandler(String operationId)

Instantiates a new SegmentHandler object bound to an IDoc operation.

NameTypeDescription
operationIdStringThe sys\_id of a configured ERP model operation.Table: ERP model operation \[sn\_erp\_integration\_model\_operation\]

The following example shows how to build a header and item with nested segments in one fluent chain. The structure depends on the configured schema. The schema pattern is arrays of segments with nested arrays.

let obj = new sn_erp_integration.SegmentHandler('460d3ff2ff5de210d3a2fffffffffff8');

obj.addSegment("E1EDK01")
    .addField("BELNR", "1000")
    .addField("CURCY", "USD")
    .endSegment()
    .addSegment("E1EDP01")
    .addField("MENGE", "50")
    .addField("POSEX", "0010")
    .addSegment("E1EDP20")
    .addField("AMENG", "10")
    .addField("WMENG", "5")
    .addSegment("E1EDP19")
    .addField("IDTNR", "MAT01")
    .endSegment()
    .endSegment()
    .endSegment();

gs.info(JSON.stringify(obj.getData(), null, 2));

Output:

{
  "data": {
    "E1EDK01": [{
      "BELNR": "1000",
      "CURCY": "USD"
    }],
      "E1EDP01": [{
        "MENGE": "50",
        "POSEX": "0010",
        "E1EDP20": [{
          "AMENG": "10",
          "WMENG": "5",
          "E1EDP19": [{
            "IDTNR": "MAT01"
          }]
        }]
      }]
  }
}

SegmentHandler - addSegment(String segmentName, Object values)

Adds a predefined top-level segment of the type segmentName to the payload.

Note: The segment must be defined in the operation.

NameTypeDescription
segmentNameStringName of the top-level segment.
valuesObjectOptional implementation in place of the addField() method.
TypeDescription
ObjectSegmentHandle for the new segment instance.

The following example shows how to build a header and item with nested segments in one fluent chain. The structure depends on the configured schema. The schema pattern is arrays of segments with nested arrays.

let obj = new sn_erp_integration.SegmentHandler('460d3ff2ff5de210d3a2fffffffffff8');

obj.addSegment("E1EDK01")
    .addField("BELNR", "1000")
    .addField("CURCY", "USD")
    .endSegment()
    .addSegment("E1EDP01")
    .addField("MENGE", "50")
    .addField("POSEX", "0010")
    .addSegment("E1EDP20")
    .addField("AMENG", "10")
    .addField("WMENG", "5")
    .addSegment("E1EDP19")
    .addField("IDTNR", "MAT01")
    .endSegment()
    .endSegment()
    .endSegment();

gs.info(JSON.stringify(obj.getData(), null, 2));

Output:

{
  "data": {
    "E1EDK01": [{
      "BELNR": "1000",
      "CURCY": "USD"
    }],
      "E1EDP01": [{
        "MENGE": "50",
        "POSEX": "0010",
        "E1EDP20": [{
          "AMENG": "10",
          "WMENG": "5",
          "E1EDP19": [{
            "IDTNR": "MAT01"
          }]
        }]
      }]
  }
}

SegmentHandler - getData()

Gets the built segment payload.

NameTypeDescription
None  
TypeDescription
ObjectObject containing segment data.
dataWrapper for the payload object containing all top-level and nested segments.`{ data: }` The exact structure depends on the configured schema, but the pattern \(arrays of segments with nested arrays\) is the same. Data type: Object

The following example shows how to build a header and item with nested segments in one fluent chain. The structure depends on the configured schema. The schema pattern is arrays of segments with nested arrays.

let obj = new sn_erp_integration.SegmentHandler('460d3ff2ff5de210d3a2fffffffffff8');

obj.addSegment("E1EDK01")
    .addField("BELNR", "1000")
    .addField("CURCY", "USD")
    .endSegment()
    .addSegment("E1EDP01")
    .addField("MENGE", "50")
    .addField("POSEX", "0010")
    .addSegment("E1EDP20")
    .addField("AMENG", "10")
    .addField("WMENG", "5")
    .addSegment("E1EDP19")
    .addField("IDTNR", "MAT01")
    .endSegment()
    .endSegment()
    .endSegment();

gs.info(JSON.stringify(obj.getData(), null, 2));

Output:

{
  "data": {
    "E1EDK01": [{
      "BELNR": "1000",
      "CURCY": "USD"
    }],
      "E1EDP01": [{
        "MENGE": "50",
        "POSEX": "0010",
        "E1EDP20": [{
          "AMENG": "10",
          "WMENG": "5",
          "E1EDP19": [{
            "IDTNR": "MAT01"
          }]
        }]
      }]
  }
}

sndocs is an independent community mirror and is not affiliated with or endorsed by ServiceNow.

ServiceNow, the ServiceNow logo, Now, and other ServiceNow marks are trademarks and/or registered trademarks of ServiceNow, Inc., in the United States and/or other countries. Other company and product names may be trademarks of the respective companies with which they are associated.

© 2026 ServiceNow, Inc. All rights reserved.

Documentation content is redistributed under the Apache License 2.0 from the ServiceNowDocs repository.