# Issue using variables in function input queries

**URL:** <https://community.shopify.com/t/issue-using-variables-in-function-input-queries/287948>\
**Category:** Functions\
**Tags:** optimization, input-queries, Setting-Up\
**Created:** [January 23, 2024, 4:15am UTC](https://community.shopify.com/t/issue-using-variables-in-function-input-queries/287948 "2024-01-23T04:15:39Z")\
**Posts on this page:** 3\
**Page:** 1

## AI-generated summary

A discount Function uses an input variable ($selectedCollectionIds) to check product membership via inAnyCollection, but results are always false. The metafield holding the IDs (namespace: discountApp, key: collectionData) was created on the Shop.

Key update: The issue is the metafield owner. For Function input variables, values must come from a metafield on the function owner (the discount), not the Shop. Change ownerId to the discount (function owner) when setting the metafield so the variable is populated in the input query.

Clarification: You can read Shop metafields in the input query (e.g., for other data), but you cannot use Shop metafields to supply input variables; variables must be on the function owner.

Open question: A participant asks how to manage shared, frequently changing logic across hundreds of discounts if variables must be per-discount, implying potential maintenance overhead. No resolution provided; guidance focuses on correcting the metafield owner to fix the immediate issue.

<div class="post-metadata">

**Author:** ![SomeUsernameHe](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/someusernamehe/32/321738_2.png) [@SomeUsernameHe](https://community.shopify.com/u/SomeUsernameHe)\
**Post date:** [January 23, 2024, 4:15am UTC](https://community.shopify.com/t/issue-using-variables-in-function-input-queries/287948/1 "2024-01-23T04:15:39Z")

</div>

I am developing a Shopify Function to apply discounts based on customer tags and product collection memberships.

collectionData (Namespace: discountApp): Holds only the variable selectedCollectionIds with an array of collection IDs.

```auto
          {
            "node": {
              "id": "gid://shopify/Metafield/27485936845038",
              "namespace": "discountApp",
              "key": "collectionData",
              "value": "{\"selectedCollectionIds\":[\"gid://shopify/Collection/422046531822\",\"gid://shopify/Collection/421527421166\"]}",
              "type": "json",
            }
          }

```

I have included this in my shopify.extension.toml file:

```css
[extensions.input.variables]
  namespace = "discountApp"
  key = "collectionData"

```

and here is my run.graphql:

```css
query Input($selectedCollectionIds: [ID!]) {
  cart {
    lines {
      merchandise {
        ... on ProductVariant {
          product {
            id
            handle
            inAnyCollection(ids: $selectedCollectionIds)
          }
        }
      }
    }
    buyerIdentity {
      customer {
        metafield(namespace: "discountApp", key: "tag") {
          value
        }
      }
    }
  }
  shop {
    metafield(namespace: "discountApp", key: "tags") {
      value
    }
  }
}

```

Here is what is returned to me from Shopify:

```css
{
  "cart": {
    "lines": [
      {
        "merchandise": {
          "product": {
            "id": "gid://shopify/Product/8212928430318",
            "handle": "hat",
            "inAnyCollection": false
          }
        }
      },
      {
        "merchandise": {
          "product": {
            "id": "gid://shopify/Product/8208496492782",
            "handle": "t-shirt",
            "inAnyCollection": false
          }
        }
      }
    ],
    "buyerIdentity": {
      "customer": {
        "metafield": {
          "value": "Pro"
        }
      }
    }
  },
  "shop": {
    "metafield": {
      "value": "{\"Pro\":{\"collections\":{\"hats\":\"20%\",\"pants\":\"10%\",\"shirts\":\"35%\"}},\"VIP\":{\"collections\":{\"hats\":\"30%\",\"pants\":\"20%\",\"shirts\":\"40%\"}}}"
    }
  }
}

```

and here is the graphql code I used to create the metafield:

```css
mutation {
  metafieldsSet(metafields: [
    {
      namespace: "discountApp",
      key: "collectionData",
      type: "json",
      value: "{\"selectedCollectionIds\":[\"gid://shopify/Collection/422046531822\",\"gid://shopify/Collection/421527421166\"]}",
      ownerId: "gid://shopify/Shop/61637230830"
    }
  ]) {
    metafields {
      id
    }
  }
}

```

But no matter what I do, I cannot get inAnyCollection to be true. Any suggestions or help?

---

<div class="post-metadata">

**Author:** ![Nick\_Wesselman](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/nick_wesselman/32/309011_2.png) [@Nick\_Wesselman](https://community.shopify.com/u/Nick_Wesselman)\
**Post date:** [January 23, 2024, 1:49pm UTC](https://community.shopify.com/t/issue-using-variables-in-function-input-queries/287948/2 "2024-01-23T13:49:02Z")

</div>

This is your issue here:

```javascript
ownerId: "gid://shopify/Shop/61637230830"

```

As mentioned in [input query docs](https://shopify.dev/docs/apps/functions/input-output/variables-queries#step-2-set-the-metafield), the variable values need to be set on a metafield on the [function owner](https://shopify.dev/docs/apps/functions/input-output/queries-metafields#how-it-works), which is your case is probably the discount, based on your metafield naming.

---

<div class="post-metadata">

**Author:** ![Stephen2020](https://avatars.discourse-cdn.com/v4/letter/s/e36b37/32.png) [@Stephen2020](https://community.shopify.com/u/Stephen2020)\
**Post date:** [January 23, 2024, 4:22pm UTC](https://community.shopify.com/t/issue-using-variables-in-function-input-queries/287948/3 "2024-01-23T16:22:17Z")

</div>

Does this mean that you can read shop metafields in the input query, but you can’t use shop metafields for the input variables?

If so, this is a significant limitation. What do you do if you have several hundred discount codes that all follow the same logic, which changes on a weekly basis?
