Shopify Flow is an ecommerce automation platform that enables you to automate tasks and processes within your store and across your apps.
As part of Winter Editions `24, Flow launched a new Run code action. If you have questions, comments, etc this is the place to ask.
Hi Shopify,
The new Run code feature sounds promising! Many of our wholesale customers request a CSV line_sheet (overview of products, barcodes, image links etc) for their orders. But it is currently not easily done through Shopify. My background is in javascript, so being able to execute javascript in a flow, could make sense for generating the desired super simple CSV-file... But would you say, it a possible usecase for the feature?
General the comma-separate variables part should be easy. That said, Flow doesn't currently generate files or have a way to do file handing. So you might need to rely on something else to do the attachment part.
It's possibly you could use Google Sheets for that - Flow does have a connector for Sheets.
Is this the right place to set up a Flow to delete a line item from an order?
I've got this code sample that works in GraphQL (from https://community.shopify.com/c/customers-discounts-and-orders/remove-lineitem-form-order-via-api-re...) but totally lost on how to convert this for RUN CODE.
mutation orderEditBegin($id: ID!) {
orderEditBegin(id: $id) {
userErrors {
field
message
}
calculatedOrder {
id
lineItems(first: 5){
edges{
node{
id
quantity
}
}
}
originalOrder{
lineItems(first:5){
edges{
node{
id
}
}
}
}
}
}
}
mutation changeLineItemQuantity {
orderEditSetQuantity(id: "gid://shopify/CalculatedOrder/55116136575", lineItemId: "gid://shopify/CalculatedLineItem/13953156284543", quantity: 0) {
calculatedOrder {
id
addedLineItems(first: 5) {
edges {
node {
id
quantity
}
}
}
}
userErrors {
field
message
}
}
}
mutation commitEdit {
orderEditCommit(id: "gid://shopify/CalculatedOrder/55116136575", notifyCustomer: false, staffNote: "I edited the order! It was me!") {
order {
id
}
userErrors {
field
message
}
}
}
Do I need to do 3 separate RUN CODE blocks to complete the line item delete? Any guidance or even a complete solution would be appreciated.
I've had a look at https://github.com/Shopify/flow-code-examples/tree/main/run-code-examples for other hints as well.
See input data section here, and the list of limitations. The action is not yet ready for API queries. The "input" is for querying Flow's environment, not the API.
https://help.shopify.com/en/manual/shopify-flow/reference/actions/run-code
@paul_n I'm looking for functionality in Flows that can help me add an order tag using a value from the orders custom attributes. I haven't been able to do this earlier in Flow and am wondering if this new feature could help me achieve this. Included a screenshot below of what I think the function would look like (TODO is the actual tagging of the order):
My understanding is that this would not be possible because we don't have access to the API from here. Can you confirm this? And are you aware of any other functionality in Flows that would help me achieve this?
P.S. The overall goal is to be able to add different order tags from the website frontend depending on the customers behavior. I wasn't able to find a way to add tags directly, so I resorted to adding custom attributes through the /cart endpoints. I figured Flows would help me find an automated way of translating these custom attributes into an order tag.
You don't need API access in Run code to solve this use case. You can feed in customAttributes from the Flow environment (which does the API access for you) as you are doing in the input query (remove the commas by the way).
You can't call the tags API from the action, but you can pass the tag(s) you want to add through the return data. And then put that as a variable inside the Add tag action.
Hi Paul - interestingly this is just the use case which we're trying, but the add tag action doesn't seem to persist the input variable, and the tag added to the order is an empty array [] - however, if I use the same variable as the input to the 'add note' action then the text is correctly stored, so looks like there's a bug in the add tag action.
After you put in the liquid you need to hit enter to save it. It will look like a tag. I recommend also saving the liquid in the description in case you later need to modify it.
Thanks for that - that worked (not the most intuitive UI), and the functionality opens a good set of opportunities. I like.
Agree, in the future you could use a separate step to set the liquid variable and then just enter a basic variable in that tag field
TBH, I think it's easier than that - all it needs is an 'add tag' button next to the entry field so that there's a positive action!
It would be really handy to be able to do this type of query in the run code action:
query{
metaobjects( type: "all_group_tags" first: 250){
nodes{
displayName
}
}
}
I have a "uniqe" code I tag customers with to be able to group them and save the used tags in a metaobject to be able to get the name of the group in a easy way. When a customer is created I generate a random group code, but since it isn't totally uniqe I need to check so it doesn't exist first (if the customer is the first to register to that group). As I understand there is currently no way of getting a metaobject information (in flow) unless it has the ownerType shop (stored in the shop's metafield, but I can't do that through the admin dashboard unfortunately). And the metaobjects created in the admin dashboard doesn't have that (unfortunately).
Maybe there there is another way of doing this?
/Kind regards Alicia
Hi, to clarify the input query in the Run code action is to query the data available in Flow's environment. It is not to get more data.
We have a separate project underway to make accessing Metaobjects in Flow much easier. We will likely have an action to get a Metaobject value and return it into the Flow. That could then be passed to Run code if needed.
Regarding accessing Metaobjects in Flow, you can access a metaobject as long as it's tied to a resource (shop, customer, product, etc). It's a bit hard to use but it's possible. The project I mentioned is also looking to clean up that approach.
Did they finally introduce a way to insert steps, move steps and copy steps?
You can already insert and move steps, unless you mean something more specific. Copy is missing.
Can you tell me how to move a step or insert a step. Just to clarify....
1. Insert a new step between 2 other steps.
2. Move a step from between 2 current steps to another area in the same flow.
To insert a new step:
The process is roughly the same for moving a step.
Here's a video: https://screenshot.click/14-04-6u9i6-1zotw.mp4
The video says it all. Thanks
Hello,
It seems like this tool doesn't yet support TextEncoder or crypto.subtle (though perhaps I'm just doing something wrong!)
I'm trying to hash some data as sha256 before I send it via API, but neither the Liquid Filter or 'Run Code' functions seem to enable this workflow (the Liquid Filter should be working, but it's giving an 'undefined filter' error).
I believe both TextEncoder and crypto have wide support, so I'm hoping they're on the roadmap for 'Run Code' 🤞
Here's an snippet of what I'm trying to run:
function sha256(message) {
// encode as UTF-8
const msgBuffer = new TextEncoder().encode(message);
// hash the message
const hashBuffer = crypto.subtle.digest("SHA-256", msgBuffer);
// convert ArrayBuffer to Array
const hashArray = Array.from(new Uint8Array(hashBuffer));
// convert bytes to hex string
const hashHex = hashArray
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
return hashHex;
}
and the errors in Flow 'Run Code':
Thanks!
Jack
Correct those libraries are not supported. Can you elaborate on your use case for them? What API requires SHA256?
Thanks for getting back to me!
For privacy / security; the Pinterest API, Meta API and Google Ads API all require SHA256 hashing of anything sensitive. (Pinterest Example, Google Example, Meta Example). I'm sure other platforms do too, but those are 3 that I've come across already.
I've been calling the APIs using the 'Send HTTPS Request' step in the flow. My goal was to format and hash the data in Run Code before passing it onto the next step in the flow. I also tried to use the SHA256 Liquid Filter directly within the 'Send HTTPS Request' step, but it isn't currently supported.
Hi, I'm trying to use the run code feature where I want to make a calculation and output it as kgQuantity. I want to then use this value later in the flow. It seems I can only recall the {{runCode.""}} on the immediate step after but not further down the line? I have a small glitch in our POS system which sets 500g product inventory to zero I'm trying to capture it before its set to 0 then to update it back once it does. I normally send the payload in an http to Make.com and was hoping I can use the Run Code feature to solve this.
I'm getting the following error:
Hi, looks like we didn't document that limitation. If there is a wait step, the output will not be available on the other side. I don't have an ETA on this, but will report back when I know more.
Docs:
https://help.shopify.com/en/manual/shopify-flow/reference/actions/run-code#limitations
This is a limitation of wait steps. When you have data coming from another source and then potentially waiting for up to 90 days, the data can get stale. So currently, it's not stored. Are you able to refactor to put the wait step in a different spot?
Hi @paul_n , right I see. Unfortunately no, there is a bit of a delay, so I have to wait. Usually within the first 30seconds the inventory goes to zero, If I try to run code then it would calculate it from the new quantity. I just realised, I should be able to replace it with {{inventoryQuantityPrior}}. However, I can see many scenarios where the wait step would be useful and the output could be used. Maybe limit it to 24 hours so the data doesnt get stale? I'm interested in the data value at the time it is run, so it wouldn't matter to you if it's stale or not, I might have a particular need for it. :))
I'm looking for a way to tag, say, top 10 most purchased products in the past week. I could pass in getOrderData, then count and sort within the Run Code block. However, I wasn't able to output the product objects themselves (see screenshot).
My workaround was to turn the list of product IDs into a query string ((id: XXX) OR (id: YYY) OR ...), then use that string in a subsequent Get Product Data block, but then the result would come in a different ordering.
Is there any way I can achieve this with Run Code's current implementation? If not, is there anything in future plans about returning GraphQL objects themselves, in a way that can be looped over in later Flow blocks?
The Output types are unaware of Shopify resources. Generally, I would output only what you need for subsequent steps. Outputting a list of product objects is counter-productive because you'll then need to write liquid in the next step to get the IDs you want.
If you want to get Shopify resources into a code step right now, the best way is to use a step before the Run code action to get the data that you want. Then you can access the information in Run code.
Point taken for the product ID. In that case, is it possible to then pass said ID into, say, an Add Tags to Product block? I notice that the block often says "Using the product identified by: ..." (screenshot attached), so evidently it only needs the ID. Still, if I export said ID from the Run Code block, would the subsequent Add Tags block somehow recognize the exported IDs as valid product IDs?
Unfortunately, no, because you need a way to override that setting and the ID returned from Run code won't be recognized as a "product". I think it this case you could hack it by calling "Get product data" right after run code. We are considering adding singular actions like (Get single product) to enable this kind of use case, as well as the ability to fetch data about the product after steps like Run code.
That's what I tried in my first post, yeah. As I mentioned, the order/permutation of the items is important in my use case, and Get Product Data doesn't take into account my desired ordering. While I suppose I can unroll the list into 10 variables or so, or even use Send HTTP Request to make an API call for this purpose, I'm definitely looking forward to the singular action and data fetch ideas.
You should be able to return a sorted array of the top 10 product IDs from a Run code step, then fetch those products and iterate over them like discussed but then use Liquid to determine their index in the array. Here is some sample Liquid that does the check which I used in a Log output action. It could be reworked for tagging. Note: the appending of an empty string is to ensure the IDs are both treated as strings for evaluation.
Product ID: {{getProductDataForeachitem.legacyResourceId}}
{%- assign rank = -1 -%}
{%- for top10ProductIds_item in runCode.top10ProductIds -%}
{% assign val1 = top10ProductIds_item | append: '' %}
{% assign val2 = getProductDataForeachitem.legacyResourceId | append: '' %}
{%- if val1 == val2 -%}
{%- assign rank = forloop.index -%}
{%- endif -%}
{%- endfor -%}
Is rank (1-indexed): {{rank}}
Hope that helps!
Run code now supports console.log. See docs here: https://help.shopify.com/en/manual/shopify-flow/reference/actions/run-code
Since the launch of Flow's Run Code action we've been using this action to customize outputs for other actions that need specific information (for example: an http request to mailgun to send an email from a template that receives variables). Sometimes we need certain info with specific formats like currency or a date with internationalization.
How can we know which JavaScript APIs are available? It would be great to know if the runtime you use to run javascript in the Run Code action, or if it is a Flow specific JavaScript runtime, which APIs are available and will be available to use in this Flow Action Block 🙂
It might be easiest to know the limits rather than the other way around: https://help.shopify.com/en/manual/shopify-flow/reference/actions/run-code#limitations
Anything missing from that doc that you want to know?
Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024