Search code examples
jsonzapierxero-api

How to access specific line item value from Xero in a custom Zapier app?


I'm building a custom Zapier app and need to get a specific nested value via the Xero API.

My API endpoint is set up like this:

const options = {
  url: `https://api.xero.com/api.xro/2.0/Contacts/${bundle.inputData.ContactID}`,
  method: 'GET',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'Authorization': `Bearer ${bundle.authData.access_token}`,
    'xero-tenant-id': process.env.TENANT_ID
  },

}

return z.request(options)
  .then((response) => {
    response.throwForStatus();
    const results = response.json;



    return results;
  });

and this is returning:

  "Id": "8092d924-6ce1-415e-9a45-f61294c17bc7",
  "Status": "OK",
  "ProviderName": "Commission statements",
  "DateTimeUTC": "/Date(1620408485950)/",
  "Contacts": [
    {
      "ContactID": "fd9c3e88-4128-4d68-94f6-d40eef08d3b2",
      "AccountNumber": "1846",
      "ContactStatus": "ACTIVE",
      "Name": "xxx",
      "FirstName": "",
      "LastName": "",
      "EmailAddress": "[email protected]",
      "BankAccountDetails": "",
      "Addresses": [
        {
          "AddressType": "STREET",
          "City": "",
          "Region": "",
          "PostalCode": "",
          "Country": "",
          "AttentionTo": ""
        },
        {
          "AddressType": "POBOX",
          "City": "",
          "Region": "",
          "PostalCode": "",
          "Country": "",
          "AttentionTo": ""
        }
      ],
      "Phones": [
        {
          "PhoneType": "DDI",
          "PhoneNumber": "",
          "PhoneAreaCode": "",
          "PhoneCountryCode": ""
        },
        {
          "PhoneType": "DEFAULT",
          "PhoneNumber": "",
          "PhoneAreaCode": "",
          "PhoneCountryCode": ""
        },
        {
          "PhoneType": "FAX",
          "PhoneNumber": "0014I000023R123",
          "PhoneAreaCode": "",
          "PhoneCountryCode": ""
        },
        {
          "PhoneType": "MOBILE",
          "PhoneNumber": "",
          "PhoneAreaCode": "",
          "PhoneCountryCode": ""
        }
      ],
      "UpdatedDateUTC": "/Date(1618586879300+0000)/",
      "ContactGroups": [],
      "IsSupplier": false,
      "IsCustomer": true,
      "ContactPersons": [],
      "HasAttachments": false,
      "Attachments": [],
      "HasValidationErrors": false
    }
  ]
}

I need to return the value of 'PhoneNumber' within Phones ONLY when the PhoneType is FAX. So in this case I need to return 0014I000023R123. I am really struggling with the syntax of how to achieve this (I'm a marketer first and foremost not a developer!). Any help here would be hugely appreciated, thank you.


Solution

  • I'm not very good at Javascript but the following should work:

    var phoneList = results.Contacts[0].Phones;
    

    Will get you the list of phones from the first (and only, assuming there is only one) Contact.

    var fax = phoneList.filter(function (phone) {
        return phone.PhoneType == "FAX";
    })[0].PhoneNumber;
    

    Will filter through the list of phones and return you the first one with a PhoneType == FAX, and then get the PhoneNumber for that Phone.

    So, in this case fax will equal 0014I000023R123 and you can return that.

    Hopefully this answer is helpful, there might be a prettier way to do it but I've tested it and it's working.