1

I want to assign a MOD10V05 valid Bpay CRN(generated using https://github.com/JedWatson/node-bpay) to the contact in xero account using xero-api. I'm able to get the contacts and update them with the generated Bpay CRN. But I want to perform a check before updating the account number so that a duplicate account number doesn't exist already.

I've tried to get the contacts and apply a map to them to store the ContactID of those with no account numbers to an array(noAccountNumContacts). Then map that array to update the contact with Bpay CRN. But I'm unable to apply a check for duplication.

Here is the code:

const XeroClient = require("xero-node").AccountingAPIClient;
const bpay = require("bpay");

(async function() {
  let xero = new XeroClient(config);
  let noAccountNumContacts = [];

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

  // push contact with no account no. to array
  contacts.map(contact => {
    const { ContactID, Name, AccountNumber } = contact;
    console.log("Contact ID: ", ContactID);
    console.log("Name: ", Name);
    if (AccountNumber === undefined) {
      noAccountNumContacts.push(ContactID);
    } else {
      console.log("Account no: ", AccountNumber);
    }
  });

  console.log("Contacts to be updated ", noAccountNumContacts);

  // update account number
  if (noAccountNumContacts.length !== 0) {
    noAccountNumContacts.map(async contact => {
      const crn = bpay.generateBpayCRN(7); 
      await xero.contacts.update({
        ContactID: contact,
        AccountNumber: crn
      });
    });
  }
  noAccountNumContacts = [];
})();

I want to generate a new Bpay CRN and check if that already exists or not in the contacts. If it exists then generate again otherwise update it to the contact with a null value in the account number field.

Bhawna Saroha
  • 603
  • 1
  • 11
  • 28

1 Answers1

0

I would think the way to do it is by trying to match the account number and seeing if any are there. Can you do something like

const result = await xero.contacts.get({
AccountNumber: crn
});

and then just look at the number of contacts returned? If it's zero, you can go ahead, otherwise there's a duplicate and you'll get the ID for it.

(I am not familiar with node.js, so my syntax might be a bit out).

Of course, the other way is to look at the response that you get from your update() call, and see if it's thrown an error for a duplicate account number, in which case you can regenerate the account number and try again. I don't like that approach personally, but it would probably work if you can easily access the response. Again, I don't use node.js so I'm not sure how that is done.

droopsnoot
  • 931
  • 1
  • 7
  • 11
  • Thanks for helping. I'll try what you're suggesting and hopefully will be able to find a solution as well. – Bhawna Saroha Sep 11 '19 at 06:48
  • You might also want to look at sending the updates to Xero in a batch of, say, 50 at a time. You have a limit of 60 API calls per 60s rolling window, and 5000 per 24h window, so it's worth trying to minimise the number of calls. If this is a one-off update, it might be more trouble than it's worth, just have a pause between updates or trap the response. – droopsnoot Sep 11 '19 at 16:53