Shopify function bug for discountAutomaticAppCreate on two different development stores

After following this tutorial for adding a discount function in Shopify through functions and building a UI in Remix: Build a discounts UI with Remix. I tested this particular block of code on two different stores with two different results:

export const action = async ({ params, request }) => {
  const { functionId } = params;
  const { admin } = await shopify.authenticate.admin(request);
  const formData = await request.formData();
  const {
    title,
    method,
    code,
    combinesWith,
    usageLimit,
    appliesOncePerCustomer,
    startsAt,
    endsAt,
    configuration,
  } = JSON.parse(formData.get("discount"));

  const baseDiscount = {
    functionId,
    title,
    combinesWith,
    startsAt: new Date(startsAt),
    endsAt: endsAt && new Date(endsAt),
  };

  const metafields = [
    {
      namespace: "$app:volume-discount",
      key: "function-configuration",
      type: "json",
      value: JSON.stringify({
        quantity: configuration.quantity,
        percentage: configuration.percentage,
      }),
    },
  ];

  if (method === DiscountMethod.Code) {
    const baseCodeDiscount = {
      ...baseDiscount,
      title: code,
      code,
      usageLimit,
      appliesOncePerCustomer,
    };

    const response = await admin.graphql(
      `#graphql
          mutation CreateCodeDiscount($discount: DiscountCodeAppInput!) {
            discountCreate: discountCodeAppCreate(codeAppDiscount: $discount) {
              codeAppDiscount{
                discountId
              }
              userErrors {
                code
                message
                field
              }
            }
          }`,
      {
        variables: {
          discount: {
            ...baseCodeDiscount,
            metafields,
          },
        },
      },
    );

    const responseJson = await response.json();

    const errors = responseJson.data.discountCreate?.userErrors;
    const discount = responseJson.data.discountCreate?.codeAppDiscount;
    return json({ errors, discount: { ...discount, functionId } });
  } else {
    const response = await admin.graphql(
      `#graphql
          mutation CreateAutomaticDiscount($discount: DiscountAutomaticAppInput!) {
            discountCreate: discountAutomaticAppCreate(automaticAppDiscount: $discount) {
              automaticAppDiscount {
                discountId
              }
              userErrors {
                code
                message
                field
              }
            }
          }`,
      {
        variables: {
          discount: {
            ...baseDiscount,
            metafields,
          },
        },
      },
    );

    const responseJson = await response.json();
    const errors = responseJson.data.discountCreate?.userErrors;
    return json({ errors });
  }
};

On my old development store created about 2 - 3 years ago, the following error is returned when calling the discountAutomaticAppCreate mutation:

{
  "data": {
    "discountCreate": {
      "automaticAppDiscount": null,
      "userErrors": [
        {
          "code": "INVALID",
          "message": "Function not found.",
          "field": [
            "automaticAppDiscount",
            "functionId"
          ]
        }
      ]
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 10,
      "actualQueryCost": 10,
      "throttleStatus": {
        "maximumAvailable": 2000,
        "currentlyAvailable": 1990,
        "restoreRate": 100
      }
    }
  }
}

This error is returned even though the function ID is valid and all the suggestions have been followed according to this previous thread with similar errors: Solved: GraphQL can't create Production Function though it can find it - Shopify Community (I can’t link to other threads because Shopify prevents me from adding more than 2 links)

When the same code in Remix is called for a Shopify development store that was created about 1 - 2 weeks ago no error response is returned and a discount is successfully created.

Is this a bug on Shopify’s platform? Do older development stores have no or poor support for newer features provided in Shopify’s APIs?

Hi Jeffquach,

is the older dev store running a specific developer preview? There could be instances where older dev stores don’t have access to newer features, especially if they are tied to a developer preview. We typically wouldn’t recommend using a dev store that’s >2 years old for testing purposes.

No the store isn’t using a specific developer preview. I have tested other Shopify functions on this older development store and they seem to run fine.

I will take note of your suggestion to not test on dev stores > 2 years old.

@Liam-Shopify I just tried this on a brand new dev store with the Checkout Extensibility preview and using a functions only extension. I can see price adjustments work just fine, but when I attempt the mutation to add the discount I end up with the error.

I tried this in both my localhost graphql browser and also directly on the store’s installed Shopify GraphiQL App.

I also tried deleting the cart transform before creating the discount but that didn’t make any difference.

Any thoughts on what could be going on here?

My query:

query queryShopifyFunctions {
  shopifyFunctions(first: 10) {
    nodes {
      id
    }
  }
}

Query results:

"data": {
  "shopifyFunctions": {
    "nodes": [
      {
        "id": "f9297f7e-77fe-440a-a801-13d0d02e395c"
      }
    ]
  }
}

My mutation:

mutation createAppDiscount {
  discountAutomaticAppCreate(
    automaticAppDiscount: {
      title: "Testing!"
      functionId: "f9297f7e-77fe-440a-a801-13d0d02e395c"
      startsAt: "2024-09-25"
    }
  ) {
    automaticAppDiscount {
      discountId
    }
    userErrors {
      field
      message
    }
  }
}

My result:

"data": {
  "discountAutomaticAppCreate": {
    "automaticAppDiscount": null,
    "userErrors": [
      {
        "field": [
          "automaticAppDiscount",
          "functionId"
        ],
        "message": "Function not found."
      }
    ]
  }
}

Turns out for me that my function type was incorrect; I forgot that I’d started down the road to create a Cart Transform. When I generated a new extension with “Discount products - Function” type, the mutation worked as expected.

1 Like

Glad to hear that when you generate this as a discount function it worked correctly :slight_smile: