Monday 5 December 2016

Dynamically Determine the Netsuite Field Type in an Existing Object

Sometimes back I was ran into  an issue in one of my project where we had a requirement to dynamically display the fields to User in a customized UI and the field has to be displayed according to the definition of Netsuite field types. For example say Today someone has added a  new field into the object and marked it as mandatory then irrespective of the type of the filed which has been created it has to be reflected on the customized UI page to the user and should let the user identify the field as mandatory while creating a record.

So below is the script which would dynamically help to determine the mandatory fields in an Object.



function getFields(datain) {
    var record = nlapiCreateRecord ( datain.recordtype );
    var fields = record.getAllFields();
    var requiredFields = {};
    fields.forEach(function(fieldName){
        var field = record.getField(fieldName);
        if(field.mandatory === true) {
            var id = field.getName();
            var field_details = {}
            field_details['Type'] =  field.getType();
            field_details['Label'] =  field.getLabel();
            if(field.getType() == 'select' || field.getType() == 'multiselect') {
                var Options = field.getSelectOptions();
                var selectOptions = {};
                for(var i in Options) {
                    var opt_id = Options[i].getId();
                    selectOptions[opt_id] = Options[i].getText()
                }
                field_details['Options'] =  selectOptions;
            }
            requiredFields[id]=field_details;
        }
    });
    return requiredFields;
 }​