How can I save Customer Notes into Metafields correctly?

Hello everyone, I’m trying to parse and save text data stored in Customer Notes, into several Metafields. For now I chose single line Metafields, Flow run history shows no error but unfortunately nothing happens.

Here is a snapshot of the flow:

Code:

{%- assign customernote_lines = customer.note | split: "<br />" -%}
{%- for line in customernote_lines -%}
  {%- if line contains "City:" -%}
    {%- assign datacity = line -%}
    "{{- datacity | strip_newlines | strip -}}"
  {%- endif -%}
{%- endfor -%}

Which is shown below in context:

Before step 4, a
is added at the end of each line with newline_to_br so data looks like this:

Nickname: Rob<br />City: London<br />Country: United Kingdom<br />

Can you please advise about what’s wrong with this code?

Hey @Digico

The code you provided looks mostly correct for parsing and extracting the “City” information from the customer notes. However, it seems that you are not actually saving the extracted data into the Metafields.

To save the extracted data into the Metafields, you need to use the Shopify API or Shopify SDK to update the customer’s Metafields. Here’s an example of how you can modify your code to save the extracted “City” data into a Metafield:

{%- assign customernote_lines = customer.note | split: "
" -%}
{%- assign datacity = '' -%}

{%- for line in customernote_lines -%}
  {%- if line contains "City:" -%}
    {%- assign datacity = line | replace: "City:", "" | strip -%}
  {%- endif -%}
{%- endfor -%}

{% comment %}
Assuming you have already set up the necessary Metafields for the customer, you can save the extracted city data like this:
{% endcomment %}
{%- assign namespace = 'your_namespace' -%}
{%- assign key = 'city' -%}
{%- assign value = datacity -%}

{% comment %}
Replace `customer.id` with the actual customer ID you are working with.
{% endcomment %}
{% shopify_api 'PUT', "/admin/api/2021-07/customers/{{ customer.id }}/metafields.json" %}
{
  "metafield": {
    "namespace": "{{ namespace }}",
    "key": "{{ key }}",
    "value": "{{ value }}",
    "value_type": "string"
  }
}
{% endshopify_api %}

In the above code, replace ‘your_namespace’ with the namespace you’ve set up for your Metafields, and ‘city’ with the key for the specific Metafield you want to store the city data in.

Make sure you have the necessary permissions to update Metafields using the Shopify API, and ensure that you are running this code in an appropriate context (such as a Shopify app or a custom Shopify script).

If I managed to help you then, don’t forget to Like it and Mark it as Solution!

Best Regards,
Moeed

Thanks for the answer and time @Moeed , issue is question is about Shopify Flow App in this forum. Simply because in Shopify Flow App:

Customer Id aka {{ customer.id }} is already filled when you reach step 4, so it shouldn’t need to repeat neither calling API (again). Shopify Flow uses GraphQL mutations to query API.

So I think the key problem where is that you are trying to update the note and then used the updated note in the next step. Flow pre-fetches all data used in a workflow at the start, in order to optimize API use. So it will use the old note in that metafield action. It you want to refetch that note, then you can add a short wait step before the metafield action. Anything after the wait step will be fetched after the wait step completes.

Thanks for feedback, my bad some code was wrong so input data was empty.

Finally got it working removing step 3 to go directly to 4 and updating code a bit. Using a short wait step was a good suggestion, but not needed. Also make sure to click “Show all” Metafields in admin because when created by Flow, by default they’re unpinned and lack definition - so they’re hidden in UI.

1 Like

Did you find the way to put into customer metafield whats in customers note?
Trying to put into customer metafield whats im getting from customer note lik “11111” text line string.

Be pleaced if you can help me with this

If you mean put the note an an order into a metafield, wouldn’t you simply add {{ order.note }} to the value field and use the namespace/key of the metafield you want to store it in.

Hi,

could you please show me what you did?

Thank you

would you mind sharing the approach that worked for you?

You might have better luck posting a new thread with your own use case and where you are stuck

Hi,

I wanna add a custom field to registration form. as I understood I have to save it as a note. but I could not find how I can pass this note to the custom field. I wanna also show this field in customer page. can you please help me ?

for custom fields I cannot define value. there is no value field.

thanks in advance for your help.

Best regards,
Sara

Hi,
Can you please help me with the value code? it doesn’t work for me.

What does “doesn’t work” mean? Show the problem you are having.

I do see you are setting a single line string but you have newlines in your code. Try adding hyphens to that last variable to remove whitespace, like {{- city -}}

1 Like

Thank you now it shows the city on meta field. Do you happen to know how I can add a custom required filed in registration form?

{{ 'Company' }}

I have added the code to the registration form of shopify. but I cannot see the error when it is empty

No, you might want to post that in a more applicable forum.

Hello Sara,

Glad to hear you made it work. Now about your latest question, like Paul said it’s not about Shopify Flow app so another forum would be best.

Also in a nutshell, Liquid (and Shopify by extension) almost never returns errors when data is empty. That’s because of truthy and falsy values in Liquid (https://shopify.github.io/liquid/basics/truthy-and-falsy/). I’d suggest you study a bit Liquid docs there (https://shopify.github.io/liquid/).

Thanks for your answer.
Regarding flow application, I have added a custom fields as a file. so users can upload a file. but in note I can see jut the name of the file. so when I update the custom fields, it just send the name of that. I cannot have the path of the file to download. can you please help me with that?

I think notes are plain text. It’s unclear what you are using to add the name there. But there is a URL field on metafields that have a file refererence.

this is the input I have added to the form

{{ 'Upload' }}*
is there any possibility to pass the url of the file to URL metafield when user register?

I don’t know what form you are referring to with that form. As I mentioned before, you will have better luck asking in the appropriate topic