John
John

Reputation: 45

How can I add an invoice to Xero?

I managed to add a contact but when I try and add an invoice I get an error message 'A validation exception occurred'. I would appreciate suggestions as to what is causing this error.

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        /// first create an instance of the Xero API
        var api = new Xero.Api.Example.Applications.Private.Core(false);

        Contact newContact = new Contact();
        newContact.Name = "Orac"; 
        Invoice newInvoice = new Invoice();
        newInvoice.Contact = new Contact();
        newInvoice.Contact = newContact;
        newInvoice.Date = System.DateTime.Now;
        newInvoice.DueDate = System.DateTime.Now.AddMonths(1);
        newInvoice.Status = Xero.Api.Core.Model.Status.InvoiceStatus.Authorised;
        newInvoice.Type = Xero.Api.Core.Model.Types.InvoiceType.AccountsReceivable;
        List<LineItem> lines = new List<LineItem>();
        LineItem li = new LineItem();
        li.LineAmount = Convert.ToDecimal("200.00");
        li.Quantity = Convert.ToDecimal("1.0000");
        li.ItemCode = "100";
        li.Description = "Webdev inv test";
        li.AccountCode = "200";
        li.UnitAmount = Convert.ToDecimal("50.00");
        lines.Add(li);
        newInvoice.LineItems = lines;

        // call the API to create the contact
        api.Invoices.Create(newInvoice);
        //api.Contacts.Create(newContact);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

Upvotes: 0

Views: 956

Answers (2)

MJMortimer
MJMortimer

Reputation: 865

The type of exception thrown is a ValidationException. Either catch that type specifically or cast your caught generic Exception, and inspect the ValidationErrors property

Upvotes: 2

rustyskates
rustyskates

Reputation: 866

Retain the result of your creation request - e.g.

var result = api.Invoices.Create(newInvoice);

...and examine the Errors and Warnings properties of the result to determine what's wrong with your request.

Upvotes: 2

Related Questions