Check unicity of a field value
Hello,
I am currently struggling with SuiteScript 2.0. I am a beginner on SuiteScript and this is my first script.
I check all parameters on the record and the script is well deployed and activate on the record.
I test on a function pageInit to eventually pop up a window on page init but even this doesn’t work. I don’t understand why.
Then the function fieldchange is checking if there is unicity on a field in all records already save. It doesn’t work and I don’t know why. Execution log doesn’t appear unless the script is active and deployed.
Here is the script I try :
/**
* @NApiVersion 2.0
* @NScriptType ClientScript
* @NModuleScope Public
*/
define(['N/currentRecord', 'N/log', 'N/record', 'N/search'],
/**
* @param{currentRecord} currentRecord
* @param{log} log
* @param{record} record
* @param{search} search
*/
function(currentRecord, log, record, search) {
function pageInit(scriptContext){
var currentRecord = scriptContext.currentRecord;
var id = currentRecord.id;
console.log(id);
alert({
title: 'Hello World!',
message: 'Click OK to continue.'+id
});
}
/**
* Function to be executed when field is changed.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
* @param {string} scriptContext.fieldId - Field name
* @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
* @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
*
* @since 2015.2
*/
function fieldChanged(scriptContext) {
var currentRec = scriptContext.currentRecord;
var po = currentRec.getValue('otherrefnum');
var customer = currentRec.getValue('entity');
if (scriptContext.fieldId === 'otherrefnum') {
try {
alert('un message important');
console.log('un message');
po = po.trim();
log.error('saveRecord' , JSON.stringify(scriptContext));
log.error('currentRecord' + JSON.stringify(currentRec));
var exist = searchPO(customer, po);
if (exist) {
alert('The po "' + currentRec.getText('otherrefnum') + '" already exists for the sales order.');
} else {
return true;
}
}
catch(e) {
console.log(e);
log.debug({
title: '[Error while writing PO]',
details: 'Error while writing PO' + (e)
});
}
}
}
function searchPO (entity, po){
var exist = false;
var searchPo = search.create({
type: search.Type.SALES_ORDER,
filters: [["entity","is",entity], "AND", ["otherrefnum", "is" ,po]]
});
var searchResultCount = searchPo.runPaged(currentRecord).count;
if (searchResultCount > 0) {
exist = true;
}
return exist;
}
/**
* Validation function to be executed when field is changed.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
* @param {string} scriptContext.fieldId - Field name
* @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
* @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
*
* @returns {boolean} Return true if field is valid
*
* @since 2015.2
*/
function validateField(scriptContext) {
}
/**
* Validation function to be executed when record is saved.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @returns {boolean} Return true if record is valid
*
* @since 2015.2
*/
function saveRecord(scriptContext) {
}
return {
pageInit : pageInit,
fieldChanged: fieldChanged,
//validateField: validateField,
//saveRecord: saveRecord
};
});
Could you help me with it ?
Thank you !
Hi,
Are you saying nothing appears to be running at all?
If so, could you please share some screenshots of your script and script deployment records.
Thanks,
Chris







