0

I am trying to add values to a JSON object to post an invoice in the Xero API. When I do it with simple text values it works fine and when I used input values it also works fine, but if I try and combine the two, like invObj.InvoiceNumber = "INV-" + document.getElementById("orderId").value; It fails to create an invoice. Is there a way to join a string and input value so that it will be accepted by the api.

The Json is created in the renderer process of an electron app and passed to the main process where the api call is made within an express app.


        var invObj = {};
        invObj.Type = "ACCREC";
        invObj.Contact = {};
        invObj.Contact.Name = document.getElementById("customerName").value;
        invObj.Contact.EmailAddress = document.getElementsByClassName("bill-address-input")[0].value;
        invObj.Contact.FirstName = document.getElementsByClassName("bill-fname-input")[0].value;
        invObj.Contact.LastName = document.getElementsByClassName("bill-lname-input")[0].value;
        invObj.InvoiceNumber = "INV-" + document.getElementById("orderId").value; //fails
        invObj.InvoiceNumber = "INV-WS2222"; //succeeds
        invObj.InvoiceNumber = document.getElementById("orderId").value; //succeeds
        invObj.DateString = "2009-05-27T00:00:00";
        invObj.DueDateString = "2009-06-06T00:00:00";
        invObj.LineAmountTypes = "Exclusive";
        invObj.LineItems = [

        ];
        invObj.LineItems[0] = {};
        invObj.LineItems[0].Description = "Services as agreed";
        invObj.LineItems[0].Quantity = "4";
        invObj.LineItems[0].UnitAmount = "100";
        invObj.LineItems[0].AccountCode = "201";

This is the api call

        let oauth_verifier = req.query.oauth_verifier;
        let accessToken = await xeroClient.oauth1Client.swapRequestTokenforAccessToken(lastRequestToken, oauth_verifier)
            .then(async() => {
                var invoice = await xeroClient.invoices.create(data)           
                    .then((invoice) => {.....
twl2009
  • 79
  • 1
  • 11
  • Does the response from the `create()` call not give you any information about precisely what it doesn't like about your invoice? – droopsnoot Jul 08 '19 at 17:45
  • Below is my catch error code, it doesn't give an error, but the invoice just isnt created. Iif I use just the input value or string on there own then the invoice is created fine. `}) .catch((err) => { console.log('Failed to create invoice in xero' + err); event.returnValue = false; win.close(); })` – twl2009 Jul 08 '19 at 18:41
  • Also weirdly both these work:- `invObj.InvoiceNumber = "INV-" + "WS8888"` `invObj.InvoiceNumber = document.getElementById("orderId").value + document.getElementById("orderId").value` – twl2009 Jul 08 '19 at 19:18
  • If you sign into the Xero developer section at developer.xero.com, choose "My Apps", click on your app name, then click on the "History" tab near the top of the page, it'll show you all the calls from your API. If any of them fail, you get a "view" link in the right hand column, and that will show what you sent, and what the full response was. That might show you more detail on what's going wrong. – droopsnoot Jul 09 '19 at 08:47
  • Okay, done that. The results are strange. The `invoices.create` line gets a 200 response and I can get the invoice id with `.then((invoice) => { var inv = invoice["Invoices"][0]; var invId = inv["InvoiceID"];` But the invoice isn't created in xero and a GET request for that invoice id returns error 404 not found – twl2009 Jul 09 '19 at 11:48
  • That is, indeed, strange - I'm using vb.net which of course is different anyway. I don't think I can help other than to suggest contacting Xero. If you don't have a "proper" account (like I don't, as I'm still in the dev stage) you can go through the motions of posting in their community forum, it'll reject the post but offer to send it to a Xero support rep. I've had good (overnight) responses that way. – droopsnoot Jul 09 '19 at 12:44
  • What about if you get a dump of your data, and post it into the API previewer on the dev site? I doubt that would clarify anything given that it can work in so many other ways. Could the length of your orderId be causing an issue, and might .trim() help? Straw clutching, now, I'm afraid. – droopsnoot Jul 09 '19 at 12:46
  • I will try trim, but cant see why it would work . I have console.logged the invObj and entered it into the previewer and it works fine. I will open ticket with xero – twl2009 Jul 09 '19 at 13:09

0 Answers0