Created
August 19, 2020 17:17
-
-
Save 23maverick23/30911c85d610ecb92afb1c524c3299ae to your computer and use it in GitHub Desktop.
NS: Generate a unique UUID for a Customer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
*@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