Thanks Tyler.
Settings script:
import {
connections,
integrations,
exports,
imports,
flows,
request
} from 'integrator-api';
class Form {
constructor() {
this.fieldMap = {};
this.layout = {};
this.layout.containers =
;
}
fields(...fields) {
this.layout.fields = fields;
}
container(type, label, ...fields) {
this.layout.containers.push({
type: type,
containers: [{
label: label,
fields: fields
}]
});
}
column(label, ...fields) {
this.layout.type = 'column';
this.layout.containers.push({
label: label,
fields: fields
});
}
}
class Field {
constructor(id, name, label, type) {
this.id = id;
this.name = name;
this.label = label;
this.type = type;
this.removeInvalidValues();
}
isRequired() {
this.required = true;
}
removeInvalidValues() {
this.removeInvalidValues = true;
}
canBeDeleted() {
this.showDelete = true;
}
isMultiline() {
this.multiline = true;
}
description(description) {
this.description = description;
}
help(helpText) {
this.helpText = helpText;
}
typeOfInput(type) {
this.inputType = type;
}
default(defaultValue) {
this.defaultValue = defaultValue;
}
delimiter(delimiter) {
this.delimiter = delimiter;
}
keyNamePlaceholder(keyName) {
this.keyName = keyName;
}
valueNamePlaceholder(valueName) {
this.valueName = valueName;
}
mode(mode) {
this.mode = mode;
}
addOption(arg1, arg2, excludeItems) {
if (!excludeItems) {
if (!this.hasOwnProperty('options')) {
this.options = [{
items:
}];
}
if (arg2) {
this.options[0].items.push({
label: arg1,
value: arg2
});
} else {
this.options[0].items.push(arg1);
}
} else {
if (!this.hasOwnProperty('options')) {
this.options = [];
}
if (arg2) {
this.options.push({
label: arg1,
value: arg2
});
} else {
this.options.push(arg1);
}
}
}
addStaticMapOptions(keyName, keyLabel, keyOptions, valueName, valueLabel, valueOptions) {
this.keyName = keyName;
this.keyLabel = keyLabel;
this.keyOptions = keyOptions;
this.valueName = valueName;
this.valueLabel = valueLabel;
this.valueOptions = valueOptions;
}
addStaticMapOptionsMap() {
this.optionsMap =
;
this.allowFailures = false;
this.hideLookupAllowFailures = true;
}
}
function formInit(options) {
let form = new Form();
let { fieldMap, layout } = form;
var connectionId = getConnectionIdByAlias(options, 'netsuiteconnectionalias');
// Header Discount Item
// fieldMap.netsuiteHeaderDiscountItem = new Field('nsHeaderDiscountItem', 'nsHeaderDiscountItem', 'Header Discount Item', 'select');
// let nsItem = getNetSuiteValues(connectionId, 'discountitem', 'itemid', 'internalid');
// for (let s of nsItem) {
// fieldMap.netsuiteHeaderDiscountItem.addOption(s.label, s.value, false);
// }
// Shopify Store ID
fieldMap.shopifyStore = new Field(
'shopifyStore',
'shopifyStore',
'Shopify Store ID',
'text'
);
// Size Metafield
fieldMap.sizeGuide = new Field(
'sizeGuide',
'sizeGuide',
'Size Guide Metaobject ID',
'text'
);
// Location Mapping
fieldMap.locationMap = new Field('nsLocationToShopify', 'nsLocationToShopify', 'Location Mapping', 'staticMap');
fieldMap.locationMap.addStaticMapOptionsMap();
fieldMap.netsuiteLocation = new Field('nsLocation', 'nsLocation', 'NetSuite Location', 'select');
let nsLocations = getNetSuiteValues(connectionId, 'location', 'name', 'internalid');
for (let s of nsLocations) {
fieldMap.netsuiteLocation.addOption(s.label, s.value, true);
}
fieldMap.locationMap.optionsMap.push(fieldMap.netsuiteLocation);
fieldMap.shopifyLocation = new Field('shopLocation', 'shopLocation', 'Shopify Location', 'text');
// Location Metaobject ID
fieldMap.locationMetaobjectId = new Field(
'locationMetaobjectId',
'locationMetaobjectId',
'Location Metaobject ID',
'text'
);
fieldMap.locationMap.optionsMap.push(fieldMap.shopifyLocation);
fieldMap.locationMap.optionsMap.push(fieldMap.locationMetaobjectId);
form.fields(
'shopifyStore',
'sizeGuide',
...['locationMap']
);
options.resource.settingsForm.form = form;
return form;
}
// Remove duplicate location mappings
function scriptInput(options) {
if (
options.resource &&
options.resource.settings &&
options.resource.settings.nsLocationToShopify &&
options.resource.settings.nsLocationToShopify.value
) {
const seen = new Set();
options.resource.settings.nsLocationToShopify.value =
options.resource.settings.nsLocationToShopify.value.filter(function(row) {
const key = row.nsLocation;
if (seen.has(key)) return false;
seen.add(key);
return true;
});
}
return options;
}
function getNetSuiteValues(connectionId, recordType, name, value) {
return exports.runVirtual({
'export': {
'name': 'Lookup',
'_connectionId': connectionId,
'asynchronous': true,
'oneToMany': false,
'netsuite': {
'type': 'restlet',
'skipGrouping': true,
'statsOnly': false,
'restlet': {
'recordType': recordType,
'useSS2Restlets': true,
'columns': [
{
'name': name,
'label': 'label'
},
{
'name': value,
'label': 'value'
}
]
},
'distributed': {
'useSS2Framework': false
}
},
'distributed': {
'disabled': false
},
'adaptorType': 'NetSuiteExport'
}
}).data;
}
function getConnectionIdByAlias(data, aliasToFind) {
let resource = data.resource;
if (!resource.aliases) resource = data.parentResource;
if (!resource.aliases) resource = data.grandParentResource;
if (!resource.aliases) return null;
const aliasEntry = resource.aliases.find(alias => alias.alias === aliasToFind);
return aliasEntry ? aliasEntry._connectionId : null;
}
The Script input holds duplicate entries for some of the Locations, although they don’t show in the UI.