Sync table displays only part of the data

Hi,
I am trying to retrieve a list of invoices from the Factomos API, using a pagination of 50 records per GET requets, and requesting 5 pages.
But while the logs show that all 5 GET request have returned 50 records each, so 250 records in total, Coda displays only 140 records in the resulting synced table.

Can anyody help?
My code :

// Schema for an Invoice
const InvoiceSchema = coda.makeObjectSchema({
  properties: {
    invoice_formated_number: { type: coda.ValueType.String },
    contact_pid: { type: coda.ValueType.String },
    client_company_name: { type: coda.ValueType.String },
    invoice_type: { type: coda.ValueType.String },
    invoice_validated: { type: coda.ValueType.String },
    invoice_status: { type: coda.ValueType.String },
  },
  displayProperty: "invoice_formated_number", // Display the formatted invoice number
  idProperty: "contact_pid", // Use contact_pid as a unique identifier
});


pack.addSyncTable({
  name: "Invoices",
  identityName: "Invoice",
  schema: InvoiceSchema,
  connectionRequirement: coda.ConnectionRequirement.None,
  formula: {
    name: "SyncInvoices",
    description: "Syncs the invoices.",
    parameters: [],
    execute: async function (params, context) {

      const totalPages = 6; // We want the first 5 pages of invoices

      // Create an array of fetch requests for each page
      let fetchPromises = [];
      for (let page = 1; page <= totalPages; page++) {
         const url = `https://${myDomain}/invoices?page=${page}`;
        
        fetchPromises.push(
          context.fetcher.fetch({
            method: "GET",
            url: url,
            headers: {
              "Content-Type": "application/json",
              "Accept": "application/vnd.status+json; version=1",
              "Authorization": `Bearer ${token}`,
            },
          })
        );
      }

      // Wait for all requests to complete
      let responses = await Promise.all(fetchPromises);

      // Check each response and concatenate the results
      let allInvoices = [];
      for (let i = 0; i < responses.length; i++) {
        if (!responses[i].body || !Array.isArray(responses[i].body.results)) {
          throw new Error(`Invalid response from the Factomos API on page ${i + 1}`);
        }
        allInvoices = allInvoices.concat(responses[i].body.results);
      }

      // Map the results to the desired format
      let result = allInvoices.map(invoice => ({
        invoice_formated_number: invoice.invoice_formated_number,
        contact_pid: invoice.contact_pid,
        client_company_name: invoice.client_company_name,
        invoice_type: invoice.invoice_type,
        invoice_validated: invoice.invoice_validated,
        invoice_status: invoice.invoice_status,
      }));
      return {
        result: result,
      };
    },
  },
});
1 Like

Hi @Seine_Avenue - The idProperty on the schema defines which property contains a unique ID for the row, and if multiple rows have the same value for that property they will be collapsed into one row. Looking at your schema, you appear to be using contact_pid as your unique ID:

idProperty: "contact_pid", // Use contact_pid as a unique identifier

I’m not familiar with this API, but I would imagine that the same contact can be associated with multiple invoices? If so then you’ll need to select a different property to be the idProperty. Perhaps invoice_formated_number?

2 Likes

Yes, that was it!
You are amazing @Eric_Koleda , thank you so much for your help!

1 Like

Awesome, I’m happy to hear it’s working now! Let me know if you run into any other issues.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.