SAP Business One DI API Sales Order Code Generator (C#)
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.
Fill in
Add a sales order
using System;
using SAPbobsCOM;
class AddSalesOrder
{
static void Main()
{
Company oCompany = new Company();
oCompany.Server = "SQLSRV01";
oCompany.DbServerType = BoDataServerTypes.dst_MSSQL2019;
oCompany.CompanyDB = "SBODEMOUS";
oCompany.UserName = "manager";
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 = "C20000";
oOrder.DocDate = DateTime.Today;
oOrder.DocDueDate = DateTime.Today.AddDays(14);
oOrder.Lines.ItemCode = "FG-1001";
oOrder.Lines.Quantity = 10;
oOrder.Lines.UnitPrice = 149.50;
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);
}
}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.
Plan your DI API to Service Layer move
Optional: a short call to review add-ons and integrations built on the DI API.
Optional. The tool and every export work without this. No spam.
What the code does
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.
- 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
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
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.
Frequently Asked Questions
Should I use the DI API or the Service Layer?
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?
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?
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?
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.
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.
Related Tools
SAP Business One Service Layer Request Builder
Fill in host, company and item details and get ready-to-paste Service Layer requests for login, item query, sales order and production order.
SAPSAP Business One Document Object Type Codes
Lookup of ObjType numbers used in B1 queries, UDO links and approval templates, with header and row tables.
SAPSAP Business One Database Tables Reference
Searchable lookup of the core SAP Business One tables, with the matching row table and what each one stores.
SAPSAP Business One Go-Live Cutover Checklist
A cutover checklist for taking a Business One company live: freeze, balances, stock counts, users, integrations and hypercare.
More Developer Tools tools
See all- SAP B1 Service Layer Sales Order JSON Generator
- SAP Business One T-SQL to HANA SQL Query Conversion Reference
- SAP Business One AR Aging SQL Query Generator (SQL Server and HANA)
- SAP Business One Formatted Search (FMS) Query Generator
- SAP Business One Pending Approvals Query Generator (OWDD, WDD1)
- SAP Business One Service Layer $batch Request Generator
- SAP Business One DI API Connection Troubleshooting Checklist
- SAP Sales Order ABAP SELECT Generator (VBAK, VBAP, VBFA)
- SAP Sales Order Create BAPI ABAP Generator (BAPI_SALESORDER_CREATEFROMDAT2)
New to SAP Business One? Start with the SAP Business One guide and toolkit.
Keep exploring
Free tools
- SAP Business One DI API Connection Troubleshooting ChecklistWork through installation, Company connection properties, error handling, environment and runtime checks when an SAP Business One DI API Connect call fails.
- SAP B1 Service Layer Sales Order JSON GeneratorFill in server, company database, customer and line details to get ready-to-test Login and sales order POST requests for the Service Layer REST API.
- SAP Business One Service Layer Request BuilderFill in host, company and item details and get ready-to-paste Service Layer requests for login, item query, sales order and production order.
Runs with your ERP
- Avalara AvaTax and SAP Business One: sales and use tax inside the ERPHow Avalara AvaTax calculates sales and use tax inside SAP Business One, how the endorsed add-on connects, what breaks, and where private on-prem AI helps.
- Spreadsheet Server with SAP Business One: live Excel reporting on inventory, sales and costHow insightsoftware Spreadsheet Server gives Excel live drill-down on SAP Business One inventory, sales and production cost data, with setup and pitfalls.
Integrations
- How to automate inbound email in SAP Business OneTurn inbound POs, RFQs and supplier emails into SAP Business One drafts: mailbox capture, extraction, Business Partner matching, Service Layer posting and review.
- How to add AI agents to SAP Business OneHow to put AI agents on SAP Business One: Service Layer tool design, read-only first, draft documents for human approval, and an on-prem model option.