Xero - beautiful accounting software

Xero Developer Help Center

Xero Developer Community

Community > Wrapper libraries >

Problems retrieving contacts by Account Number

Started by Mike Edwards -   in Wrapper libraries

I'm linking Xero to my customer's Point of Sale system, and I'm trying to link the customer file on there to the contacts on Xero. I've grabbed all the contact details out of Xero, matched them to the PoS system, and now I'm trying to store the customer code from the PoS system in the ContactNumber field.

In doing so, I get each one and check to see if the account exists in Xero. I use this code

Dim findContact = From cnt In api.Contacts.Find Where cnt.AccountNumber = tempACC

where tempACC is the account number that I initially retrieved from Xero at the start of the process. By that I stress - I know it exists. If I open Xero, I can search for it and Xero will display the account.

However, for a significant number of contacts, the API returns no results, suggesting that the account number is invalid. If I edit the code and instead of using a variable, I hard-code an account code that I know exists, it won't find it. If I try another, it does find it. None have special characters in them, they're just letters and/or numbers.

I've used similar code in other areas (for example I check a contact exists before trying to post an invoice to it) but in those cases, I tend to search using the Contact Number. I can't do that here because, at this point, it's blank. I've tried trimming spac

So, any ideas why the search doesn't find accounts that I know exist?

(Sorry, forgot to add - the code is vb.net, and is using the Xero .net API. Generally all functionality is working, just this one thing is driving me mad.)
Hmm, more and more curious. I changed the code so that I could use the "id" field instead of the account number, and that gives me a similar problem. The id appears to be the same, but it returns no results. If I drop the checks and just run the update, it stores the contact number correctly, in the correct contact.

Mike Edwards  

Oh, now I have more of a problem. Code that used to work, is no longer working. I've added contact numbers into every contact, and now I'm trying to send an invoice across. In order to be sure it's going to work, I run the same code to see if the account exists, by doing

Dim findContact = From d In api.Contacts.Find
Where d.ContactNumber = invoiceheader(0).invcontact

which has worked for some time, and allowed me to post invoices. Now it does not find any contacts, so it refuses to post the invoice.

I am reluctant to allow it to blindly send invoices to what might be non-existent contacts, but I am struggling to understand what has changed. Is it a change at the Xero end?

Mike Edwards  

In case it helps anyone else using vb.net, the problem here is the syntax I used to search for a contact. Using that syntax (which I got from some sample code on here) seems to automatically invoke paging, and rather than the pages referring to the results-set (in my case this would be either 0 or 1 result as ContactNumber is a unique field, so I presumed they would all be on page 1), it seems that I would have to know which page of the complete result set my contact resides on. So this piece of code

Dim findContact = From d in api.Contacts.Find
Where d.ContactNumber = "12345"

would work as long as that contact is in the first 100 found, but if it was in the second block of 100, I would have to add:

Dim findContact = From d in api.Contacts.Page(2).Find
Where d.ContactNumber = "12345"

and increment that page number. It appears to be trying to retrieve all contacts, then use "Find" on what it has retrieved.

An alternative syntax that appears to work for me would be:

Dim findContact = api.Contacts.Find("12345")

Of course, there's no "count" property, so I enclose the lot in a try-catch and set a flag in the catch model - I'm only doing this as a check for existence, after all.

The reason my code appeared to have been working a month or two go and then stop working was that, between it working and not working, I had imported the complete set of "live" contacts from my customers old accounting system. Before that, I just had some test accounts, fewer than 100, so they were always on the first page of results.

Mike Edwards