Add multiple values to a custom metafield | Flow Automation

Topic summary

Problem: A user attempted to use Shopify Flow to automatically populate a list-type metafield (filters.farbe) with multiple color values from another metafield (custom_label_2). Products with multiple colors (e.g., black and beige) should receive all applicable values, but only one value was being added.

Initial Approach: The workflow used separate conditional branches for each color, which only executed one action instead of capturing all matching colors.

Attempted Solution: Following advice to consolidate logic, the user tried implementing a “Run code” action with JavaScript to check multiple color values and build a list. However, this resulted in errors due to missing input query definitions and metafield type mismatches.

Resolution: The user’s development team found an alternative solution by creating a new metafield that captures values from the existing metafield as list values, with additional logic to add “multicolor” when products have multiple colors. The exact automation method (one-time vs. ongoing) wasn’t fully detailed.

Open Question: Another user later asked for more specifics about the final solution and whether it runs automatically on product creation/updates, but this remains unanswered.

Summarized with AI on November 1. AI used: claude-sonnet-4-5-20250929.

UPDATE:
I tried the following flow:

The code in “run code” is the following:

export default function main({product}) {
 // Exisitng Values in Metafield filters.farbe
 const filtersFarbeMetafieldObject = product.metafields.find((metafield) => metafield.namespace === "filters" && metafield.key === "farbe");
 const filtersFarbe = filtersFarbeMetafieldObject ? JSON.parse(filtersFarbeMetafieldObject.value) : [];
 const colorValues = ["schwarz", "beige", "braun", "weiß", "grau", "grün", "orange", "lila", "gelb", "blau", "rosa", "rot","bunt"]; // Die Farbwerte, die du überprüfen möchtest

 // Check whether the product label contains the colors 
 colorValues.forEach((colorValue) => {
   if (product.metafields.some((metafield) => metafield.namespace === "mm-google-shopping" && metafield.key === "custom_label_2" && metafield.value.includes(colorValue))) {
     if (!filtersFarbe.includes(colorValue)) {
       filtersFarbe.push(colorValue);
     }
   }
 });

 // Output of a string that can be directly transmitted to a metafield update action   return { filtersFarbe: JSON.stringify(filtersFarbe) };
}

I ran the flow and got the following error message:

I don’t know much about programming, which is why I can’t understand what I did wrong.
From an other member of this forum I was told that the problem is the definition of my metafield “filters.farbe”. It is currently not a JSON type metafield.

So I deleted the old filters.farbe and created a new one and now I am stuck here:

What goes into the field here now? Basically, it should contain all the possible colors (schwarz", “beige”, “braun”, “weiß”, “grau”, “grün”, “orange”, “lila”, “gelb”, “blau”, “rosa”, “rot”,“bunt”) and allow a list of values in that metafield. For example, when there’s a product that includes black and green color, both values should be sent to “filters.farbe” metafield for that product.

Thanks in advance.