0

I'm trying to achieve this with xero api and xero-node:

  1. Make an API call using Xero API and fetch all the contacts.
  2. Perform a check for new Contacts with a null value in Account Number in Contacts.
  3. Create unique CRN(customer reference number) and perform a check that this unique number has not been used previously.
  4. Update this unique CRN to Contact [ Account Number ].

What I've been able to achieve for now:

  1. All available contacts fetched using xero-node "get" method
  2. Check applied to the contacts to find those with no account number and stored them in an array
  3. Update action done on all the contacts stored in array but that is only updating the last contact for now.

This is the code for update method

noAccountNumContacts is the array of the IDs of those contact which has no account number

let xero = new XeroClient(config);

let noAccountNumContacts = [];

const result = await xero.contacts.get();

const contacts = result.Contacts;

contacts.map(contact => {

    const { ContactID, Name, AccountNumber } = contact;

    if (AccountNumber === undefined) {

      noAccountNumContacts.push(ContactID);

    }

});

noAccountNumContacts.map(contact => {

  xero.contacts.update({

    ContactID: contact,

    AccountNumber: 123456

  });

});

I expect this code to update the account number of the IDs stored in array(noAccountNumContacts). But what I'm getting for now is that the update call only updates a single contact rather than updating all the contacts.

After trying a lot I was unable to find the solution. Can you please help me understand what I'm doing wrong? Also, how can I generate a unique CRN which can be used as Account Number for Contact?

Bhawna Saroha
  • 603
  • 1
  • 11
  • 28
  • The first question, then, is what is in your `noAccountNumContacts` array after you have built it? Is your problem that it is not building the array properly, or that it is not iterating through the array to perform the update? – droopsnoot Sep 02 '19 at 08:39
  • And, is the update call only updating a single contact because you're trying to set them all to the same hard-coded account number? Account numbers must be unique across Xero contacts, so it will reject the second and subsequent attempt if your code is as above. – droopsnoot Sep 02 '19 at 08:40
  • Answer to your first question, the `noAccountNumContacts` array contains the ContactID of the contacts which have no value as their account number. The array is getting built and mapped properly(check it by printing to console). But the problem is that it is only updating single contact. – Bhawna Saroha Sep 02 '19 at 10:31
  • And, I also tried updating the `ContactName` which I believe can be common. But it still updated a single contact only. – Bhawna Saroha Sep 02 '19 at 10:32
  • No, the name has to be unique as well, though there is a note on the docs that suggests it might be changing in the future, and not to rely on name to identify a contact. To test whether that's the case, what about just having an incrementing counter starting at 1 and increasing by 1 for each contact, set the account number to that, and see if it makes any difference? I'm not very familiar with js so can't put it in code. Can you use the array index somehow? – droopsnoot Sep 02 '19 at 11:46
  • Actually I may be wrong about the AccountNumber having to be unique - the doc says that "if an existing contact matches your ContactName or ContactNumber then you will receive an error." and doesn't mention AccountNumber (I put the same in both). Can you get the response from Xero somehow, and look to see if it's giving you any useful information in there? – droopsnoot Sep 02 '19 at 11:50
  • Thanks a lot, it worked. I tried setting it using a counter and incrementing it by 1 as you mentioned. – Bhawna Saroha Sep 03 '19 at 04:27
  • For future reference, I'd forgotten that if you go into the developer.xero.com site, click on "My Apps", select your app name, and click "History", it will show you a list of all the calls you've made to the API, and (failed ones only, for some reason) you can click "View" and see the call you made, and the response that Xero sent. That can be useful if it's tricky to examine the response in your code, though it's always worth checking the responses in live production code, for robustness. – droopsnoot Sep 03 '19 at 08:30
  • Thanks for mentioning that. I'll take a look and see if it helps in future. – Bhawna Saroha Sep 04 '19 at 12:31

0 Answers0