Solved

Save Customer Notes into Metafields

Digico
Shopify Partner
46 1 4

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:

Snap4.jpg

 

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:

Snap6.jpg

Before step 4, a <br /> 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? 

Accepted Solution (1)
Digico
Shopify Partner
46 1 4

This is an accepted solution.

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.

View solution in original post

Replies 9 (9)

Moeed
Shopify Partner
3697 932 1166

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: "<br />" -%}
{%- 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

 

Need a Shopify developer? Chat on WhatsApp


- For Shopify Design Changes | Shopify Custom Coding | Custom Modifications
- Your Coffee Tip and my code, A perfect blend. ❤️
Digico
Shopify Partner
46 1 4

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.

paul_n
Shopify Staff
946 128 225

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.

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.
Digico
Shopify Partner
46 1 4

This is an accepted solution.

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.

Cpuga
Shopify Partner
41 0 5

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

paul_n
Shopify Staff
946 128 225

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. 

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.
atertonien
Excursionist
40 0 27

Hi,

could you please show me what you did?

 

Thank you

Ram_A
Explorer
60 3 19

would you mind sharing the approach that worked for you?

paul_n
Shopify Staff
946 128 225

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

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.