How can I access multiple discountNodes Metafields value in Shopify discount function?

I’m having trouble accessing multiple metafields in Shopify discount functions. I used the **discountAutomaticAppCreate** GraphQL mutation to create two automatic discounts with the same appId and the same metafield namespace.

I’m currently creating the discount using the discountAutomaticAppCreate mutation:

const client = new shopify.clients.Graphql({session});
const data = await client.query({
  data: {
    "query": `mutation discountAutomaticAppCreate($automaticAppDiscount: DiscountAutomaticAppInput!) {
      discountAutomaticAppCreate(automaticAppDiscount: $automaticAppDiscount) {
        userErrors {
          field
          message
        }
        automaticAppDiscount {
          discountId
          title
          startsAt
          endsAt
          status
          appDiscountType {
            appKey
            functionId
          }
          combinesWith {
            orderDiscounts
            productDiscounts
            shippingDiscounts
          }
        }
      }
    }`,
    "variables": {
      "automaticAppDiscount": {
        "title": "Discount - 1",
        "functionId": "abababab-abab-432f-43sdfd-98dd14fb8af5",
        "startsAt": "2025-05-05T17:09:21Z",
        "endsAt": "2025-05-05T17:09:21Z",
        "combinesWith": {
          "orderDiscounts": false,
          "productDiscounts": false,
          "shippingDiscounts": false
        },
        "metafields": [
          {
            "namespace": "test",
            "key": "test-key-1",
            "type": "json",
            "value": "{\n  \"discounts\": [{\n    \"value\": {\"fixedAmount\": {\"amount\": 5}},\n    \"targets\": [{\"orderSubtotal\": {\"excludedVariantIds\": []}}]\n  }],\n  \"discountApplicationStrategy\": \"FIRST\"\n}"
          }
        ]
      }
    },
  },
});

However, I’m unable to retrieve these metafield values within the Shopify discount functions. Below is the input.graphql code I’ve been working with.

Each discount has its own metafield data, but when I try to access them inside the Shopify Function, I can’t retrieve the values.

@tobebuilds suggested this approach, but unfortunately, it’s not working in my case.

query GetCartContents($selectedCollectionIds: [ID!]) {
  cart {
    lines {
      quantity
      merchandise {
        __typename
        ... on ProductVariant {
          id
          product {
            id
            inAnyCollection(ids: $selectedCollectionIds)
          }
        }
      }
    }
  }
 discountNode {
    metafield_1: metafield(namespace: "test", key: "test-key-1") {
      value
    }
    metafield_2: metafield(namespace: "test", key: "test-key-2") {
      value
    }
  }
}

Output I am getting:

"discountNode": {
    "metafield1": null,
    "metafield2": {
      "value": "{\"title\":"Test metadata",\"products\":[]}"
    },
  },

I’d appreciate any insights or suggestions. Thanks in advance!

Hi, just following up, I’m using discountAutomaticAppCreate to create two automatic discounts with the same appId and metafield namespace (but different key), but in my function I can only access one metafield.

@Liam-Shopify @Liz-Shopify @LisaV-Shopify @tobebuilds @lodel @Alan_G @NickWesselman would appreciate any guidance!

From my experience doing the same — but creating the discounts using a Discount Admin UI Extension — the metafields need to be created with the same key.
And when the function runs multiple times each run has its own metafield.

The issue I’m facing is that when I create multiple discounts, the function runs once for each, and it only keeps one. It seems like selectionStrategy: "All" might have a bug, it behaves as if it were set to "MAXIMUM", since only the highest discount is applied across all items.