Is it possible to update an integer customer metafield in Flow

I have a Flow that runs when the membership product is purchased. I am attempting to update the “yearJoined” metafield, which is of type integer. This is what I have in the body of the “updateCustomerMetafield” action:

{%- assign yr_joined = order.metafields | where: “namespace”, “custom” | where: “key”, “yearJoined” -%}
{%- if yr_joined.value == blank %}
{{ “now” | date: “%Y” | strip | plus: 0 }}
{% endif -%}

But it keeps throwing the following error: Exception: Got error updating metafield: “Value must be an integer.” For value: " 2026\n", Type: number_integer

I know it must be an integer. That’s the point of the strip and plus:0 filters.

Anyone know why these seem to be ignored by Flow?

@susanweber The error is happening because the value being saved contains whitespace and a newline. Even though it looks like a number, Shopify Flow is strict — a number_integer metafield must receive a pure integer, not a string with spaces or line breaks. Please try this.

{% assign yr_joined = order.metafields.custom.yearJoined %}

{% if yr_joined == blank %}
{% assign current_year = “now” | date: “%Y” | plus: 0 %}
{{ current_year }}
{% endif %} 

Flow does what you tell it to do, however, every liquid code row leaves an empty line in resulting output.

So, whitespace control is important.

Consider replacing all {% with {%- and {{ with {{- (and so on) in your code.

Or do it like this:

{%- liquid
  assign yr_joined = order.metafields | where: "namespace", "custom" | where: "key", "yearJoined"
  if yr_joined.value == blank
    assign new_value =  "now" | date: "%Y" | strip | plus: 0
  else
    assign new_value = yr_joined.value
  endif
-%}
{{- new_value -}}

Or like this:

{%- liquid
  assign yr_joined = order.metafields | where: "namespace", "custom" | where: "key", "yearJoined"
  assign new_value = yr_joined.value
  if new_value == blank
    assign new_value =  "now" | date: "%Y" | strip | plus: 0
  else
  echo new_value
-%}

Thanks so much tim_1, your answer makes a lot of sense. I’ve installed your version 1, and will mark your answer as the solution as soon as the flow triggers and runs successfully.

1 Like

Confirmed, tim_1 posted the correct solution. Thank you SO much, Tim! This has been a back-burner issue for me for the last 18 months. Such a relief to finally get it working haha. Much appreciated :slight_smile:

1 Like