During an examination of NetSuite’s customer record management, a peculiar behavior was discovered regarding the updating of address labels. Specifically, when modifying the address line 1 field, it was observed that the associated address label failed to reflect these changes, persisting with the originally set value. Further investigation revealed that while the label can be edited and updated separately within the administrator role, subsequent changes to the address line do not automatically update the label in the customer record.
However, a solution was devised using a Suitelet page in the customer center, where a script was implemented to fetch the value from the edited or updated address line and dynamically set the label column accordingly. This approach effectively resolved the issue, providing a seamless and automated method for updating address labels in NetSuite customer records.
/**
* @NApiVersion 2.1
* @NScriptType Suitelet
*/
/***********
* SteriTek-USA-NS
* STERI-119 : Custom page for Address list in NetSuite
*
*
**************************************************************************************
********
*
* Author: Jobin and Jismi IT Services
*
* Date Created : 17-January - 2024
*
* Description : This script is used to manage the address list in the customer center where customer can add, remove and edit address info
* REVISION HISTORY
*
* @version 1.0 STERI-119 : 17-January-2024 : Created the initial build by JJ0149
* **************************************************************************************
* **/
define(["N/search", "N/record", "N/ui/serverWidget", "N/url", "N/runtime"], (
search,
record,
serverWidget,
url,
runtime
) => {
let customerId;
const CUSTOMER_CENTER_ID = 1033;//To do
const TEST_CUSTOMER_ID = 1955;// To do
const IMAGE_URL =
"https://6714807-sb1.app.netsuite.com/core/media/media.nl?id=14808&c=6714807_SB1&h=c6FuMre9yuePyYqzSlvc2Hhk1bo8R-eSirC_LNfqgDrPtwnS";// To do
const COUNTRY_MAPPING = {
1: "AD",
2: "AE",
3: "AF",
4: "AG",
5: "AI",
6: "IO",
7: "AM",
9: "AO",
10: "AQ",
11: "AR",
12: "AS",
13: "AT",
14: "AU",
15: "AW",
16: "AZ",
17: "BA",
18: "BB",
19: "BD",
20: "BE",
21: "BF",
22: "BG",
23: "BH",
24: "BI",
25: "BJ",
26: "BL",
27: "BM",
28: "BN",
29: "BO",
30: "BR",
31: "BS",
32: "BT",
33: "BV",
34: "BW",
35: "BY",
36: "BZ",
37: "CA",
38: "CC",
39: "CD",
41: "CG",
42: "CH",
43: "CI",
44: "CK",
45: "CL",
46: "CM",
47: "CN",
48: "CO",
49: "CR",
50: "RS",
52: "CU",
53: "CV",
54: "CX",
55: "CY",
56: "CZ",
57: "DE",
58: "DJ",
59: "DK",
60: "DM",
61: "DO",
62: "DZ",
63: "EC",
64: "EE",
65: "EG",
66: "EH",
67: "ER",
68: "ES",
69: "ET",
70: "FI",
71: "FJ",
72: "FK",
73: "FM",
74: "FO",
75: "FR",
76: "GA",
77: "GB",
78: "GD",
79: "GE",
80: "GF",
81: "GG",
82: "GH",
83: "GI",
84: "GL",
85: "GM",
86: "GN",
87: "GP",
88: "GQ",
89: "GR",
90: "GS",
91: "GT",
92: "GU",
93: "GW",
94: "GY",
95: "HK",
96: "HM",
97: "HN",
98: "HR",
99: "HT",
100: "HU",
101: "ID",
102: "IE",
103: "IL",
104: "IM",
105: "IN",
107: "IQ",
108: "IR",
109: "IS",
110: "IT",
111: "JE",
112: "JM",
113: "JO",
114: "JP",
115: "KE",
116: "KG",
117: "KH",
118: "KI",
119: "KM",
120: "KN",
121: "KP",
122: "KR",
123: "KW",
124: "KY",
125: "KZ",
126: "LA",
127: "LB",
128: "LC",
129: "LI",
130: "LK",
131: "LR",
132: "LS",
133: "LT",
134: "LU",
135: "LV",
136: "LY",
137: "MA",
138: "MC",
139: "MD",
140: "ME",
141: "MF",
142: "MG",
143: "MH",
144: "MK",
145: "ML",
146: "MM",
147: "MN",
148: "MO",
149: "MP",
150: "MQ",
151: "MR",
152: "MS",
153: "MT",
154: "MU",
155: "MV",
156: "MW",
157: "MX",
158: "MY",
159: "MZ",
160: "NA",
161: "NC",
162: "NE",
163: "NF",
164: "NG",
165: "NI",
166: "NL",
167: "NO",
168: "NP",
169: "NR",
170: "NU",
171: "NZ",
172: "OM",
174: "PE",
175: "PF",
177: "PH",
178: "PK",
179: "PL",
180: "PM",
181: "PN",
182: "PR",
183: "PS",
184: "PT",
185: "PW",
186: "PY",
187: "QA",
188: "RE",
189: "RO",
190: "RU",
191: "RW",
192: "SA",
193: "SB",
194: "SC",
195: "SD",
196: "SE",
197: "SG",
198: "SH",
199: "SI",
200: "SJ",
201: "SK",
202: "SL",
203: "SM",
204: "SN",
205: "SO",
206: "SR",
207: "ST",
208: "SV",
209: "SY",
210: "SZ",
211: "TC",
212: "TD",
213: "TF",
214: "TG",
215: "TH",
216: "TJ",
217: "TK",
218: "TM",
219: "TN",
220: "TO",
221: "TL",
222: "TR",
223: "TT",
224: "TV",
225: "TW",
226: "TZ",
227: "UA",
228: "UG",
229: "UM",
230: "US",
231: "UY",
232: "UZ",
233: "VA",
234: "VC",
235: "VE",
236: "VG",
237: "VI",
238: "VN",
239: "VU",
240: "WF",
241: "WS",
242: "YE",
243: "YT",
244: "ZA",
245: "ZM",
246: "ZW",
247: "AX",
248: "EA",
249: "IC",
250: "BQ",
251: "CW",
252: "SX",
253: "SS",
254: "XK",
};
const MODEL = {
/**
* @description Function to get value by passing key of an object
* @param {object} obj
* @param {string} targetValue
* @returns
*/
findKeyByValue(obj, targetValue) {
const entry = Object.entries(obj).find(
([key, value]) => value === targetValue
);
// If entry is found, return the key; otherwise, return null
return entry ? entry[0] : null;
},
/**
* @description Function to get states for a country that is available in NetSuite database
* @param {string} country
* @returns
*/
getStates(country) {
let states = [];
try {
let stateSearchObj = search.create({
type: "state",
filters: [
["country", "anyof", country],
"AND",
["inactive", "is", "F"],
],
columns: [
search.createColumn({
name: "id",
sort: search.Sort.ASC,
label: "Id",
}),
search.createColumn({ name: "fullname", label: "Full Name" }),
search.createColumn({ name: "shortname", label: "Short Name" }),
search.createColumn({ name: "country", label: "Country" }),
search.createColumn({ name: "inactive", label: "Inactive" }),
],
});
stateSearchObj.run().each(function (result) {
let temp = {};
temp.id = result.getValue({
name: "id",
sort: search.Sort.ASC,
label: "Id",
});
temp.name = result.getValue({ name: "fullname", label: "Full Name" });
states.push(temp);
return true;
});
return states;
} catch (error) {
log.error("Error @getState", error);
return [];
}
},
/**
* @description This function is to return the state id by passing shiort name of the state since id wasn't retrieved in customer search.
* @param {string} shortname
* @returns
*/
getStateinfo(para, isId) {
try {
let filters = [];
let columns;
if (isId) {
filters.push("AND", ["shortname", "is", para],);
columns = { name: "id", label: "State Id" }
} else {
filters.push("AND", ["id", "equalto", para],);
columns = { name: "shortname", label: "Short Name" }
}
let res;
let stateSearchObj = search.create({
type: "state",
filters: [
["inactive", "is", "F"],
...filters
],
columns: [
search.createColumn(columns),
],
});
stateSearchObj.run().each(function (result) {
res = result.getValue(columns);
});
return res;
} catch (error) {
log.error("Error @getStateId", error);
return "";
}
},
/**
* @description Function to fetch address information for a particuler customer.
* @param {string| Number} customerId
* @returns
*/
getAddressList(customerId) {
let addressList = [];
try {
let customerSearchObj = search.create({
type: "customer",
filters: [["internalidnumber", "equalto", customerId]],
columns: [
search.createColumn({ name: "address", label: "Address" }),
search.createColumn({ name: "address1", label: "Address 1" }),
search.createColumn({ name: "address2", label: "Address 2" }),
search.createColumn({ name: "city", label: "City" }),
search.createColumn({ name: "country", label: "Country" }),
search.createColumn({ name: "state", label: "State/Province" }),
search.createColumn({ name: "zipcode", label: "Zip Code" }),
search.createColumn({ name: "addressee", label: "Addressee" }),
search.createColumn({
name: "addressinternalid",
sort: search.Sort.ASC,
label: "Address Internal ID",
}),
search.createColumn({
name: "addresslabel",
label: "Address Label",
}),
search.createColumn({
name: "isdefaultbilling",
label: "Default Billing Address",
}),
search.createColumn({
name: "isdefaultshipping",
label: "Default Shipping Address",
}),
search.createColumn({
name: "statedisplayname",
label: "State/Province Display Name",
}),
],
});
customerSearchObj.run().each(function (result) {
let temp = {};
temp.addressText = result.getValue({
name: "address",
label: "Address",
});
temp.addressInternalId = result.getValue({
name: "addressinternalid",
label: "Address Internal ID",
});
temp.addressLabel = result.getValue({
name: "addresslabel",
label: "Address Label",
});
temp.isDefaultBilling = result.getValue({
name: "isdefaultbilling",
label: "Default Billing Address",
});
temp.isDefaultShipping = result.getValue({
name: "isdefaultshipping",
label: "Default Shipping Address",
});
temp.address1 = result.getValue({
name: "address1",
label: "Address 1",
});
temp.country = result.getValue({ name: "country", label: "Country" });
temp.state = result.getValue({
name: "state",
label: "State/Province",
});
temp.city = result.getValue({ name: "city", label: "City" });
//log.debug("temp.city",temp.city);
temp.zip = result.getValue({ name: "zipcode", label: "Zip Code" });
temp.address2 = result.getValue({
name: "address2",
label: "Address 2",
});
temp.stateName = result.getValue({
name: "statedisplayname",
label: "State/Province Display Name",
});
temp.addressee = result.getValue({
name: "addressee",
label: "Addressee",
});
addressList.push(temp);
return true;
});
return addressList;
} catch (error) {
log.error("Error @getAddressList", error);
return [];
}
},
};
/**
* @description This function is to create form based mode. Mode will be create or edit
* @param {object} context
* @returns
*/
const createForm = (context,body) => {
try {
let isEdit = context?.request?.parameters?.edit || false;
//let countryDefaultValue = isEdit == "true" ? "US" : 230;
let country = context?.request?.parameters?.country || 230;
//log.debug("country", country)
let countryCode = COUNTRY_MAPPING[country];
let states = MODEL.getStates(countryCode);
//log.debug("states", states);
let form = serverWidget.createForm({
title: isEdit == "true" ? "Edit Address" : "Manage Address",
hideNavBar: isEdit == "true" ? true : false,
});
let address1Field = form.addField({
id: "custpage_address1",
type: serverWidget.FieldType.TEXT,
label: "Address 1",
});
let countryField = form.addField({
id: "custpage_country",
type: serverWidget.FieldType.SELECT,
label: "Country",
source: -159,
});
countryField.isMandatory = true;
let addresseeField = form.addField({
id: "custpage_addressee",
type: serverWidget.FieldType.TEXT,
label: "Addressee",
});
address1Field.isMandatory = true;
let address2Field = form.addField({
id: "custpage_address2",
type: serverWidget.FieldType.TEXT,
label: "Address 2",
});
let cityField = form.addField({
id: "custpage_city",
type: serverWidget.FieldType.TEXT,
label: "City",
});
let stateField;
if (states.length > 0) {
stateField = form.addField({
id: "custpage_state",
type: serverWidget.FieldType.SELECT,
label: "State",
});
stateField.addSelectOption({
value: "",
text: "",
});
for (let i = 0; i < states.length; i++) {
stateField.addSelectOption({
value: states[i].id,
text: states[i].name,
});
}
} else {
stateField = form.addField({
id: "custpage_state",
type: serverWidget.FieldType.TEXT,
label: "State",
});
}
stateField.isMandatory = true;
let zipField = form.addField({
id: "custpage_zip",
type: serverWidget.FieldType.TEXT,
label: "ZIP Code",
});
zipField.isMandatory = true;
let defaultShippingField = form.addField({
id: "custpage_is_default_shipping_address",
type: serverWidget.FieldType.CHECKBOX,
label: "Is Default Shipping Address?",
});
let defaultBillingField = form.addField({
id: "custpage_is_default_billing_address",
type: serverWidget.FieldType.CHECKBOX,
label: "Is Default Billing Address?",
});
if (isEdit == "true") {
let addressIdField = form.addField({
id: "custpage_address_id",
type: serverWidget.FieldType.TEXT,
label: "Address ID",
displayType: serverWidget.FieldDisplayType.HIDDEN,
});
addressIdField.defaultValue = context?.request?.parameters?.addressId;
addressIdField.updateDisplayType({
displayType: serverWidget.FieldDisplayType.HIDDEN,
});
let state = context?.request?.parameters?.state;
let stateId = state ? MODEL.getStateinfo(state, true) : false;
let address1 = context?.request?.parameters?.address1;
let address2 = context?.request?.parameters?.address2;
let addressee = context?.request?.parameters?.addressee;
let city = context?.request?.parameters?.city;
//log.debug("City", city);
let zip = context?.request?.parameters?.zip;
let stateName = context?.request?.parameters?.stateName;
let isDefaultBillingAddress = context?.request?.parameters?.isBilling;
let isDefaultShippingAddress = context?.request?.parameters?.isShipping;
//log.debug("SaedID at edit",stateId);
stateField.defaultValue = states.length > 0 ? stateId : stateName;
address1Field.defaultValue = address1;
address2Field.defaultValue = address2;
cityField.defaultValue = city;
zipField.defaultValue = zip;
addresseeField.defaultValue = addressee;
countryField.defaultValue = country;
defaultShippingField.defaultValue =
isDefaultBillingAddress == "true" ? "T" : "F";
defaultBillingField.defaultValue =
isDefaultShippingAddress == "true" ? "T" : "F";
form.addButton({
id: "custpage_save_address",
label: "OK",
functionName: "editAddress",
});
form.addButton({
id: "custpage_cancel_address",
label: "Cancel",
functionName: `cancelPage`,
});
} else {
form.addButton({
id: "custpage_add_address",
label: "Add",
functionName: "addAddress",
});
form.addButton({
id: "custpage_remove_address",
label: "Remove",
functionName: "removeAddress",
});
countryField.defaultValue = country;
let addressSublist = form.addSublist({
id: "custpage_addresses",
type: serverWidget.SublistType.LIST,
label: "Address",
isDynamic: true
});
addressSublist.addField({
id: "custpage_select",
type: serverWidget.FieldType.CHECKBOX,
label: "Select",
});
addressSublist.addField({
id: "custpage_addressinternalid",
type: serverWidget.FieldType.TEXT,
label: "ID",
displayType: serverWidget.FieldDisplayType.INLINE,
});
addressSublist.addField({
id: "custpage_defaultshipping",
type: serverWidget.FieldType.TEXT,
label: "Default Shipping",
displayType: serverWidget.FieldDisplayType.INLINE,
});
addressSublist.addField({
id: "custpage_defaultbilling",
type: serverWidget.FieldType.TEXT,
label: "Default Billing",
displayType: serverWidget.FieldDisplayType.INLINE,
});
addressSublist.addField({
id: "custpage_addresslabel",
type: serverWidget.FieldType.TEXT,
label: "Label (Taken from Address 1)",
displayType: serverWidget.FieldDisplayType.INLINE,
});
addressSublist.addField({
id: "custpage_wholeaddress",
type: serverWidget.FieldType.TEXT,
label: "Address",
displayType: serverWidget.FieldDisplayType.INLINE,
});
let editOption = addressSublist.addField({
id: "custpage_edit_option",
type: serverWidget.FieldType.TEXTAREA,
label: "Edit",
});
let customerResult = MODEL.getAddressList(customerId);
//log.debug("customerResult",customerResult);
for (let i = 0; i < customerResult.length; i++) {
addressSublist.setSublistValue({
id: "custpage_defaultbilling",
line: i,
value: customerResult[i].isDefaultBilling ? "Yes" : " ",
});
addressSublist.setSublistValue({
id: "custpage_defaultshipping",
line: i,
value: customerResult[i].isDefaultShipping ? "Yes" : " ",
});
addressSublist.setSublistValue({
id: "custpage_addressinternalid",
line: i,
value: customerResult[i].addressInternalId || " ",
});
let addressLabelField = addressSublist.setSublistValue({
id: "custpage_addresslabel",
line: i,
value: customerResult[i].addressLabel || " ",
});
addressSublist.setSublistValue({
id: "custpage_wholeaddress",
line: i,
value: customerResult[i].addressText || " ",
});
let country = MODEL.findKeyByValue(COUNTRY_MAPPING, customerResult[i].country);
let suiteletUrl = url.resolveScript({
scriptId: "customscript_jj_manage_address_steri_119",
deploymentId: "customdeploy_jj_manage_address_steri_119",
params: {
edit: true,
state: customerResult[i].state || "",
address1: customerResult[i].address1 || "",
address2: customerResult[i].address2 || "",
country: country || "",
zip: customerResult[i].zip || "",
isShipping: customerResult[i].isDefaultBilling || false,
isBilling: customerResult[i].isDefaultShipping || false,
stateName: customerResult[i].stateName || null,
city: customerResult[i].city || "",
addressId: customerResult[i].addressInternalId || "",
addressee: customerResult[i].addressee || "",
},
});
let suiteletUrl1 = url.resolveScript({
scriptId: "customscript_jj_manage_address_steri_119",
deploymentId: "customdeploy_jj_manage_address_steri_119",
params: {
edit: true,
address1: customerResult[i].address1 || "",
},
});
let editLink = `<div style="position: relative; display: inline-block;">
<img src=${IMAGE_URL} alt="Edit" style="width: 15px; height: 15px;">
<button onclick="window.open('${suiteletUrl}', '_blank', 'width=800,height=600')" id="editButton" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border: none; background: none; cursor: pointer;"></button>
</div>`;
let editLink1 = `<div style="position: relative; display: inline-block;">
<img src=${IMAGE_URL} alt="Edit" style="width: 15px; height: 15px;">
<button onclick="window.open('${suiteletUrl1}', '_blank', 'width=800,height=600')" id="editButton" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border: none; background: none; cursor: pointer;"></button>
</div>`;
addressSublist.setSublistValue({
id: "custpage_edit_option",
line: i,
value: editLink,
});
addressSublist.setSublistValue({
id: "custpage_edit_option1",
line: i,
value: editLink1,
});
}
}
return form;
} catch (error) {
log.error("Error @createForm", error);
return false;
}
};
const ADDRESS_MANAGER = {
/**
* @description This function is to remove an existing address record foe a particular customer.
* @param {object} body
*/
removeAddress(body, context) {
try {
let customerRecord = record.load({
type: "customer",
isDynamic: true,
id: customerId,
});
let lines = customerRecord.getLineCount({
sublistId: "addressbook",
});
for (let i = lines - 1; i >= 0; i--) {
customerRecord.selectLine({
sublistId: "addressbook",
line: i,
});
let addressId = customerRecord.getSublistValue({
sublistId: "addressbook",
fieldId: "internalid",
line: i,
});
if (body.includes(addressId.toString())) {
customerRecord.removeCurrentSublistSubrecord({
sublistId: "addressbook",
fieldId: "addressbookaddress",
line: i,
});
customerRecord.removeLine({
sublistId: "addressbook",
line: i,
});
}
}
customerRecord.save();
return context.response.write("success");
} catch (error) {
log.error("Error @removeAddress", error);
return context.response.write(error.message);
}
},
/**
* @description This function is to add new address to the customer record.
* @param {object} body
*/
addAddress(body, context) {
try {
let country = body.country;
let city = body.city;
let countryCode = COUNTRY_MAPPING[country];
let states = MODEL.getStates(countryCode);
let stateValue;
if (states.length > 0) {
stateValue = MODEL.getStateinfo(body.state, false);
} else {
stateValue = body.state
}
//log.debug("stateValue", stateValue);
let customerRecord = record.load({
type: "customer",
isDynamic: true,
id: customerId,
});
customerRecord.selectNewLine({
sublistId: "addressbook",
});
customerRecord.setCurrentSublistValue({
sublistId: "addressbook",
fieldId: "defaultshipping",
value: body.defaultShipping,
});
customerRecord.setCurrentSublistValue({
sublistId: "addressbook",
fieldId: "defaultbilling",
value: body.defaultBilling,
});
let myAddressSubRecord = customerRecord.getCurrentSublistSubrecord({
sublistId: "addressbook",
fieldId: "addressbookaddress",
});
let countryValue = COUNTRY_MAPPING[body.country];
myAddressSubRecord.setValue({
fieldId: "addressee",
value: body.addressee,
});
myAddressSubRecord.setValue({
fieldId: "country",
value: countryValue,
});
myAddressSubRecord.setValue({
fieldId: "addr1",
value: body.address1,
});
myAddressSubRecord.setValue({
fieldId: "addr2",
value: body.address2,
});
myAddressSubRecord.setValue({
fieldId: "city",
value: city+",",
});
stateValue && myAddressSubRecord.setValue({
fieldId: "state",
value: stateValue,
});
myAddressSubRecord.setValue({
fieldId: "zip",
value: body.zip,
});
customerRecord.commitLine({
sublistId: "addressbook",
});
customerRecord.save();
return context.response.write("success");
} catch (error) {
log.error("Error @addAddress", error);
return context.response.write(error.message);
}
},
/**
* @description This function is to update existing address records.
* @param {object} body
*/
editAddress( body, context) {
try {
let country = body.country;
let city = body.city.includes(",") ? body.city : body.city + ",";
let countryCode = COUNTRY_MAPPING[country];
let states = MODEL.getStates(countryCode);
let stateValue;
if (states.length > 0) {
stateValue = MODEL.getStateinfo(body.state, false);
} else {
stateValue = body.state
}
let customerRecord = record.load({
type: "customer",
isDynamic: true,
id: customerId,
});
let lines = customerRecord.getLineCount({
sublistId: "addressbook",
});
//log.debug("Line count", lines);
for (let i = 0; i < lines; i++) {
customerRecord.selectLine({
sublistId: "addressbook",
line: i,
});
let addressId = customerRecord.getSublistValue({
sublistId: "addressbook",
fieldId: "internalid",
line: i,
});
if (body.addressId == addressId) {
customerRecord.setCurrentSublistValue({
sublistId: "addressbook",
fieldId: "defaultshipping",
value: body.defaultShipping,
line: i,
});
customerRecord.setCurrentSublistValue({
sublistId: "addressbook",
fieldId: "label",
value: body.address1,
line: i,
});
customerRecord.setCurrentSublistValue({
sublistId: "addressbook",
fieldId: "defaultbilling",
value: body.defaultBilling,
line: i,
});
// customerRecord.setCurrentSublistValue({
// sublistId: "addressbook",
// fieldId: "defaultbilling",
// value: body.defaultBilling,
// line: i,
// });
let myAddressSubRecord = customerRecord.getCurrentSublistSubrecord({
sublistId: "addressbook",
fieldId: "addressbookaddress",
});
let countryValue = COUNTRY_MAPPING[body.country];
myAddressSubRecord.setValue({
fieldId: "country",
value: countryValue,
});
myAddressSubRecord.setValue({
fieldId: "addressee",
value: body.addressee,
});
myAddressSubRecord.setValue({
fieldId: "addr1",
value: body.address1,
});
myAddressSubRecord.setValue({
fieldId: "addr2",
value: body.address2,
});
myAddressSubRecord.setValue({
fieldId: "city",
value: city,
});
myAddressSubRecord.setValue({
fieldId: "state",
value: stateValue,
});
myAddressSubRecord.setValue({
fieldId: "zip",
value: body.zip,
});
customerRecord.commitLine({
sublistId: "addressbook",
line: i,
});
customerRecord.save();
}
}
return context.response.write("success");
} catch (error) {
log.error("Error@editAddress", error);
return context.response.write(error.message);
}
},
};
/**
* Defines the Suitelet script trigger point.
* @param {Object} scriptContext
* @param {ServerRequest} scriptContext.request - Incoming request
* @param {ServerResponse} scriptContext.response - Suitelet response
* @since 2015.2
*/
const onRequest = (context) => {
try {
let action = context.request.parameters.action;
let requestBody = context.request.body;
let body = requestBody ? JSON.parse(requestBody) : false;
log.debug("onreq body ",body );
let currentUser = runtime.getCurrentUser();
customerId = (currentUser.role === CUSTOMER_CENTER_ID) ? currentUser.id : TEST_CUSTOMER_ID;
if (context.request.method === "GET") {
log.debug("onreq body after get: ",body );
const form = createForm(context,body);
if (form) {
form.clientScriptModulePath = "./jj_cs_manage_address_steri_119";
return context.response.writePage(form);
} else {
return context.response.write("Something went wrong. Please contact the administrator!");
}
} else {
// let action = context.request.parameters.action;
// let requestBody = context.request.body;
// let body = requestBody ? JSON.parse(requestBody) : false;
switch (action) {
case "add":
if (body) ADDRESS_MANAGER.addAddress(body, context);
break;
case "remove":
if (body) ADDRESS_MANAGER.removeAddress(body, context);
break;
case "edit":
if (body) ADDRESS_MANAGER.editAddress( body, context);
break;
default:
log.debug("Unsupported action:", action);
}
}
} catch (error) {
log.error("Error @onRequest", error);
return context.response.write(
"Something went wrong!Please contact Administrator"
);
}
};
return { onRequest };
});