Is there a function for adding and consolidating values

Hi Community,

I have built a warehouse and fulfillment automation process for my ecommerce business with Make.

To make things even easier, we now want to create a picking list that includes all products and their total quantities from the next 15 orders.

  • each product should only appear once on that list
  • the quantity per product should be the sum of given input from single orders (which we iterate through).

Example Input:

  • 2x Product A
  • 1x Product B
  • 3x Product C
  • 3x Product A
  • 1x Product C

Expected output:

  • 5x Product A
  • 1x Product B
  • 4x Product C

So far, If created this process by iterating through orders to get the products and quantities per order.

Then, I use a text aggregator to create the input list. This text input will then be given to GPT4o with instructions to do what I want.

In 95% of all cases, this works well. However, in some cases, the AI just makes mistakes (some quantities don’t add up correctly).

I tried to fix the prompt, but it still seems unreliable. This is why I am re-thinking doing this step AI and try to find a better solution.

Can somebody maybe point me into a better way of doing this using math-functions in Make?

This looks doable, but you should provide some sample output bundles so we can tell how these product quantities are structured.

For further assistance, please provide the following:

1. Relevant Screenshots

You can upload images here using the Upload icon in the text editor:

2. Scenario blueprint

Please export the scenario blueprint file to allow others to view the mapped variables in the module fields. At the bottom of the scenario editor, you can click on the three dots to find the Export Blueprint menu item.

3. And most importantly, Input/Output bundles

Please provide the output bundles of the modules by running the scenario (or get from the scenario History tab), then click the white speech bubble on the top-right of each module and select “Download input/output bundles”.

A.

Save each bundle contents in your text editor as a bundle.txt file, and upload it here into this discussion thread.

B.

If you are unable to upload files on this forum, alternatively you can paste the formatted bundles in this manner:

Here are two ways to format text so that it won’t be changed by the forum:

A. Type code block manually
Add three backticks ``` before and after the content/bundle, like this:

```
content goes here
```

B. Highlight and click the format button in the editor

Providing the input/output bundles will allow others to replicate what is going on in the scenario even if they do not use the external service.

Following these steps will allow others to assist you here. Thanks!

1 Like

Hi @samliew . Thanks for offering to help. I was not looking into getting a scenario fixed, as I’m trying to find the best approach for this task first.

Instead of using AI, one way could be to use Javascript with a function that does exactly what I want. I found out there is a CustomJS Node that I could use for that.

The function looks like this:

// Function to consolidate and sum quantities
function consolidateProducts(productsList) {
  const productMap = new Map();

  // Split input into lines and process each line
  productsList
    .trim()
    .split("\n")
    .forEach((item) => {
      // Split quantity and product
      const [quantity, ...productParts] = item.split(" ");
      const product = productParts.join(" ");
      const quantityNum = parseInt(quantity.replace("x", ""), 10);

      // Sum quantities for each product
      if (productMap.has(product)) {
        productMap.set(product, productMap.get(product) + quantityNum);
      } else {
        productMap.set(product, quantityNum);
      }
    });

  // Convert the map back to an array of strings
  const output = [];
  productMap.forEach((quantity, product) => {
    output.push(`${quantity}x ${product}`);
  });

  return output.join("\n");
}

const productsList = `
1x Product A
1x Product B
3x Product B
...
`;

const consolidatedList = consolidateProducts(productsList);
console.log(consolidatedList);

Welcome to the Make community!

Yes, that is possible. You’ll need a minimum of three modules:

The complexity formula is a fixed 3 operations, no matter how many lines of items are in your input.

Module Export

You can copy and paste this module export into your scenario. This will paste the modules shown in my screenshots above.

  1. Copy the JSON code below by clicking the copy button when you mouseover the top-right of the code block

  2. Enter your scenario editor. Press ESC to close any dialogs. Press CTRLV (paste keyboard shortcut for Windows) to paste directly in the canvas.

  3. Click on each imported module and save it for validation. You may be prompted to remap some variables and connections.

Click to Expand Module Export Code

JSON - Copy and Paste this directly in the scenario editor

{
    "subflows": [
        {
            "flow": [
                {
                    "id": 182,
                    "module": "util:ComposeTransformer",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "value": "2x Product A\n1x Product B\n3x Product C\n3x Product A\n1x Product C"
                    },
                    "metadata": {
                        "designer": {
                            "x": 1240,
                            "y": -2337
                        },
                        "restore": {},
                        "expect": [
                            {
                                "name": "value",
                                "type": "text",
                                "label": "Text"
                            }
                        ]
                    }
                },
                {
                    "id": 183,
                    "module": "regexp:Parser",
                    "version": 1,
                    "parameters": {
                        "pattern": "(?<num>\\d+)x (?<product>.+)",
                        "global": true,
                        "sensitive": true,
                        "multiline": false,
                        "singleline": false,
                        "continueWhenNoRes": false,
                        "ignoreInfiniteLoopsWhenGlobal": false
                    },
                    "mapper": {
                        "text": "{{182.value}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 1484,
                            "y": -2340
                        },
                        "restore": {},
                        "parameters": [
                            {
                                "name": "pattern",
                                "type": "text",
                                "label": "Pattern",
                                "required": true
                            },
                            {
                                "name": "global",
                                "type": "boolean",
                                "label": "Global match",
                                "required": true
                            },
                            {
                                "name": "sensitive",
                                "type": "boolean",
                                "label": "Case sensitive",
                                "required": true
                            },
                            {
                                "name": "multiline",
                                "type": "boolean",
                                "label": "Multiline",
                                "required": true
                            },
                            {
                                "name": "singleline",
                                "type": "boolean",
                                "label": "Singleline",
                                "required": true
                            },
                            {
                                "name": "continueWhenNoRes",
                                "type": "boolean",
                                "label": "Continue the execution of the route even if the module finds no matches",
                                "required": true
                            },
                            {
                                "name": "ignoreInfiniteLoopsWhenGlobal",
                                "type": "boolean",
                                "label": "Ignore errors when there is an infinite search loop",
                                "required": true
                            }
                        ],
                        "expect": [
                            {
                                "name": "text",
                                "type": "text",
                                "label": "Text"
                            }
                        ],
                        "interface": [
                            {
                                "type": "text",
                                "name": "num",
                                "label": "num"
                            },
                            {
                                "type": "text",
                                "name": "product",
                                "label": "product"
                            },
                            {
                                "type": "uinteger",
                                "name": "i",
                                "label": "i"
                            },
                            {
                                "type": "any",
                                "name": "__IMTMATCH__",
                                "label": "Fallback Match"
                            }
                        ]
                    }
                },
                {
                    "id": 184,
                    "module": "util:FunctionAggregator2",
                    "version": 1,
                    "parameters": {
                        "fn": "sum",
                        "feeder": 183
                    },
                    "mapper": {
                        "value": "{{183.num}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 1727,
                            "y": -2341
                        },
                        "restore": {
                            "parameters": {
                                "fn": {
                                    "label": "SUM"
                                }
                            },
                            "extra": {
                                "feeder": {
                                    "label": "Text parser - Match pattern [183]"
                                }
                            }
                        },
                        "parameters": [
                            {
                                "name": "fn",
                                "type": "select",
                                "label": "Aggregate function",
                                "required": true,
                                "validate": {
                                    "enum": [
                                        "avg",
                                        "sum",
                                        "count",
                                        "max",
                                        "min"
                                    ]
                                }
                            }
                        ],
                        "expect": [
                            {
                                "name": "value",
                                "type": "number",
                                "label": "Value"
                            }
                        ],
                        "advanced": true
                    },
                    "flags": {
                        "groupBy": "{{183.product}}"
                    }
                },
                {
                    "id": 185,
                    "module": "util:TextAggregator",
                    "version": 1,
                    "parameters": {
                        "rowSeparator": "\n",
                        "feeder": 184
                    },
                    "mapper": {
                        "value": "{{184.result}}x {{184.`__IMTKEY__`}}"
                    },
                    "metadata": {
                        "designer": {
                            "x": 1970,
                            "y": -2340,
                            "messages": [
                                {
                                    "category": "last",
                                    "severity": "warning",
                                    "message": "A transformer should not be the last module in the route."
                                }
                            ]
                        },
                        "restore": {
                            "parameters": {
                                "rowSeparator": {
                                    "label": "New row"
                                }
                            },
                            "extra": {
                                "feeder": {
                                    "label": "Tools - Numeric aggregator [184]"
                                }
                            },
                            "flags": {
                                "groupBy": {
                                    "collapsed": true
                                },
                                "stopIfEmpty": {
                                    "collapsed": true
                                }
                            }
                        },
                        "parameters": [
                            {
                                "name": "rowSeparator",
                                "type": "select",
                                "label": "Row separator",
                                "validate": {
                                    "enum": [
                                        "\n",
                                        "\t",
                                        "other"
                                    ]
                                }
                            }
                        ],
                        "expect": [
                            {
                                "name": "value",
                                "type": "text",
                                "label": "Text"
                            }
                        ],
                        "advanced": true
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}

Hope this helps! Let me know if there are any further questions or issues.

— @samliew

1 Like

Oh wow. Thank you @samliew . That works actually pretty well and only takes very few operations.

I just found one potential error source: empty character spaces in product names. In one or two cases, the grouping by product name didn’t work because there was a space behind the name. Is there any way to erase the before the grouping or ignore them?

Extra question: Could I provide a given sorting order of the products? Or would that be something I should do with AI again.

Yes, the regular expression pattern has to be simply updated.

Please start a new thread for a new question. This will give others a chance to assist you too. I’m also not at my computer at the moment so I can’t follow up further.

Thank you…

— @samliew

1 Like