Insert invoice C# example

22 views
Skip to first unread message

Dan Palley

unread,
Nov 1, 2019, 6:27:24 PM11/1/19
to TramsAPI Beta
We've started working on the C# demo code again, after a long break.  

The hope is that customers using C# can get up and running quickly with the API, using the sample code.

Here's a snippet example of an invoice insert:

// get a blank invoice response that we can populate
        WebResponse invoiceLoad = await Session.Load("invoice", -1);

        // convert the invoice response into a dataset
        DataSet invoiceDataset = invoiceLoad.ResultDataset();

        // create a new invoice datarow
        DataRow invoiceRow = invoiceDataset.Tables[Invoice_Consts.Table_Invoice].NewRow();

        // set the desired column values for the new invoice datarow
        invoiceRow[Invoice_Consts.Field_Invoice_Client_LinkNo] = 1;
        invoiceRow[Invoice_Consts.Field_Invoice_Branch_LinkNo] = 0;
        invoiceRow[Invoice_Consts.Field_Invoice_RecordLocator] = "ABCDEF";

        // add the new invoice datarow to the invoice table
        invoiceDataset.Tables[Invoice_Consts.Table_Invoice].Rows.Add(invoiceRow);

        // create a new booking datarow
        DataRow bookingRow = invoiceDataset.Tables[Invoice_Consts.Table_Booking].NewRow();

        // set the desired column values for the new booking datarow
        bookingRow[Invoice_Consts.Field_Booking_Vendor_LinkNo= 2;
        bookingRow[Invoice_Consts.Field_Booking_ConfirmNo] = "ABC123";

// associate the booking datarow with the invoice datarow; booking is a child table of invoice
        bookingRow[Invoice_Consts.Field_Booking_Invoice_LinkNo] = invoiceRow[Invoice_Consts.Field_Invoice_InvoiceNo];

        // add the new booking datarow to the booking table
        invoiceDataset.Tables[Invoice_Consts.Table_Booking].Rows.Add(bookingRow);

        if (invoiceDataset.HasChanges())
        {
            JProperty invoiceDeltaJson = JsonDataset.ChangesFromDataset(invoiceDataset);
            Console.WriteLine(invoiceDeltaJson.ToString());

            WebResponse InvoiceSave = await Session.Save("Invoice"invoiceDeltaJson);
        }

First, we've added some classes that define common constants for field names and parameter names (Invoice_Consts, InvoiceQuery_Consts).  

Second,fields that represent primary keys (i.e. Invoice.InvoiceNo) automatically get a dummy default value when new data is added, .The actual value is assigned when ApplyUpdates is called.  The dummy value is used when linking child records back to a parent record, i.e. booking.invoice_linkno => invoice.invoiceno.

Lastly, we're doing a better job of enforcing required fields before the changes are sent to the API.  Calling Rows.Add on a table will raise an exception if a required field, like Invoice.Client_LinkNo, is NULL.

Here's what the API request looks like when we save the invoice:

invoice/applyupdates
{
    "deltaDataset": {
        "invoice": [
            {
                "insert": {
                    "invoiceNo"-1,
                    "client_LinkNo"1,
                    "branch_LinkNo"0,
                    "recordLocator""ABCDEF"
                "booking": [
                     {
                         "insert": {
                             "bookingNo"-2,
                             "vendor_LinkNo"2,
                             "confirmNo""ABC123"
                        }
                     }
                 ]
                }
            }
        ]
    }
}

Dan
Reply all
Reply to author
Forward
0 new messages