如何在自定义 Zapier 应用程序中从 Xero 访问特定行项目值?

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

我正在构建自定义 Zapier 应用程序,需要通过 Xero 获取特定的嵌套值 API。

我的 API 端点设置如下:

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;
  });

这是 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": "xxx@test.com",
      "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
    }
  ]
}

仅当电话类型为传真时,我才需要 return 电话中 'PhoneNumber' 的值。所以在这种情况下我需要 return 0014I000023R123。我真的在为如何实现这一目标的语法而苦苦挣扎(我首先是营销人员,而不是开发人员!)。如有任何帮助,我们将不胜感激,谢谢。

我不太擅长Javascript,但以下应该可行:

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

将为您提供第一个(并且仅假设只有一个)联系人的电话列表。

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

将筛选电话列表,return 您是第一个 PhoneType == FAX 的电话,然后获取 Phone 电话号码 Phone.

因此,在这种情况下,传真将等于 0014I000023R123,您可以 return 那个。

希望这个回答对您有所帮助,可能有更漂亮的方法,但我已经测试过它并且有效。