DA5: Code inside the workflow

Lesson 4 of 5 ยท reading 15โ€“25 minutes; practice and project time is on the module page

Common transformations

๐ŸŽฏYou'll be able to

Apply filter, map, group-and-total, look-up and de-duplicate patterns to real workflow data.

๐Ÿ The everyday picture

A shopkeeper doing the evening accounts repeats the same few moves: add up the day's sales, group them by product, cross out duplicate bills, and look up prices in the rate list. Most code steps repeat the same few moves too.

๐Ÿ“˜What it really is

๐Ÿ“A worked example

Weekly CRM tidy-up for Sharma Textiles, one of the three path projects. Input: 412 contacts with E.164 phones from DA4.

const contacts = $input.all().map(i => i.json);
const byPhone = {};
for (const c of contacts) {
  const key = c.phone_e164;
  if (!key) continue;                       // invalid numbers went to review in DA4
  const current = byPhone[key];
  if (!current || c.updated_at > current.updated_at) byPhone[key] = c;   // newest wins
}
const kept = Object.values(byPhone);
return [
  ...kept.map(c => ({ json: { ...c, action: 'keep' } })),
  { json: { action: 'report', before: contacts.length, after: kept.length } }
];

The run reports 412 contacts before and 371 after: 41 duplicates to merge. Because the rule is "newest wins", the report also lists which record was kept for each phone number, so staff can check.

๐ŸŒWhere it's used

๐Ÿ“Case scenario

After a deduplication step, a long-standing customer's GST number disappears from the CRM. The step kept the newest record, which came from a quick enquiry form without a GST field. You change the rule to merge fields: keep the newest value for each field, but don't overwrite a filled field with an empty one. You rerun on a copy first and compare.

๐Ÿ–ฑ๏ธTry it

Using your 20 cleaned contacts from DA4, write a Code node that removes duplicates by phone number using "newest wins", and returns a report item with the before and after counts. Then add the rule "don't overwrite a filled field with an empty one".

โš ๏ธCommon mistakes

๐Ÿ› ๏ธFor your project

Project 3's duplicate finding is group-and-merge on cleaned phones and lower-case emails. Write and test it on data/contacts.json.

โœ…Quick check

Name the five common transformation patterns.

Show the answer

Filter, map, group and total, deduplicate, and look up.

Why should a missing price stop or flag an item rather than become zero?

Show the answer

A zero price would quietly produce wrong totals or free orders.

โ† PreviousPython in the workflow
ProLearners Assist