Programmatic Access to Lookup Caches

Is there a way to access the Lookup Cache through Javascript? I'm sure there is, but I don't know how.

Hi @jackharris ... sorry, not quite yet.

Lookup cache so far is only usable via Mapper and via the resource API. We are planning to flesh out the rest of the patterns of using lookup cache in the near future - including via JavaScript.

Gotcha, I can work around it I think. Thanks for the response!

While @tonycurcio is correct in that it isn't yet natively supported with our JavaScript runtime functions, you can use virtual exports and virtual imports. If you watched our winter webinar, I showed an example of using a lookup to get cache values for all the SKUs within an order. Instead of using a separate lookup step, I also made an example of how to do this in a script.

import {connections, integrations, exports, imports, flows, request} from 'integrator-api';

var ioConnectionId = "67be8cbe795a7cf36de43ae8";

function preSavePage (options) {
  
  // Extract SKUs from line_items in each data object
  const skus = options.data.reduce((acc, order) => {
    if (order.line_items && Array.isArray(order.line_items)) {
      order.line_items.forEach(item => {
        if (item.sku) {
          acc.push(item.sku);
        }
      });
    }
    return acc;
  }, []);

  let skuLookups = getLookupCacheValues("67be9954e03b1469908c0198", skus);

  const skuMapping = skuLookups.reduce((acc, item) => {
      acc[item.key] = item.value; // Map SKU to its corresponding data
      return acc;
    }, {});
    
  options.data.forEach(order => {
    if (order.line_items && Array.isArray(order.line_items)) {
      order.line_items.forEach(item => {
        if (skuMapping[item.sku]) {
          item.sku_details = skuMapping[item.sku]; // Attach response data
        }
      });
    }
  });
  
  return {
    data: options.data,
    errors: options.errors,
    abort: false,
    newErrorsAndRetryData: []
  }
}

function getLookupCacheValues(lookupCacheId, keys) {
  
  let body = JSON.stringify({keys: keys});
  
  return exports.runVirtual({
    "export": {
      "_connectionId": ioConnectionId,
      "http": {
        "relativeURI": `/v1/lookupcaches/${lookupCacheId}/getData`,
        "method": "POST",
        "body": body,
        "response": {
          "resourcePath": "data"
        }
      }
    }
  }).data;
}

Here was my input:

{
  "data": [
    {
      "id": 6214499205440,
      "line_items": [
        {
          "sku": "012QSSD2NRNR0"
        },
        {
          "sku": "027ddeb13710200044e0bfc8bcbe5d00"
        }
      ]
    }
  ]
}

Here is my output:

3 Likes

Hey @tylerlamparter ... good call on that! Definitely a VERY effective workaround with supported current features !!!!