What's your biggest current challenge? Have your say in Community Polls along the right column.

How can I append to an existing metafield value in a flow?

How can I append to an existing metafield value in a flow?

Hades
Visitor
1 0 1

Hello,

I am trying to extract all orders a customer has placed over the past 5 days, and write those order names to the order.note of the newest flow-triggering order.  Our fulfillment team will use that information to group multiple orders in one single shipment.

I have created a Flow that utilizes:

  • getOrderData for the last 5 days (excluding the current triggering order)
  • Count >= 1
  • For each loop to get the order names of the returned orders

I understand if I execute Update Order Note from within the For each loop, the Flow will only write content to the notes of returned (past) orders, and not to the intended triggering order's.  Therefore I tried to get the Flow to write the extracted order names to a global metafield (shop.metafields) and let the subsequent "Then" step (after exiting the For each loop) pull the constructed content from that shop.metafield and update the order note of the triggering order.

 

It looks like the For each loop could return the past order name as planned.  However I am seeing issues when it comes to appending past order names as a single line text in the metafield.

 

For example:

  • The initial metafield value is A
  • The past three order names extracted via the For each loop are B, C and D respectively
  • I put this liquid in the For each loop > Update shop metafield > Value field:
    • {{ metafields_item.value | append: getOrderDataForeachitem.name }}
  • I expected the metafield will get appended a new order name value in each iteration, like this:
    • metafields_item.value becomes A B
    • metafields_item.value becomes A B C
    • metafields_item.value becomes A B C D
  • However, the final outcome is always A D

It looks like to me the "expected to be updated" metafields_item.value did not pass on from one iteration to another.  It seems like the loops did not know that the metafield value has been updated from its preceding loop, and always sees the immediate metafield value as A only.

 

I wasn't sure if I have done something wrong in the For each loop, so I created another simplified Flow solely for testing "append new values to the existing metafield value".  However the result was the same.  Each Update shop metafield action did not get the updated metafields_item.value from the preceding update action:

 

20231227 08 - Shopify Flow - shop.metafields.png

 

The liquid I put in the three Update shop metafield Action Value fields are:

 

-=-= Update shop metafield Action (1) =-=-

{%- for metafields_item in shop.metafields -%}
    {%- if metafields_item.namespace == "custom" and metafields_item.key == "note_content" -%}
        {{ metafields_item.value | append: " B" }}
    {%- endif -%}
{%- endfor -%}


-=-= Update shop metafield Action (2) =-=-

{%- for metafields_item in shop.metafields -%}
    {%- if metafields_item.namespace == "custom" and metafields_item.key == "note_content" -%}
        {{ metafields_item.value | append: " C" }}
    {%- endif -%}
{%- endfor -%}


-=-= Update shop metafield Action (3) =-=-

{%- for metafields_item in shop.metafields -%}
    {%- if metafields_item.namespace == "custom" and metafields_item.key == "note_content" -%}
        {{ metafields_item.value | append: " D" }}
    {%- endif -%}
{%- endfor -%}

The final updated metafield value was still A D ,  instead of the expected A B C D

 

I want to understand if metafield value can be updated shared/passed to another Action within the same Flow?  Did I miss anything?  Please advise how I can construct a list of past order names and write that the note of the triggering order.

 

Any comment is welcome.  Thanks in advance! 

 

Reply 1 (1)

paul_n
Shopify Staff
1433 157 332

I don't think you need metafields at all. You should be able to update the note on the original order by just connecting to after any conditions and then using liquid to determine what the note should be. 

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.