Counting associated records (rollup)

Today I wanted to see if it was possible to create a count based on relationships.


In my example, I created a custom object called Signal, with a many-to-one relationship to People (so one person can have multiple signals).

The workflow is triggered whenever the Signals relationship on a Person changes.



It then runs this code:


async function main(inputs) {
  const newSignals = inputs.newSignals || [];

  return newSignals.length;
}


The final step updates a Number of Signals attribute on the Person record.


It seems to work well going forward. Every time a new Signal is linked to a Person, it counts all of the associated Signals and updates the total.


The only limitation I've found so far is that if you manually delete a Signal, it won't trigger the workflow, so the count won't update straight away. As soon as another Signal is added or the relationship changes again, though, it recalculates and fixes itself.

I could see this being useful for Companies too, or really any custom object where you want to keep a running count of related records.

Still pretty new to Attio, so if there's a better or more native way of doing this I'd love to hear it!