# SAP Business One DI API Sales Order Code Generator (C#)

> Source: https://netray.co/tools/sap-business-one-di-api-sales-order-csharp
> Publisher: Netray (netray.co), AI and integration engineering for ERP and manufacturing systems.
> Section: Free Tools

## Short answer

Fill in connection and order details to get a C# DI API skeleton that connects, adds a sales order and reports errors.

## Category

developer

## Erp

SAP Business One

## Audience

- B1 developer
- Add-on developer
- Integration engineer

## Tool

**Kind:** template

**Output:** code

**Language:** csharp

**Fields:**

#### Database server

**Key:** server

**Type:** text

**Default:** SQLSRV01

#### Database type

**Key:** dbtype

**Type:** select

**Options:**

- dst_MSSQL2019
- dst_MSSQL2017
- dst_HANADB

**Default:** dst_MSSQL2019

#### Company database

**Key:** company

**Type:** text

**Default:** SBODEMOUS

#### B1 user

**Key:** user

**Type:** text

**Default:** manager

#### Customer code

**Key:** customer

**Type:** text

**Default:** C20000

#### Item code

**Key:** item

**Type:** text

**Default:** FG-1001

#### Quantity

**Key:** qty

**Type:** number

**Default:** 10

#### Unit price

**Key:** price

**Type:** number

**Default:** 149.50

**Blocks:**

#### Add a sales order

**Body:** using System;
using SAPbobsCOM;

class AddSalesOrder
{
    static void Main()
    {
        Company oCompany = new Company();
        oCompany.Server = "{{server}}";
        oCompany.DbServerType = BoDataServerTypes.{{dbtype}};
        oCompany.CompanyDB = "{{company}}";
        oCompany.UserName = "{{user}}";
        oCompany.Password = Environment.GetEnvironmentVariable("B1_PASSWORD");

        int rc = oCompany.Connect();
        if (rc != 0)
        {
            Console.WriteLine("Connect failed: " + oCompany.GetLastErrorCode() + " " + oCompany.GetLastErrorDescription());
            return;
        }

        Documents oOrder = (Documents)oCompany.GetBusinessObject(BoObjectTypes.oOrders);
        oOrder.CardCode = "{{customer}}";
        oOrder.DocDate = DateTime.Today;
        oOrder.DocDueDate = DateTime.Today.AddDays(14);

        oOrder.Lines.ItemCode = "{{item}}";
        oOrder.Lines.Quantity = {{qty}};
        oOrder.Lines.UnitPrice = {{price}};
        oOrder.Lines.Add();

        rc = oOrder.Add();
        if (rc != 0)
        {
            Console.WriteLine("Add failed: " + oCompany.GetLastErrorCode() + " " + oCompany.GetLastErrorDescription());
        }
        else
        {
            Console.WriteLine("Created order, key = " + oCompany.GetNewObjectKey());
        }

        System.Runtime.InteropServices.Marshal.ReleaseComObject(oOrder);
        oCompany.Disconnect();
        System.Runtime.InteropServices.Marshal.ReleaseComObject(oCompany);
    }
}

**Notes:** Requires a reference to the SAPbobsCOM library that matches your B1 version and the same bitness as your process (the DI API version is tied to the B1 version). Enum names for database types vary by release, so confirm in the SDK help. The password is read from an environment variable on purpose.

## Content

**Intro:** The DI API (SAPbobsCOM) is the older COM-based way to create and update Business One data from code, and it is still used in add-ons and Windows services. This generator produces a clean C# skeleton you can adapt to other documents.

**Sections:**

#### What the code does

**Body:** It builds a Company object with server, database type, company database and user, connects, gets a Documents business object for sales orders, sets header and line fields, calls Add and reads the result. Connect and Add both return zero on success; on failure the error code and description come from the Company object, so print both.

**Bullets:**

- Connect once, then create many documents
- Lines.Add() closes a line and opens the next
- GetNewObjectKey returns the DocEntry
- Release COM objects and disconnect when done

#### Adapting it

**Body:** To create other documents change the BoObjectTypes value: oPurchaseOrders for purchasing, oDeliveryNotes for deliveries. To link a line to a base document set BaseType, BaseEntry and BaseLine. For production, use oProductionOrders, and set ItemNo, PlannedQuantity and DueDate. The object type list in this site shows the matching enums.

#### Pitfalls

**Body:** The DI API must match the B1 version and the process bitness, and a mismatch produces connection errors that look unrelated. Do not hard-code passwords. Do not keep a connection open for hours in a service without a reconnect strategy. Consider the Service Layer for new web integrations, since it avoids installing the DI API on the calling machine.

**Cta:** If you are maintaining DI API add-ons or planning a move to the Service Layer, Netray can help review and automate that work with human approval on writes.

**Faq:**

#### Should I use the DI API or the Service Layer?

**Answer:** The Service Layer suits web and cross-platform integrations and needs no client install. The DI API suits Windows add-ons and services that already run next to the SDK. Many new projects prefer the Service Layer.

#### Why does Connect fail with a version error?

**Answer:** The DI API build must match the Business One version of the target company, and the process bitness must match the installed DI API. Reinstall the matching DI API or switch the build target.

#### How do I get the new document number?

**Answer:** After a successful Add, call GetNewObjectKey on the Company object. It returns the DocEntry as a string. Read the document back with GetByKey to obtain DocNum.

#### Does the DI API enforce approval procedures?

**Answer:** Documents added through the DI API go through the same approval templates as the client, so a document that needs approval can return an error or be saved as a draft pending approval, depending on the configuration.

## Search terms

sap b1 di api example c#, sap business one di api sales order, sapbobscom documents add, sap b1 di api connect, sap business one sdk c# sample, di api getlasterror, sap b1 add sales order code, sapbobscom company connect

---
Talk to an engineer: https://netray.co/contact?intent=erp_ai | Machine catalog: https://netray.co/agent/catalog.json
