Search code examples
javascriptnetsuitesuitescript

Setting Multi-Select field with values from a text field


I am new to NetSuite scripting and learning as I go. I needed values from a text field to set the values in a Multi Select field for Custom Record. This is running in SuiteScript 1.0. (According to people I spoke to, NetSuite does not have a native function to paste mass custom record names in a Multi Select field so the suggestion was to have a text field and an AfterSubmit event.)

My script works in a beforeSubmit event but now trying to work out multiple values rather than singular. See test script below:

function beforeSubmit(type) {
    var existId = checkStr(nlapiGetFieldValue('custevent_ids'));

    if (checkStr(existId) == '') {
        var testIds = nlapiGetFieldValue('custevent_add_ids');
        nlapiSetFieldText('custevent_ids', testIds);
    }
}

I tried different variation of arrays to set the field with multiple custom record names but I just get blank, but singular it sets the custom record.

Thanks for helping a newbie out


Solution

  • This will do what you are asking using SuiteScript 2.0:

    function afterSubmit(context) {
        var existId = context.newRecord.getValue('custevent_ids');
    
        if (existId != null) {
            var loadedRec = record.load({ type: 'customrecord_your_record_type', id: context.newRecord.id });
    
            var testIds = loadedRec.getValue('custevent_add_ids');
            var arrayIds = testIds.split('\r\n');
            log.debug('array', arrayIds);
    
            loadedRec.setText('custevent_ids', arrayIds);
            loadedRec.save();
        }
    }
    

    I haven't worked with SuiteScript 1.0 but here are a few things I think you are missing:

    • The record must be loaded in order to set Select fields (per Krypton's comment)
    • You must split the input field text into an array separated by \r\n
    • If you do use SS 1.0, be sure to use the nlapiSetFieldTexts method to set the Multi-Select field