Admin GraphQL API discountAutomaticAppCreate mutation "Function not found" error

Solved

Admin GraphQL API discountAutomaticAppCreate mutation "Function not found" error

adkhamllama
Shopify Partner
27 1 50

I am creating discount app and using Shopify Functions Product Discount API to create discounts. I have used this template ( https://github.com/Shopify/function-examples/tree/main/sample-apps/discounts-tutorial ). It works well and created the app with product discounts extension. To create a discount itself it uses admin GraphQL API with discountAutomaticAppCreate mutation. All works when creating discounts from the app template. However, when I try to send the same request from Postman I am getting the following error:

adkhamllama_0-1660408178941.png

Here is my mutation:

mutation CreateAutomaticAppDiscount($discount: DiscountAutomaticAppInput!) {
discountAutomaticAppCreate(
automaticAppDiscount: $discount
) {
userErrors {
code
message
field
}
}
}
Variables:
{
"discount": {
"functionId": "FUNCTION_ID_FROM_APP",
"title": "POSTMAN DISCOUNT",
"combinesWith": {
"orderDiscounts": false,
"productDiscounts": true,
"shippingDiscounts": true
},
"startsAt": "2022-08-11T14:23:27.757Z",
"endsAt": null,
}
}

It looks like API does not see my function. I checked the functionId, it's correct.
I want to change the project template to a custom one, because of inconvenience and other problems. That is why I cannot continue using this template.

P.S. I tried to send the request from another app's backend (working app) and received the same error.
All requests are done to the store with checkout extensibility preview.

Can anyone help with this?

Accepted Solution (1)

Nick_Wesselman
Shopify Staff
171 43 70

This is an accepted solution.

Currently an app can only reference its own functions. That is, when invoking discountAutomaticAppCreate, the API Key used must belong to the same app as the function.

 

In the case of Postman then, be sure you are using an offline access token for the same app.

Nick Wesselman | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

View solution in original post

Replies 9 (9)

rkmsoftware
Shopify Partner
13 1 4

Hi adkhamllama 

What is the function ID here while creating the app coupon and how we can get that? 
is it AppDiscountType.functionId ?

Nick_Wesselman
Shopify Staff
171 43 70

This is an accepted solution.

Currently an app can only reference its own functions. That is, when invoking discountAutomaticAppCreate, the API Key used must belong to the same app as the function.

 

In the case of Postman then, be sure you are using an offline access token for the same app.

Nick Wesselman | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

RickyMagana
Shopify Partner
6 0 0

I have the same problem when i try save a order discount, my app works fine in development store but i dont understand why when install on production store i get "automaticAppDiscount.functionId Function not found", i tried create others apps and in development store works great but on production I get the same error.

I am installing the app to production by install link.

The SHOPIFY_API_KEY is correct.

 

My question is: what do i have to do for my app works fine on production store.

Nick_Wesselman
Shopify Staff
171 43 70

@RickyMagana This may also indicate that you are attempting to use a custom app with a non-Plus shop. Functions can only be used in custom/single-shop apps with a Plus subscription.

Nick Wesselman | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

RickyMagana
Shopify Partner
6 0 0

That has sense, thank you.

akilvarli
Shopify Partner
1 0 0

hello,

mutation {
discountAutomaticAppCreate(automaticAppDiscount:{
startsAt: "2024-09-25",
title: "Order Discount",
functionId: "4e215005-26ae-486b-b310-54827cdd4cf7"
}) {
automaticAppDiscount {
discountId
}
userErrors {
code
message
}
}
}
This function works in my development test store and I get an internal server error in the production area. I wonder if the Order Discount Function is available only for Shopify Plus subscribers.
Here is the error I encountered during this mutation operation : 
{
"errors": [
{
"message": "Internal error. Looks like something went wrong on our end.\nRequest ID: 690f9175-1aaf-4b24-8caf-5c79c7a2ff72-1727308714 (include this in support requests).",
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"requestId": "690f9175-1aaf-4b24-8caf-5c79c7a2ff72-1727308714"
}
}
]
}


guimei
Shopify Partner
18 0 5

How to get the "offline access token"?

matoow
Shopify Partner
15 1 8
matoow
Shopify Partner
15 1 8

Nick,  Hi, I am struggling with this also.  I am using a custom/single-shop setup, and trying to create a product discount function following the tutorial https://shopify.dev/docs/apps/selling-strategies/discounts/experience/getting-started.

 

When I move it to the production store, running the GraphiQL query to retrieve the functions in step 4, does not return the function, even though it is installed.

 

I followed this https://shopify.dev/docs/apps/auth/oauth/getting-started to create an offline token using the client_id from the app listed in the shopify partner site.   

 

Using this access token, I can now run a graphql, which lists out the function correctly, but when I try to add the discountAutomaticAppCreate via graphql using the same access token, it returns 'function not found'.

 

The token has write_discounts scope.

 

Any idea what I'm doing wrong?

 

Does the store need to be on a PLUS plan for this to work?   

 

"Functions can only be used in custom/single-shop apps with a Plus subscription"

 

This kind of suggests that they do, but I have other (cart-validation) functions working with a single shop/custom setup...???