Efficient way to get a list of store credit accounts with balance

Hi, is there an efficient way to get a list of store credit accounts with balance w/o looping through all customers one by one?

The only way that I am aware of is to loop through all customers w/o a way to narrow it down to customers with store credit account, which is very inefficient: customers - GraphQL Admin

Hey Andrew,

I asked the .dev assistant on shopify.dev and it suggested that it should be possible to “efficiently retrieve a list of store credit accounts with balances without looping through all customers, you can use the storeCreditAccounts field available on the Customer object. This field allows you to query store credit accounts directly, and you can use filters like query to narrow down the results to accounts with balances.”

Here’s an example query:

query GetStoreCreditAccountsWithBalance {
  customers(first: 10) {
    edges {
      node {
        id
        displayName
        storeCreditAccounts(query: "balance.amount:>0") {
          edges {
            node {
              id
              balance {
                amount
                currencyCode
              }
            }
          }
        }
      }
    }
  }
}

Can you test that out? I don’t have customers with store credit to test it on myself.

This will still need to loop through all customers with customers(first: 10) and pagination. It would work if shopify supports this instead: customers(first: 10,query: “balance.amount:>0”)

Hi again Andrew,

I’ve connected with the store credit team to see if there’s a more efficient method, or if they are considering investing in building support for other methods.

Hi Andrew,
Eileen here from the store credit team. There isn’t a better way to do this at the moment. We have an item in our backlog to create a top level query that will allow you to do this type of filtering much more efficiently. I don’t have a specific timeline for this to share, but we will post in the changelog when available.
Also worth noting, in admin you can now build customer segments based on store credit balance.

1 Like

@Eileen_Adams

Wondering if there is any updates on this?

It is not particularly efficient to loop through customers to get something like this.

Ideally a top level query would allow visibility of storeCredits as well as status/currency/customer (so we can check if they are used or not).

This is used downstream to align with Accounting systems.

@Eileen_Adams @Liam-Shopify Also tried this to get a list of customer id and emails with store credit balance so that I can do another query to get the actual store credit accounts but it fails even though it is a valid query:

      query queryWithVariables( $query: String!, $after: String) {
        customerSegmentMembers(first: 250, query: $query, after: $after) {
          edges {
            node {
              id
              defaultEmailAddress {
                emailAddress
              }             
              firstName
              lastName
            }
          }
          pageInfo {
            hasNextPage
            endCursor
          }
        }
      }

variables:

{ "query": "store_credit_accounts MATCHES (balance >= 0.01)" }

It is giving me this error even though it should be a valid query according to the shopify query syntax documentation:

status: 200, code: 0, body: [{"message"=>"Line 1 Column 0: 'MATCHES' is unexpected.", "locations"=>[{"line"=>2, "column"=>38}], "path"=>["customerSegmentMembers", "query"]}]