Any way to the metaobject values into a flow?

Any way to the metaobject values into a flow?

BGalloway
Tourist
5 0 1

I've created a metaobject type called Territory which holds some basic information about which sales territory a customer belongs to. It has the following configuration (truncated to the important bits): 

territory.name,
territory.rep_name,
...
territory.assigned_states // (List of String containing US State names [Alabama, Alaska...])

When an customer is added or updated, my flow will detect if the customer is assigned to a territory and tag associated orders accordingly. If there isn't a territory assigned, it'll attempt to assign one to the customer and then tag any associated orders as usual.

 

I used the new Run Code action type and have manually recreated the important bits in Javascript so I can match the customer's state (province) to the appropriate territory. I've successfully matched customers to my fake territory object, returned the appropriate metaobject ID and used downstream actions to assign the correct metaobject to the customer. While this works, I'd obviously like to avoid managing which states are attached to territories in multiple places. I'd prefer to reference the list of available metaobjects, loop through though and then loop through their associated states. 

 

Here's the Run Code setup:

input: 

query{
  customer{
    defaultAddress {province},
    addresses {province}
  }
}

outputs: 

"The output of Run Code"
type Output {
  "The territory ID linked by the territory Metaobject gid returned by the script"
  territoryId: ID
  "Success boolean"
  success: Boolean!
  "Debug messaging from the runtime"
  debugMessaging: [String]
}

JS:

let messaging = [];
let isSuccess = false;
export default function main(input) {
  return {
    territoryId: getTerritoryId(input.customer),
    success: isSuccess,
    debugMessaging: messaging
  }
}

function getTerritoryId(customer){
  const territoryMapping = [
    {id:"gid://shopify/Metaobject/12345", // Territory 1
       states:[
         "Alabama",
         "Arkansas",
         // ... other states
       ]},
    {id:"gid://shopify/Metaobject/23456", // Territory 2
       states:[
         "Connecticut",
         "Delaware",
         // ... other states
       ]},
    {id:"gid://shopify/Metaobject/34567", // Territory 3
       states:[
         "Alaska",
         "Arizona",
         // ... other states
       ]},
    {id:"gid://shopify/Metaobject/45678", // Default Territory
       states:[
         "National"
       ]}
  ];
  for ( let i = 0;i < territoryMapping.length; i++){
    const territory = territoryMapping[i];
    messaging.push("Found territoryMapping: " + territory.id);
    for ( let j = 0; j < territory.states.length; j++){
      messaging.push("Checking customer state " + customer.defaultAddress.province + " against territory[" + i + "] state " + territory.states[j]);
      if ( customer.defaultAddress.province == territory.states[j] ){
        messaging.push("Found match, returning: " + territory.id);
        isSuccess = true;
        return territory.id;
      }
    }
  }
  messaging.push("No match found...returning blank");
  return "";
}

Has anyone successfully accessed a list of metaobjects usable in the Run Code action object whether referencing from an upstream action or directly in the Run Code action? 

 

Any help is appreciated. Thanks

Replies 6 (6)

paul_n
Shopify Staff
1167 143 275

You currently can't get a metaobject on Flow that is unattached to a resource. We do have a project underway to add improvement Metaobject support. 

 

If the metaobject is attached to a shop, order, customer metafield, then you can get the values. You can also access shop metafields in Flow currently. 

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
BGalloway
Tourist
5 0 1
Paul, thanks for the response. Can you share how I'd access the values of the referenced Metaobject? I couldn't get anything besides the reference ID (Ex. gid://shopify/Metaobject/99999999...). Perhaps I'm just going at it the wrong way? Can I assume it'd be via some HTTP request to the API and parsing the JSON or something?
Thanks, Brant
paul_n
Shopify Staff
1167 143 275

If it's a list, the references are on "<resource>.metafields.references.fields"

 

If it's a single value, use "reference" instead

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
BGalloway
Tourist
5 0 1

Paul, thanks again. Should I be able to reference this in a liquid code block or in the Run Code action? When I attempt to use what you suggested in the Run Code query section for metaobject references, I get an error. The only thing it lets me return is the type. If you mean use liquid and then a loop action downstream, I can explore that route.  Thanks, Brant

 

metaobject reference.png

paul_n
Shopify Staff
1167 143 275

Either, but the syntax would be different. I forgot you need the typename at that level. So you would input "Metaobject" and then fields.

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
paul_n
Shopify Staff
1167 143 275

The syntax is a bit tricky:

query{
  customer {
    metafields {
      references { 
        ...on Metaobject {
          fields {
            key
          }  
        }
      }
    }
  }
}

 

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.