Chilkat Online Tools

SQL Server / Coupa Postman Collection - OAuth - Master / Create External PO w/ Allocations

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    -- Use this online tool to generate code from sample XML: Generate Code to Create XML

    -- The following XML is sent in the request body.

    -- <?xml version="1.0" encoding="UTF-8"?>
    -- <order-header>
    --     <type>ExternalOrderHeader</type>
    --     <po-number>PO-957645-18</po-number>
    --     <version type="integer">1</version>
    --     <payment-method>invoice</payment-method>
    --     <ship-to-address>
    --         <location-code>DUMMYST</location-code>
    --     </ship-to-address>
    --     <ship-to-user>
    --         <login>CoupaRequester</login>
    --     </ship-to-user>
    --     <supplier>
    --         <name>Adam's Apples</name>
    --     </supplier>
    --     <payment-term>
    --         <code>2/10 Net 30</code>
    --     </payment-term>
    --     <currency>
    --         <code>USD</code>
    --     </currency>
    --     <order-lines type="array">
    --         <order-line>
    --             <description>Supplies</description>
    --             <line-num>1</line-num>
    --             <price type="decimal">40.00</price>
    --             <quantity type="decimal">1.0</quantity>
    --             <type>OrderQuantityLine</type>
    --             <account>
    --                 <code>SF-Marketing-Assets</code>
    --                 <account-type>
    --                     <name>Ace Corporate</name>
    --                 </account-type>
    --             </account>
    --             <currency>
    --                 <code>USD</code>
    --             </currency>
    --             <uom>
    --                 <code>EA</code>
    --             </uom>
    --         </order-line>
    --         <order-line>
    --             <description>Testing Kit</description>
    --             <line-num>2</line-num>
    --             <price type="decimal">100.00</price>
    --             <quantity type="decimal">1.0</quantity>
    --             <type>OrderQuantityLine</type>
    --             <account-allocations type="array">
    --                 <account-allocation>
    --                     <amount type="decimal">10.00</amount>
    --                     <pct type="decimal">10.0</pct>
    --                     <account>
    --                         <code>SF-IT-Assets</code>
    --                         <account-type>
    --                             <name>Ace Corporate</name>
    --                         </account-type>
    --                     </account>
    --                 </account-allocation>
    --                 <account-allocation>
    --                     <amount type="decimal">90.00</amount>
    --                     <pct type="decimal">90.0</pct>
    --                     <account>
    --                         <code>SF-Sales-Assets</code>
    --                         <account-type>
    --                             <name>Ace Corporate</name>
    --                         </account-type>
    --                     </account>
    --                 </account-allocation>
    --             </account-allocations>
    --             <currency>
    --                 <code>USD</code>
    --             </currency>
    --             <uom>
    --                 <code>EA</code>
    --             </uom>
    --         </order-line>
    --     </order-lines>
    -- </order-header>
    -- 

    DECLARE @xml int
    -- Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT

    EXEC sp_OASetProperty @xml, 'Tag', 'order-header'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'type', 'ExternalOrderHeader'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'po-number', 'PO-957645-18'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'version', 1, 'type', 'integer'
    EXEC sp_OAMethod @xml, 'UpdateChildContentInt', NULL, 'version', 1
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'payment-method', 'invoice'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'ship-to-address|location-code', 'DUMMYST'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'ship-to-user|login', 'CoupaRequester'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'supplier|name', 'Adam''s Apples'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'payment-term|code', '2/10 Net 30'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'currency|code', 'USD'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'order-lines', 1, 'type', 'array'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line|description', 'Supplies'
    EXEC sp_OAMethod @xml, 'UpdateChildContentInt', NULL, 'order-lines|order-line|line-num', 1
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'order-lines|order-line|price', 1, 'type', 'decimal'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line|price', '40.00'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'order-lines|order-line|quantity', 1, 'type', 'decimal'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line|quantity', '1.0'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line|type', 'OrderQuantityLine'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line|account|code', 'SF-Marketing-Assets'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line|account|account-type|name', 'Ace Corporate'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line|currency|code', 'USD'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line|uom|code', 'EA'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line[1]|description', 'Testing Kit'
    EXEC sp_OAMethod @xml, 'UpdateChildContentInt', NULL, 'order-lines|order-line[1]|line-num', 2
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'order-lines|order-line[1]|price', 1, 'type', 'decimal'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line[1]|price', '100.00'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'order-lines|order-line[1]|quantity', 1, 'type', 'decimal'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line[1]|quantity', '1.0'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line[1]|type', 'OrderQuantityLine'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'order-lines|order-line[1]|account-allocations', 1, 'type', 'array'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'order-lines|order-line[1]|account-allocations|account-allocation|amount', 1, 'type', 'decimal'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line[1]|account-allocations|account-allocation|amount', '10.00'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'order-lines|order-line[1]|account-allocations|account-allocation|pct', 1, 'type', 'decimal'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line[1]|account-allocations|account-allocation|pct', '10.0'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line[1]|account-allocations|account-allocation|account|code', 'SF-IT-Assets'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line[1]|account-allocations|account-allocation|account|account-type|name', 'Ace Corporate'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'order-lines|order-line[1]|account-allocations|account-allocation[1]|amount', 1, 'type', 'decimal'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line[1]|account-allocations|account-allocation[1]|amount', '90.00'
    EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'order-lines|order-line[1]|account-allocations|account-allocation[1]|pct', 1, 'type', 'decimal'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line[1]|account-allocations|account-allocation[1]|pct', '90.0'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line[1]|account-allocations|account-allocation[1]|account|code', 'SF-Sales-Assets'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line[1]|account-allocations|account-allocation[1]|account|account-type|name', 'Ace Corporate'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line[1]|currency|code', 'USD'
    EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'order-lines|order-line[1]|uom|code', 'EA'

    -- Adds the "Authorization: Bearer <access_token>" header.
    EXEC sp_OASetProperty @http, 'AuthToken', '<access_token>'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Accept', 'application/json'

    DECLARE @sbRequestBody int
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbRequestBody OUT

    EXEC sp_OAMethod @xml, 'GetXmlSb', @success OUT, @sbRequestBody

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PTextSb', @resp OUT, 'POST', 'https://domain.com/purchase_orders/?fields=["id"]', @sbRequestBody, 'utf-8', 'application/xml', 0, 0
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @xml
        EXEC @hr = sp_OADestroy @sbRequestBody
        RETURN
      END

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @resp


    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @xml
    EXEC @hr = sp_OADestroy @sbRequestBody


END
GO

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-H "Accept: application/json"
	-d '<?xml version="1.0" encoding="UTF-8"?>
<order-header>
    <type>ExternalOrderHeader</type>
    <po-number>PO-957645-18</po-number>
    <version type="integer">1</version>
    <payment-method>invoice</payment-method>
    <ship-to-address>
        <location-code>DUMMYST</location-code>
    </ship-to-address>
    <ship-to-user>
        <login>CoupaRequester</login>
    </ship-to-user>
    <supplier>
        <name>Adam\'s Apples</name>
    </supplier>
    <payment-term>
        <code>2/10 Net 30</code>
    </payment-term>
    <currency>
        <code>USD</code>
    </currency>
    <order-lines type="array">
        <order-line>
            <description>Supplies</description>
            <line-num>1</line-num>
            <price type="decimal">40.00</price>
            <quantity type="decimal">1.0</quantity>
            <type>OrderQuantityLine</type>
            <account>
                <code>SF-Marketing-Assets</code>
                <account-type>
                    <name>Ace Corporate</name>
                </account-type>
            </account>
            <currency>
                <code>USD</code>
            </currency>
            <uom>
                <code>EA</code>
            </uom>
        </order-line>
        <order-line>
            <description>Testing Kit</description>
            <line-num>2</line-num>
            <price type="decimal">100.00</price>
            <quantity type="decimal">1.0</quantity>
            <type>OrderQuantityLine</type>
            <account-allocations type="array">
                <account-allocation>
                    <amount type="decimal">10.00</amount>
                    <pct type="decimal">10.0</pct>
                    <account>
                        <code>SF-IT-Assets</code>
                        <account-type>
                            <name>Ace Corporate</name>
                        </account-type>
                    </account>
                </account-allocation>
                <account-allocation>
                    <amount type="decimal">90.00</amount>
                    <pct type="decimal">90.0</pct>
                    <account>
                        <code>SF-Sales-Assets</code>
                        <account-type>
                            <name>Ace Corporate</name>
                        </account-type>
                    </account>
                </account-allocation>
            </account-allocations>
            <currency>
                <code>USD</code>
            </currency>
            <uom>
                <code>EA</code>
            </uom>
        </order-line>
    </order-lines>
</order-header>'
https://domain.com/purchase_orders/?fields=["id"]

Postman Collection Item JSON

{
  "name": "Create External PO w/ Allocations",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Accept",
        "value": "application/json",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<order-header>\n    <type>ExternalOrderHeader</type>\n    <po-number>PO-957645-18</po-number>\n    <version type=\"integer\">1</version>\n    <payment-method>invoice</payment-method>\n    <ship-to-address>\n        <location-code>DUMMYST</location-code>\n    </ship-to-address>\n    <ship-to-user>\n        <login>CoupaRequester</login>\n    </ship-to-user>\n    <supplier>\n        <name>Adam's Apples</name>\n    </supplier>\n    <payment-term>\n        <code>2/10 Net 30</code>\n    </payment-term>\n    <currency>\n        <code>USD</code>\n    </currency>\n    <order-lines type=\"array\">\n        <order-line>\n            <description>Supplies</description>\n            <line-num>1</line-num>\n            <price type=\"decimal\">40.00</price>\n            <quantity type=\"decimal\">1.0</quantity>\n            <type>OrderQuantityLine</type>\n            <account>\n                <code>SF-Marketing-Assets</code>\n                <account-type>\n                    <name>Ace Corporate</name>\n                </account-type>\n            </account>\n            <currency>\n                <code>USD</code>\n            </currency>\n            <uom>\n                <code>EA</code>\n            </uom>\n        </order-line>\n        <order-line>\n            <description>Testing Kit</description>\n            <line-num>2</line-num>\n            <price type=\"decimal\">100.00</price>\n            <quantity type=\"decimal\">1.0</quantity>\n            <type>OrderQuantityLine</type>\n            <account-allocations type=\"array\">\n                <account-allocation>\n                    <amount type=\"decimal\">10.00</amount>\n                    <pct type=\"decimal\">10.0</pct>\n                    <account>\n                        <code>SF-IT-Assets</code>\n                        <account-type>\n                            <name>Ace Corporate</name>\n                        </account-type>\n                    </account>\n                </account-allocation>\n                <account-allocation>\n                    <amount type=\"decimal\">90.00</amount>\n                    <pct type=\"decimal\">90.0</pct>\n                    <account>\n                        <code>SF-Sales-Assets</code>\n                        <account-type>\n                            <name>Ace Corporate</name>\n                        </account-type>\n                    </account>\n                </account-allocation>\n            </account-allocations>\n            <currency>\n                <code>USD</code>\n            </currency>\n            <uom>\n                <code>EA</code>\n            </uom>\n        </order-line>\n    </order-lines>\n</order-header>"
    },
    "url": {
      "raw": "{{URL}}/purchase_orders/?fields=[\"id\"]",
      "host": [
        "{{URL}}"
      ],
      "path": [
        "purchase_orders",
        ""
      ],
      "query": [
        {
          "key": "fields",
          "value": "[\"id\"]"
        }
      ]
    }
  },
  "response": [
  ]
}