Skip to content

Instantly share code, notes, and snippets.

@23maverick23
Created August 19, 2020 17:17
Show Gist options
  • Save 23maverick23/30911c85d610ecb92afb1c524c3299ae to your computer and use it in GitHub Desktop.
Save 23maverick23/30911c85d610ecb92afb1c524c3299ae to your computer and use it in GitHub Desktop.
NS: Generate a unique UUID for a Customer
/**
*@NApiVersion 2.x
*@NScriptType UserEventScript
*/
// Load two standard modules.
define ( ['N/record', 'N/search', 'N/ui/serverWidget'] ,
// Add the callback function.
function(record, search, serverWidget) {
// In the beforeSubmit function, add test to the Notes field.
function myBeforeSubmit(context) {
if (context.type !== context.UserEventType.CREATE)
return;
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
var custFldUuid = 'custentity_uuid';
function checkForUniqueness() {
var newUuid = uuidv4();
var mySearch = search.create({
type: search.Type.CUSTOMER,
columns: [custFldUuid],
filters: [custFldUuid, 'contains', newUuid]
});
var myResultSet = mySearch.run();
var resultRange = myResultSet.getRange({
start: 0,
end: 1
});
if (resultRange.length > 0) {
log.debug(resultRange[0]);
checkForUniqueness();
}
return newUuid;
}
var newCustomerRecord = context.newRecord;
newCustomerRecord.setValue({
fieldId: custFldUuid,
value: checkForUniqueness()
});
}
// Add the return statement that identifies the entry point funtions.
return {
beforeSubmit: myBeforeSubmit
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment