C# BOI example of creating an AR_Invoice with lines/details

SOLVED

Is there anyone that can help with providing an example of creating an AR Invoice with lines/details in C# using either BOI or the nuget package www.nuget.org/.../

  • +1
    verified answer

    Hi Cole,
    This is a quick A/R invoice example using the Sage100.ObjectManagement nuget package. www.nuget.org/.../Sage100.ObjectManagement

    using Sage100.ObjectManagement; /* https://www.nuget.org/packages/Sage100.ObjectManagement */
    using Sage100.ObjectManagement.Interfaces;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Net;
    
    
    namespace SageCityDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                var homePath = @"C:\Sage\Sage 100 Advanced\MAS90\Home";
                var company = "ABC";
                var user = "boiUser";
                var password = "****";
                var module = "A/R";
                var taskName = "AR_Invoice_ui";
                var busObjName = "AR_Invoice_bus";
                var regularInvoice = "IN";
    
                var sessionLogon = new SessionLogon
                {
                    CompanyCode = company,
                    UserLogon = new NetworkCredential { UserName = user, Password = password },
                    ModuleCode = ModuleNames.AccountsReceivable,
                    ModuleDate = DateTime.Today
                };
    
                var itemCodeList = new List<string> { "/WIDGET", "/100-AA" };
    
                using (var sySession = new MasSession(homePath))
                {
                    // Create session object.
                    sySession.InitSession(sessionLogon);
    
                    // Create business object.
                    using (var arInvoice = sySession.CreateObject<ILineEntry>(busObjName, taskName, module))
                    {
                        // Create new batch if batches are enabled.
                        if (arInvoice.BatchEnabled)
                        {
                            arInvoice.SelectBatch();
                        }
    
                        // The dynamic PvxObject pointer is available to access pvx methods directly.
                        var invoiceNo = string.Empty;
                        arInvoice.PvxObject.nGetNextInvoiceNo(ref invoiceNo);
    
                        // Start a new invoice.
                        arInvoice.SetKeyValue("InvoiceNo$", invoiceNo);
                        arInvoice.SetKeyValue("InvoiceType$", regularInvoice);
    
                        if (arInvoice.SetKey() != Mas.edtNew) throw new ApplicationException($"Failed to create a new invoice. {arInvoice.LastErrorMessage}");
    
                        // Set the customer for the invoice.
                        arInvoice.SetValue("ARDivisionNo$", "01");
                        arInvoice.SetValue("CustomerNo$", "ABF");
    
                        // Create line detail.
                        foreach (var itemCode in itemCodeList)
                        {
                            arInvoice.AddLine();
                            arInvoice.Lines.SetValue("ItemCode$", itemCode);
                            arInvoice.Lines.SetValue("QuantityOrdered", 1);
    
                            if (!arInvoice.Lines.Write()) throw new ApplicationException($"Failed to write the line.  {arInvoice.Lines.LastErrorMessage}");
                        }
    
                        // Save the new invoice.
                        if (!arInvoice.Write()) throw new ApplicationException($"Failed to write the invoice.  {arInvoice.LastErrorMessage}");
    
                        Trace.WriteLine($"New invoice {invoiceNo} was created.");
                    }
                }
            }
        }
    }



    Thank you

    Bret Richion