Update customer metafield (list of dates) with date using Flow

Update customer metafield (list of dates) with date using Flow

DaveHewer
Shopify Partner
8 0 1

Hi. I'm trying to get a customer metafield (type: list of dates and times) to update when that customer purchases a particular product. (Specifically this is an annual membership product and I want to record a list of dates for when the customer purchases/renews it.)

The Flow trigger (order created) and condition (if SKU equals) are working correctly. However the action to 'update customer metafield' is always returning an error.

 

I have the value in Flow as {{order.createdAt | date: "%Y-%m-%d"}} but it's giving me the error "Got error updating metafield: "Value is invalid JSON: unexpected token at '-10-10'." For value: "2024-10-10", Type: list.date_time."

 

Do you know what I should have the value you in order for this to work? Thanks.

Replies 7 (7)

Webstablish
Shopify Partner
72 12 16

Hi @DaveHewer,

 

Could it be that you're trying to add a single date to a list type? That requires a specific syntax.

 

I found a similar question with a solution at https://community.shopify.com/c/shopify-apps/how-can-i-add-to-a-list-of-datetimes-in-flows/m-p/24746....

MenuQoL: Hide & Copy Menus | Tutorial
- Hide your seasonal menu items instead of throwing them away
- Copy navigation menus to save time
Kalen_Jordan
Shopify Partner
803 39 147

Yeah I think that's it.

DaveHewer
Shopify Partner
8 0 1

Thanks for the input @Webstablish.

 

I've tried putting the value in square brackets and adding quotation marks (see below). However, the post you linked to seemed to be assuming that I wanted to add a list of dates, whereas I'm wanting to add a single date to a list of dates.

["{{order.createdAt | date: "%Y-%m-%d"}}"]

Webstablish
Shopify Partner
72 12 16

You should see it as an array, so even if it can contain multiple values you can give it a single item initially. The comma separates multiple items. So on the next renewal you have to somehow model: [currentValue + ',' + newValue].

MenuQoL: Hide & Copy Menus | Tutorial
- Hide your seasonal menu items instead of throwing them away
- Copy navigation menus to save time
DaveHewer
Shopify Partner
8 0 1

Thanks — that makes sense. Can you give me any more pointers on how exactly to achieve that?

ryan_i
Shopify Staff
25 6 9

Hi Dave,

 

Two things here:

1. The date format should be date:

"%Y-%m-%dT%H:%M:%S"

2. You can use the following liquid in the "value" part of the "Update customer metafield" step:

[
{% for value_item in customer.thedates.value %}
  "{{value_item}}",
{% endfor %}
"{{customer.createdAt | date: "%Y-%m-%dT%H:%M:%S"}}"
]

 I'm using customer instead of order in this example and I've created a metafield alias (as described here) called "thedates". This liquid gets all of the existing values of the metafield and then adds another value. Note that this could run into issues with multiple workflows or apps updating this specific metafield if they use the same namespace and key.

 

Thanks!

Ryan

- Finding Flow useful? Leave us a review.
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
DaveHewer
Shopify Partner
8 0 1

I think I'm close to getting this to finally work, but it's not quite there yet. I'm no longer getting an error on the Flow logs (it's saying the metafield is being successfully updated) but nothing has actually been added to the customer's metafield when I check it.

 

Screenshot below of the Flow action dialogue box, with the value as follows:

 

[
{% for value_item in order.customer.championOrderDates.value %}
"{{value_item}}",
{% endfor %}
"{{order.createdAt | date: "%Y-%m-%dT%H:%M:%S"}}"
]

 

 

Screenshot 2024-11-05 at 10.51.45.png