i have a script that is validating fileds but is n...
# suitescript
s
i have a script that is validating fileds but is not working
Copy code
/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define(['N/ui/dialog'], function (dialog) {
    function saveRecord(context) {
      



        var record = context.currentRecord;
        var Usamazonsku = record.getValue({ fieldId: 'custitem3'});
        var Caamazonsku = record.getValue({ fieldId: 'custitem9' });
        var amazonFlag = record.getValue({ fieldId: 'custitem_fa_amz_fba01'});
        var amazonFlag2 = record.getValue({ fieldId: 'custitem_fa_amz_fba02'});


        var conditions = [
            Usamazonsku != null && amazonFlag === false,
            Caamazonsku != null && amazonFlag2 === false,
            Usamazonsku === null && amazonFlag === true,
            Caamazonsku === null && amazonFlag2 === true
        ]
        console.log(!conditions.includes(false))
        if (conditions.includes(true)){
      console.log("I am watching")
        var options = {
            title: 'Error',
            message: 'All Amazon fields have not been filed out correctly !!'
        };
        dialog.alert(options);
    return false
    }
    else{
        return true
    }
    }
     return {
        saveRecord: saveRecord 
    };
})
b
you probably want to be more specific.
whats not working about? What does it do instead
s
i want to make sure if the customer sku for amazon is not null (filled out ) then the amazon fba is set to true and vices versa
s
I don;t believe
.includes()
is in ECMA5?
Try using
.indexOf(true) > -1
maybe?
b
i can tell you that you probably made a logic mistake in your use of includes
not includes false
is not the same
as includes true
not sure if thats causing an issue since i dont know whats wrong
s
if the user tries to save it falies then goes to correct error does the script reevaluite ?
s
saveRecord is returning a true or false, if you return false there is no re-evaluate, it stops the save interaction
and can be called again when the user tries to save again
s
that is the issue i have when saving again it still fails
s
I would make sure
includes()
is working, or its a flaw in the logic as battk suggested, I am not
includes()
is supported in the version of ECMA
s
im not use included trying indexof
b
you should probably log your array
conditions, instead of whatever you are logging now
i would expect that your null comparisons are causing unexpected errors
lots of things are != null
netsuite itself will rarely return null clientside
s
Yeah I would separate and simplify the checks, and change the alert/return false to be specifically which one is causing the problem, makes its easier to read/figure out what is going on
s
yeah thanks guys , got it working switched the logic and using indexof
if netsuite sends back null the script will break
b
its usually safer to rely on truthy and falsy values
s
var conditions = [ !Usamazonsku && amazonFlag === true, !Caamazonsku && amazonFlag2 === true, Usamazonsku && amazonFlag ===false, Caamazonsku && amazonFlag2 === false ]
b
!Usamazonsku && amazonFlag
mostly the same, less comparing booleans to booleans
j
I wrote myself a module to handle this because I got tired of checking all the “doesn’t have a value” stuff.
Copy code
/**
 * md_integrity_ss2.js
 * @ssScriptName ModuleIntegrity
 * @ssScriptId md_integrity
 * @ssApiVersion 2.0
 * 
 * last modified 2020-06-23 JB
 */

define(function() {

	/* Define the functions in this module. */
			
	
	// Returns true if the passed in thing has a value, false if blank, null, undefined, empty, or ‘To Be Generated’.
	// We can override whether or not to include blank, undefined, empty, or ‘To Be Generated’ as null.
	function hasValue(value_to_check, options) {
		
		// Set up some default options.
		if(options === undefined) {
			
			options = {
				check_to_be_generated: true,
				check_blank: true,
				check_undefined: true,
				check_empty_array: true,
				check_is_nan: false
			}
			
		}
		
		if(options.check_to_be_generated === undefined)
			options.check_to_be_generated = true;
		
		if(options.check_blank === undefined)
			options.check_blank = true;
		
		if(options.check_undefined === undefined)
			options.check_undefined = true;
		
		if(options.check_empty_array === undefined)
			options.check_empty_array = true;
		
		if(options.check_is_nan === undefined)
			options.check_is_nan = false;
		
		
		// Check if the passed-in-value has a value!
		if(value_to_check === null)
			return false;
		
		if(options.check_blank && value_to_check === ‘’)
			return false;
		
		if(options.check_undefined && value_to_check === undefined)
			return false;
		
		if(options.check_to_be_generated && value_to_check == ‘To Be Generated’)
			return false;
		
		if(options.check_empty_array && typeof value_to_check !== ‘string’ && value_to_check.length == 0)
			return false;
		
		if(options.check_is_nan && isNaN(value_to_check))
			return false;
		
		// At this point it passed all the checks so return true.
		return true;
		
	}
	
	// If the supplied value is an empty string, return null instead.  Useful for 
	// adding values to a sublist, which doesn’t seem to accept blank strings.
	function nullIfEmpty(value) {
		
		if(value == ‘’)
			return null;
		
		else
			return value;
		
	}
	
	/* Return the names of the functions.  Note that these can be different from the
	 * actual names above, but for clarity let’s keep them the same. 
	 */
	return {
		
		hasValue: hasValue,
		nullIfEmpty: nullIfEmpty
		
	}
	
});
then I don’t have to remember all the stuff to check, I just do
if(md_integrity.hasValue(thing))