Jurgen
06/28/2021, 1:50 PM/**
*@NApiVersion 2.1
*@NScriptType ClientScript
*/
define(["N/record", "N/ui/dialog", "N/url", "N/search"], function (record, dialog, url, nSearch) {
//
// Jurgen Rense
// This script is executed on the Customer.
// When the Customer is created or updated there will be a validation if the Hubspot key is unique, if not the duplicate customer will be shown in the error message
//
function saveRecord(context) {
var rec = context.currentRecord;
// Get Customer
var customerId = rec.getValue({ fieldId: "id" });
var customerHubspotKey = rec.getValue({
fieldId: "custentity_inn_hubspot_key",
});
var duplicates = [];
// if (validateSaveRecord(customerHubspotKey)) {
// log.debug({
// title: "True line 26"
// });
// return true;
// } else {
// log.debug({
// title: "False line 31"
// });
// return false;
// }
validateSaveRecord(customerHubspotKey)
async function validateSaveRecord(customerHSKey) {
if (customerHSKey) {
// return new Promise(function (resolve) {
log.debug({ title: "Zoeken" });
await findCustomerHubspotKey(customerId, customerHSKey).then (function(result) {
log.debug({ title: "Result" + duplicates });
//log.debug({ title: resultSet ? "ja" : "nee", details: resultSet });
if (validationCheck(duplicates)) {
log.debug({ title: "True" });
return true;
}
log.debug({ title: "False" });
return false;
});
// });
} else {
log.debug({ title: "no key" + customerHSKey });
return true;
}
}
async function findCustomerHubspotKey(customerId, customerHubspotKey) {
var search = nSearch.create({
type: "customer",
filters: [
["internalid", "noneof", customerId],
"AND",
["custentity_inn_hubspot_key", "contains", customerHubspotKey],
],
columns: [
nSearch.createColumn({ name: "entityid", label: "entityid" }),
nSearch.createColumn({ name: "companyname", label: "companyname" }),
],
});
var resultSet = await search.run().each.promise(function (result) {
//duplicateHubspotKeyCustomer = result.getValue({name: "entityid"});
duplicates.push(result.getValue({ name: "entityid" }));
log.debug({ title: "Debug in functie: ", details: duplicates });
dialog.alert({
title: "Error",
message:
"<p>Hubspot key already exists for customer: " +
result.getValue({ name: "companyname" }) +
"</p><p><br>Please make sure that you use the correct Hubspot key or merge the lead/prospect/customer</p>",
});
});
}
function validationCheck(duplicates) {
log.debug({ title: "length: " + duplicates.length });
if (duplicates.length === 0) {
log.debug({
title: "True - Debug ",
details: duplicates,
});
return true;
} else {
log.debug({
title: "False - Debug",
details: duplicates,
});
return false;
}
}
}
return {
saveRecord: saveRecord,
};
});
Sandii
06/28/2021, 2:49 PMvalidateSaveRecord
instead of calling/returning it, your return function is not returning anythingJurgen
06/28/2021, 2:50 PMJurgen
06/28/2021, 2:50 PMSandii
06/28/2021, 2:51 PMJurgen
06/28/2021, 2:57 PMJurgen
06/28/2021, 3:07 PMJurgen
06/28/2021, 3:07 PMSandii
06/28/2021, 3:11 PMSandii
06/28/2021, 3:14 PMexports.saveRecord = function saveRecord(context) {
// read id and hubspot key
// if key is empty/null, return true
// search.create() with key/id,
// if search.run().getRange(0,1).length return false
// return true
}
thats really all the logic you needSandii
06/28/2021, 3:16 PMJurgen
06/28/2021, 6:22 PMrazer456
06/28/2021, 6:25 PMJurgen
06/28/2021, 8:15 PMrazer456
06/29/2021, 4:50 AMJurgen
06/29/2021, 7:06 AMSandii
06/29/2021, 4:50 PM