1

I'm using xero-node npm package & looks like it will save me a ton of time.

I want to create OR update multiple contacts but not sure how. I'm hoping someone from Xero monitors this tag.

var contacts = [];
var contact = { "Name": "ABC", ContactNumber:"code123"};
contacts.push(xeroClient.core.contacts.newContact(contact));
const retVal = await xeroClient.core.contacts.saveContacts(contacts);

If I run it once , it creates the contact ( or multiple if I add to the array ) . I want to update the contact using my code ( not the xero generated id - because then I would need to store that it my other system).

If I run it a second time , it fails. I assume that is because it is doing a PUT instead of a POST..?

Here are the docs. https://github.com/XeroAPI/xero-node/blob/2a1ec34888e998cabd72aa79fa58a5b14f2c9cd5/docs/Contacts.md

Martin Thompson
  • 3,415
  • 10
  • 38
  • 62

1 Answers1

1

You are correct.

Here are the docs on Contacts:

https://developer.xero.com/documentation/api/contacts

See this section:

PUT Contacts Use this method to create one or more contact records. This method works very similar to POST Contacts but if an existing contact matches your ContactName or ContactNumber then you will receive an error.

ContactNumber is unique. So you're trying to create two contacts with the same ContactNumber.

I think saving the contact like in the example here would help: https://github.com/XeroAPI/xero-node/blob/2a1ec34888e998cabd72aa79fa58a5b14f2c9cd5/docs/Contacts.md

philals
  • 390
  • 2
  • 5
  • Thanks. I found the answer ( not very well documented in the node package ) - you need to pass the option "post" as the default is "put". var options = {method: "post"}; this.Postresult = await xeroClient.core.invoices.saveInvoices(this.mappedXeroOrders, options); – Martin Thompson Feb 26 '18 at 03:03