if (customerRecData.count <= 0) {
log.debug("Customer Not Found, Syncing Customer", dataObj.customer_id);
// Fetch customer details from Adobe
let customerAPIURL = adobeLib.ADOBE_API_REQUESTS.FETCH_CUSTOMER_BY_ID.replace("{customerId}", dataObj.customer_id);
log.debug("customerAPIURL", customerAPIURL);
let response = adobeLib.commonFunctions.sendGetAPIRequest(customerAPIURL);
log.debug("response", response);
if (response.code != 200) {
let errorMsg = { custrecord_jj_error_on_order_sync: "Failed to fetch customer details from Adobe." };
updateRecData("customrecord_jj_adobe_order_sync_szco227", integrationRecId, errorMsg);
return;
}
let customerData = JSON.parse(response.body);
log.debug("customerData", customerData);
// Create custom record
let customRecId = createCustomRecord(customerData);
log.debug("Custom Record Created for Customer Sync", customRecId);
// Create customer in NetSuite
let newCustomerId = createCustomerInNetSuite(customerData, customRecId);
if (!newCustomerId) {
let errorMsg = { custrecord_jj_error_on_order_sync: "Failed to create customer in NetSuite." };
updateRecData("customrecord_jj_adobe_order_sync_szco227", integrationRecId, errorMsg);
return;
}
log.debug("Customer Successfully Synced", newCustomerId);
customerRecData = { customerId: newCustomerId };
}
function createCustomRecord(dataObj) {
try {
// create custom record entry
let customRec = record.create({
type: "customrecord_jj_customer_sync_szco138",
isDynamic: true,
});
customRec.setValue({
fieldId: "custrecord_jj_customer_id",
value: dataObj.id,
});
let name = dataObj.firstname + " " + dataObj.lastname;
customRec.setValue({
fieldId: "name",
value: name,
});
customRec.setValue({
fieldId: "custrecord_jj_adobe_api_response_body",
value: JSON.stringify(dataObj),
});
let customRecId = customRec.save({ ignoreMandatoryFields: true });
return customRecId;
} catch (e) {
log.debug("error@createCustomRec", e);
}
}
function createCustomerInNetSuite(customerData, customRecId) {
try {
log.debug('Creating Customer in NetSuite', customerData);
let customerRecord = record.create({ type: record.Type.CUSTOMER, isDynamic: true });
let company = customerData.addresses[0]?.company || '';
let customerName = customerData.firstname + " " + customerData.lastname;
// Set core fields
customerRecord.setValue({ fieldId: "isperson", value: 'T' });
customerRecord.setValue({ fieldId: "firstname", value: customerData.firstname });
customerRecord.setValue({ fieldId: "lastname", value: customerData.lastname });
customerRecord.setValue({ fieldId: "email", value: customerData.email });
customerRecord.setValue({
fieldId: "phone",
value: customerData.addresses[0].telephone,
});
customerRecord.setValue({ fieldId: "subsidiary", value: 2 });
customerRecord.setValue({ fieldId: "entitystatus", value: 13 }); // CUSTOMER-Active
if (adobeLib.commonFunctions.checkForParameter(company)) {
customerRecord.setValue({ fieldId: "companyname", value: company });
}
// Set custom fields
let companyType = adobeLib.savedSearch.fetchCustomAttributes(customerData, "company_type");
customerRecord.setValue({ fieldId: "custentity_jj_company_type", value: companyType });
let taxNo = adobeLib.savedSearch.fetchCustomAttributes(customerData, "tax_id_no");
if (adobeLib.commonFunctions.checkForParameter(taxNo)) {
customerRecord.setValue({ fieldId: "vatregnumber", value: taxNo });
}
let priceLevel = adobeLib.savedSearch.fetchPriceLevelByAdobeId(customerData.group_id);
customerRecord.setValue({ fieldId: "pricelevel", value: priceLevel });
customerRecord.setValue({ fieldId: "custentity_jj_adobe_customer_id", value: customerData.id });
customerRecord.setValue({ fieldId: "leadsource", value: -6 });
// Map Addresses
for (let i = 0; i < customerData.addresses.length; i++) {
customerRecord.selectNewLine({ sublistId: "addressbook" });
let addressSubrecord = customerRecord.getCurrentSublistSubrecord({
sublistId: "addressbook",
fieldId: "addressbookaddress"
});
addressSubrecord.setValue({ fieldId: "addressee", value: customerName });
if (customerData.addresses[i]?.street[0]) addressSubrecord.setValue({ fieldId: "addr1", value: customerData.addresses[i].street[0] });
if (customerData.addresses[i]?.street[1]) addressSubrecord.setValue({ fieldId: "addr2", value: customerData.addresses[i].street[1] });
if (customerData.addresses[i]?.city) addressSubrecord.setValue({ fieldId: "city", value: customerData.addresses[i].city.slice(0, 50) });
if (customerData.addresses[i]?.region?.region) addressSubrecord.setValue({ fieldId: "state", value: customerData.addresses[i].region.region });
if (customerData.addresses[i]?.postcode) addressSubrecord.setValue({ fieldId: "zip", value: customerData.addresses[i].postcode });
if (customerData.addresses[i]?.country_id) addressSubrecord.setValue({ fieldId: "country", value: customerData.addresses[i].country_id });
if (customerData.addresses[i]?.telephone) addressSubrecord.setValue({ fieldId: "addrphone", value: customerData.addresses[i].telephone });
if (customerData.addresses[i]?.default_shipping) {
customerRecord.setCurrentSublistValue({ sublistId: "addressbook", fieldId: "defaultshipping", value: true });
}
if (customerData.addresses[i]?.default_billing) {
customerRecord.setCurrentSublistValue({ sublistId: "addressbook", fieldId: "defaultbilling", value: true });
}
customerRecord.commitLine({ sublistId: "addressbook" });
}
// Save Customer
let customerId = customerRecord.save({ ignoreMandatoryFields: true });
log.debug("Customer Created in NetSuite", customerId);
// Update custom record with customer ID
record.submitFields({
type: "customrecord_jj_customer_sync_szco138",
id: customRecId,
values: { custrecord_jj_customer_in_netsuite: customerId },
options: { enableSourcing: false, ignoreMandatoryFields: true }
});
return customerId;
} catch (e) {
log.error("Error @ createCustomerInNetSuite", e);
record.submitFields({
type: "customrecord_jj_customer_sync_szco138",
id: customRecId,
values: { custrecord_jj_err_adobe_ns_cust_creation: e.message }
});
return null;
}
}