Skip to content

Instantly share code, notes, and snippets.

@ogoldfield
Last active Aug 10, 2022
Embed
What would you like to do?
Ramp Restlet
/**
*@NApiVersion 2.1
*@NScriptType Restlet
*@NModuleScope SameAccount
*/
define(["N/error", "N/log", "N/record", "N/query"], function (
error,
log,
record,
query
) {
// Get record with provided type & id
function _get(data) {
log.debug("Checkpoint", "Start _get(data)");
log.debug("_get data:", data);
var response = {
status: {
isSuccess: true,
statusDetail: "",
},
data: null,
};
try {
var rec = record.load({
type: data.type,
id: data.id,
});
response.data = rec;
} catch (error) {
response.status.isSuccess = false;
response.status.statusDetail = error;
log.error("Error in _get", error);
}
log.debug("Checkpoint", "End _get(data)");
return JSON.stringify(response);
}
// Delete record with provided type & id
function _delete(data) {
log.debug("Checkpoint", "Start _delete(data)");
log.debug("_delete data:", data);
var response = {
status: {
isSuccess: true,
statusDetail: "",
},
data: null,
};
try {
record.delete({
type: data.type,
id: data.id,
});
} catch (error) {
response.status.isSuccess = false;
response.status.statusDetail = error;
log.error("Error in _delete", error);
}
log.debug("Checkpoint", "End _delete(data)");
return JSON.stringify(response);
}
// Create record with provided type & id
function _post(data) {
log.debug("Checkpoint", "Start _post(data)");
log.debug("_post data:", data);
log.debug("_post data.type:", data.type);
var response = {
status: {
isSuccess: true,
statusDetail: "",
},
data: [],
};
try {
// If SuiteQL query provided, run that and return results
if (data.q) {
if (data.runPaged) {
log.debug("Checkpoint", "_post Running query.runSuiteQLPaged");
var pagedResults = query.runSuiteQLPaged({
query: data.q,
pageSize: data.pageSize || 1000,
});
var iterator = pagedResults.iterator();
iterator.each(function (resultPage) {
response.data.push(...resultPage.value.data.asMappedResults());
return true;
});
} else {
log.debug("Checkpoint", "_post Running query.runSuiteQL");
var results = query.runSuiteQL({
query: data.q,
});
response.data = results.asMappedResults();
}
} else if (data.pullFormFields) {
const response_data = {};
let vendor = record.create({
type: record.Type.VENDOR,
isDynamic: true,
});
let vendorFields = [];
vendor.getFields().forEach((fieldId) => {
let field = vendor.getField({ fieldId });
let defaultValue = vendor.getValue({ fieldId });
if (field) {
vendorFields.push({
label: field.label,
id: field.id,
type: field.type,
isMandatory: field.isMandatory,
isDisplay: field.isDisplay,
defaultValue: defaultValue,
});
}
});
let creditCardCharge = record.create({
type: record.Type.CREDIT_CARD_CHARGE,
isDynamic: true,
});
let creditCardFields = [];
const sublistId = "expense";
let sublistFields = creditCardCharge.getSublistFields({
sublistId: sublistId,
});
sublistFields.forEach((fieldId) => {
let field = creditCardCharge.getSublistField({
sublistId: sublistId,
fieldId: fieldId,
line: 0,
});
if (field) {
creditCardFields.push({
label: field.label,
location: sublistId,
id: field.id,
type: field.type,
isMandatory: field.isMandatory,
isDisplay: field.isDisplay,
defaultValue: "",
});
}
});
creditCardCharge.getFields().forEach((fieldId) => {
let field = creditCardCharge.getField({ fieldId });
let defaultValue = creditCardCharge.getValue({ fieldId });
if (field) {
creditCardFields.push({
label: field.label,
location: "body",
id: field.id,
type: field.type,
isMandatory: field.isMandatory,
isDisplay: field.isDisplay,
defaultValue: defaultValue,
});
}
});
let billFields = [];
let bill = record.create({
type: record.Type.VENDOR_BILL,
isDynamic: true,
});
sublistFields = bill.getSublistFields({ sublistId: sublistId });
sublistFields.forEach((fieldId) => {
let field = bill.getSublistField({
sublistId: sublistId,
fieldId: fieldId,
line: 0,
});
if (field) {
billFields.push({
label: field.label,
location: sublistId,
id: field.id,
type: field.type,
isMandatory: field.isMandatory,
isDisplay: field.isDisplay,
defaultValue: "",
});
}
});
bill.getFields().forEach((fieldId) => {
let field = bill.getField({ fieldId });
let defaultValue = bill.getValue({ fieldId });
if (field) {
billFields.push({
label: field.label,
location: "body",
id: field.id,
type: field.type,
isMandatory: field.isMandatory,
isDisplay: field.isDisplay,
defaultValue: defaultValue,
});
}
});
let billPaymentFields = [];
let billPayment = record.create({
type: record.Type.VENDOR_PAYMENT,
isDynamic: true,
});
billPayment.getFields().forEach((fieldId) => {
let field = billPayment.getField({ fieldId });
let defaultValue = billPayment.getValue({ fieldId });
if (field) {
billPaymentFields.push({
label: field.label,
location: "body",
id: field.id,
type: field.type,
isMandatory: field.isMandatory,
isDisplay: field.isDisplay,
defaultValue: defaultValue,
});
}
});
response_data["global"] = {
vendor: vendorFields,
creditCard: creditCardFields,
bill: billFields,
billPayment: billPaymentFields,
};
response.data = response_data;
} else {
log.debug("Checkpoint", "_post Running record.create");
var rec = null;
if (data.transform) {
rec = record.transform({
fromType: data.transform.fromType,
fromId: data.transform.fromId,
toType: data.transform.toType,
isDynamic: data.isDynamic || false,
});
} else {
rec = record.create({
type: data.type,
isDynamic: data.isDynamic || false,
});
}
for (var field in data.fields) {
log.debug(
"Setting fields:",
"fieldId: " + field + " value: " + data.fields[field]
);
if (field === "trandate" || field === "duedate") {
data.fields[field] = new Date(data.fields[field]);
}
rec.setValue({
fieldId: field,
value: data.fields[field],
});
}
log.debug("Checkpoint", "Setting Sublists");
for (var sublist in data.sublists) {
log.debug("Current Sublist:", sublist);
for (var i = 0; i < data.sublists[sublist].length; i++) {
for (var field in data.sublists[sublist][i]) {
rec.setSublistValue({
sublistId: sublist,
fieldId: field,
line: i,
value: data.sublists[sublist][i][field],
});
}
}
}
var recId = rec.save();
response.data = recId;
}
} catch (error) {
response.status.isSuccess = false;
response.status.statusDetail = error;
log.error("Error in _post", error);
}
log.debug("Checkpoint", "End _post(data)");
return JSON.stringify(response);
}
// Update record with provided type & id
function _put(data) {
log.debug("Checkpoint", "Start _put(data)");
log.debug("_put data:", data);
var response = {
status: {
isSuccess: true,
statusDetail: "",
},
data: null,
};
try {
var rec = record.load({
type: data.type,
id: data.id,
isDynamic: data.isDynamic || false,
});
for (var field in data.fields) {
if (field != "subsidiary") {
log.debug(
"Setting fields:",
"fieldId: " + field + " value: " + data.fields[field]
);
if (field === "trandate") {
data.fields[field] = new Date(data.fields[field]);
}
if (field === "duedate") {
data.fields[field] = new Date(data.fields[field]);
}
rec.setValue({
fieldId: field,
value: data.fields[field],
});
}
}
log.debug("Checkpoint", "Setting Sublists in put");
for (var sublist in data.sublists) {
log.debug("Current Sublist:", sublist);
currentCount = rec.getLineCount({ sublistId: sublist });
for (var i = 0; i < currentCount; i++) {
rec.removeLine({ sublistId: sublist, line: i });
}
for (var i = 0; i < data.sublists[sublist].length; i++) {
for (var field in data.sublists[sublist][i]) {
rec.setSublistValue({
sublistId: sublist,
fieldId: field,
line: i,
value: data.sublists[sublist][i][field],
});
}
}
}
var recId = rec.save();
response.data = recId;
} catch (error) {
response.status.isSuccess = false;
response.status.statusDetail = error;
log.error("Error in _put", error);
}
log.debug("Checkpoint", "End _put(data)");
return JSON.stringify(response);
}
return {
get: _get,
delete: _delete,
post: _post,
put: _put,
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment