How to create and finish an order

Guide to creating, adding lines, and finishing an order using GraphQL mutations. Includes field requirements, example queries, and responses for each step.

You need to perform the following GraphQL requests, in this order:

1. Creating a new order head

The minimum information you must provide is:

Field Type Description
orderDate int The date of the order. An integer in the form yyyymmdd. For instance, 23 March 2022, is 20220323.
customerNo int The number identifying the customer in Business NXT. (Do not confuse this with the Visma.net customer number!)

Use a mutation with the order_create field to add a new order:

Query

mutation create_order($cid : Int!,
                      $cno : Int!,
                      $date : Int)
{
  useCompany(no : $cid)
  {
    order_create(values:[{
      orderDate : $date
      customerNo : $cno
    }])
    {
      affectedRows
      items
      {
        orderNo
        orderDate
      }
    }
  }
}
Result

{
  "data": {
    "useCompany": {
      "order_create": {
        "affectedRows": 1,
        "items": [
          {
              "orderNo": 439,
              "orderDate": 20220401
          }
        ]
      }
    }
  }
}

From the result, you must use the orderNo to add order lines and execute processings such as finishing the order.

2. Adding order lines

The minimum information you must provide for each order line is:

Field Type Description
orderNo int The order number, from the previous mutation.
productNo int The number identifying the product in the system.
quantity int The quantity of items that you want to add.

Use a mutation with the orderLine_create field to add one or more order lines:

Query

mutation create_order_line($cid : Int!,
                           $ono : Int!,
                           $pno1 : String,
                           $pno2 : String)
{
  useCompany(no : $cid)
  {
    orderLine_create(values:[
      {
        orderNo : $ono
        productNo : $pno1
        quantity : 1
      },
      {
        orderNo : $ono
        productNo : $pno2
        quantity : 2
      },
    ])
    {
      affectedRows
      items
      {
        lineNo
      }
    }
  }
}
Result

{
  "data": {
    "useCompany": {
      "orderLine_create": {
        "affectedRows": 2,
        "items": [
          {
            "lineNo": 1
          },
          {
            "lineNo": 2
          }
        ]
      }
    }
  }
}

Alternative: Create an order and its order lines with a single request

You can create both an order and its order lines with a single request, using the @export directive. This directive allows you to capture the value of an evaluated field into a variable that can be used later in the query.

Query

mutation create_order_and_line($cid : Int!,
                               $cno : Int!,
                               $date : Int
                               $pno1 : String,
                               $pno2 : String,
                               $ono : Int = 0)
{
  useCompany(no : $cid)
  {
    # create the order first
    order_create(values:[{
      orderDate : $date
      customerNo : $cno
    }])
    {
      affectedRows
      items
      {
        # capture the value of the orderNo field
        # into the ono variable
        orderNo @export(as: "ono")
        orderDate
      }
    }

    # create the order lines
    orderLine_create(values:[
      {
        orderNo : $ono
        productNo : $pno1
        quantity : 1
      },
      {
        orderNo : $ono
        productNo : $pno2
        quantity : 2
      },
    ])
    {
      affectedRows
      items
      {
        orderNo
        lineNo
      }
    }
  }
}
Result

{
  "data": {
    "useCompany": {
      "order_create": {
        "affectedRows": 1,
        "items": [
          {
              "orderNo": 440,
              "orderDate": 20221122
          }
        ]
      },
      "orderLine_create": {
        "affectedRows": 2,
        "items": [
          {
            "orderNo": 440,
            "lineNo": 1
          },
          {
            "orderNo": 440,
            "lineNo": 2
          }
        ]
      }
    }
  }
}

3. Finishing an order

The minimum information you need to finish an order is the order number.

Use a mutation with the order_processings field to execute a processing on an order. To finish the order, use the finish field:

Query

mutation finish_order($cid : Int!,
                      $ono : Int!)
{
  useCompany(no : $cid)
  {
    order_processings
    {
      finish(
        filter: {orderNo:{_eq : $ono}}
      )
      {
        succeeded
        items
        {
          handledOrderLine
          {
            lineNo
            finishedNow
          }
        }
      }
    }
  }
}
Result

{
  "data": {
    "useCompany": {
      "order_processings": {
        "finish": {
          "succeeded": true,
          "items": [
            {
              "handledOrderLine": [
                {
                  "lineNo": 1,
                  "finishedNow": 1
                },
                {
                  "lineNo": 2,
                  "finishedNow": 2
                }
              ]
            }
          ]
        }
      }
    }
  }
}

3. Finishing parameters

The finish processing has a parameter called finishType that is an integer defining what the processing should do. The implicit value is 0 which means the whole order should be finished. This is what we have seen in the previous example, where the argument was missing, therefore its default value was used.

The possible values for finishType are:

Value Description
0 Finish the whole order.
1 Finish the articles specified by their barcode.
2 Finish the articles specified by their product number.
3 Finish the articles specified by their TransactionInformation1 field.

When finishType has any other value than 0, then you need to provide the additional arguments using the group parameter. This is a dictionary of key-value pairs, where the key is the bar code, product number, or the TransactionInformation1 value of a product, and value is the quantity to finish (remove from the order).

Query

mutation finish_order($cid : Int!,
                      $ono : Int!)
{
  useCompany(no : $cid)
  {
    order_processings
    {
      finish(
        args : {
          finishType : 2
          group : [
            {key: "HW1",  quantity: 2},
            {key: "AGG4", quantity: 5}
          ]
        },
        filter: {orderNo:{_eq : $ono}}
      )
      {
        succeeded
        items
        {
          handledOrderLine
          {
            lineNo
            finishedNow
          }
        }
      }
    }
  }
}
Result

{
  "data": {
    "useCompany": {
      "order_processings": {
        "finish": {
          "succeeded": true,
          "items": [
            {
              "handledOrderLine": [
                {
                  "lineNo": 1,
                  "finishedNow": 2
                },
                {
                  "lineNo": 2,
                  "finishedNow": 5
                }
              ]
            }
          ]
        }
      }
    }
  }
}
Last modified September 24, 2024