{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":914,"path":"api-reference/server-api-reference/SegmentHandlerAPIScoped.md","title":"SegmentHandler API - Scoped","bundle":null,"doc_type":null,"release":"australia","body":"\n# SegmentHandler API- Scoped\n\nScript include providing a chainable API for building an IDoc payload for a specific operation.\n\nThis 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](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/integrate-applications/erp-integration-overview.md).\n\nRole required: sn\\_erp\\_integration.erp\\_user\n\nThe SegmentHandler class:\n\n-   Owns the root data object and the IDoc segment schema.\n-   Enables creating top-level segments. For example, `E1EDK01`, `E1EDP01`, and so on.\n\nFor handling lower-level instances of a segment, such as a header or item, refer to [SegmentHandle API - Scoped](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/SegmentHandleAPIScoped.md).\n\nSee also [sn\\_erp\\_integration API - Scoped, Global](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/sn_erp_integrationBothAPI.md).\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## SegmentHandler - SegmentHandler\\(String operationId\\)\n\nInstantiates a new SegmentHandler object bound to an IDoc operation.\n\n<table id=\"table_qlv_2gw_3hc\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\noperationId\n\n</td><td>\n\nString\n\n</td><td>\n\nThe sys\\_id of a configured ERP model operation.Table: ERP model operation \\[sn\\_erp\\_integration\\_model\\_operation\\]\n\n</td></tr></tbody>\n</table>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.\n\n```\nlet obj = new sn_erp_integration.SegmentHandler('460d3ff2ff5de210d3a2fffffffffff8');\n\nobj.addSegment(\"E1EDK01\")\n    .addField(\"BELNR\", \"1000\")\n    .addField(\"CURCY\", \"USD\")\n    .endSegment()\n    .addSegment(\"E1EDP01\")\n    .addField(\"MENGE\", \"50\")\n    .addField(\"POSEX\", \"0010\")\n    .addSegment(\"E1EDP20\")\n    .addField(\"AMENG\", \"10\")\n    .addField(\"WMENG\", \"5\")\n    .addSegment(\"E1EDP19\")\n    .addField(\"IDTNR\", \"MAT01\")\n    .endSegment()\n    .endSegment()\n    .endSegment();\n\ngs.info(JSON.stringify(obj.getData(), null, 2));\n```\n\nOutput:\n\n```\n{\n  \"data\": {\n    \"E1EDK01\": [{\n      \"BELNR\": \"1000\",\n      \"CURCY\": \"USD\"\n    }],\n      \"E1EDP01\": [{\n        \"MENGE\": \"50\",\n        \"POSEX\": \"0010\",\n        \"E1EDP20\": [{\n          \"AMENG\": \"10\",\n          \"WMENG\": \"5\",\n          \"E1EDP19\": [{\n            \"IDTNR\": \"MAT01\"\n          }]\n        }]\n      }]\n  }\n}\n```\n\n## SegmentHandler - addSegment\\(String segmentName, Object values\\)\n\nAdds a predefined top-level segment of the type segmentName to the payload.\n\n**Note:** The segment must be defined in the operation.\n\n|Name|Type|Description|\n|----|----|-----------|\n|segmentName|String|Name of the top-level segment.|\n|values|Object|Optional implementation in place of the addField\\(\\) method.|\n\n|Type|Description|\n|----|-----------|\n|Object|[SegmentHandle](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/SegmentHandleAPIScoped.md) for the new segment instance.|\n\nThe 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.\n\n```\nlet obj = new sn_erp_integration.SegmentHandler('460d3ff2ff5de210d3a2fffffffffff8');\n\nobj.addSegment(\"E1EDK01\")\n    .addField(\"BELNR\", \"1000\")\n    .addField(\"CURCY\", \"USD\")\n    .endSegment()\n    .addSegment(\"E1EDP01\")\n    .addField(\"MENGE\", \"50\")\n    .addField(\"POSEX\", \"0010\")\n    .addSegment(\"E1EDP20\")\n    .addField(\"AMENG\", \"10\")\n    .addField(\"WMENG\", \"5\")\n    .addSegment(\"E1EDP19\")\n    .addField(\"IDTNR\", \"MAT01\")\n    .endSegment()\n    .endSegment()\n    .endSegment();\n\ngs.info(JSON.stringify(obj.getData(), null, 2));\n```\n\nOutput:\n\n```\n{\n  \"data\": {\n    \"E1EDK01\": [{\n      \"BELNR\": \"1000\",\n      \"CURCY\": \"USD\"\n    }],\n      \"E1EDP01\": [{\n        \"MENGE\": \"50\",\n        \"POSEX\": \"0010\",\n        \"E1EDP20\": [{\n          \"AMENG\": \"10\",\n          \"WMENG\": \"5\",\n          \"E1EDP19\": [{\n            \"IDTNR\": \"MAT01\"\n          }]\n        }]\n      }]\n  }\n}\n```\n\n## SegmentHandler - getData\\(\\)\n\nGets the built segment payload.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n<table id=\"table_rkt_53x_3hc\" class=\"returns\"><thead><tr><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nObject\n\n</td><td>\n\nObject containing segment data.\n\n</td></tr><tr><td>\n\ndata\n\n</td><td>\n\nWrapper for the payload object containing all top-level and nested segments.`{ data: <payloadObject> }`\n\nThe exact structure depends on the configured schema, but the pattern \\(arrays of segments with nested arrays\\) is the same.\n\nData type: Object\n\n</td></tr></tbody>\n</table>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.\n\n```\nlet obj = new sn_erp_integration.SegmentHandler('460d3ff2ff5de210d3a2fffffffffff8');\n\nobj.addSegment(\"E1EDK01\")\n    .addField(\"BELNR\", \"1000\")\n    .addField(\"CURCY\", \"USD\")\n    .endSegment()\n    .addSegment(\"E1EDP01\")\n    .addField(\"MENGE\", \"50\")\n    .addField(\"POSEX\", \"0010\")\n    .addSegment(\"E1EDP20\")\n    .addField(\"AMENG\", \"10\")\n    .addField(\"WMENG\", \"5\")\n    .addSegment(\"E1EDP19\")\n    .addField(\"IDTNR\", \"MAT01\")\n    .endSegment()\n    .endSegment()\n    .endSegment();\n\ngs.info(JSON.stringify(obj.getData(), null, 2));\n```\n\nOutput:\n\n```\n{\n  \"data\": {\n    \"E1EDK01\": [{\n      \"BELNR\": \"1000\",\n      \"CURCY\": \"USD\"\n    }],\n      \"E1EDP01\": [{\n        \"MENGE\": \"50\",\n        \"POSEX\": \"0010\",\n        \"E1EDP20\": [{\n          \"AMENG\": \"10\",\n          \"WMENG\": \"5\",\n          \"E1EDP19\": [{\n            \"IDTNR\": \"MAT01\"\n          }]\n        }]\n      }]\n  }\n}\n```\n\n"}}