This function is a "PUT" REST method for a custom record. It's for our "handling unit" custom record but it's quite generic.
My question is: is there a better/cleaner way to retrieve the internal id of the custom record I'm looking for ? The key I'm searching on is unique, so it can't return more than one result. Also, is there a better way to process the results than forEachResult, since there can only be one result?
Any other suggestions for improvement would be appreciated, as this bit of code is going to get a lot of exercise.
/** * @param {Object} dataIn Parameter object * @returns {Object} Output object */ function putRESTlet(dataIn) { function dumpVars(obj) { var out = ''; for (var i in obj) { out += i + ": " + obj[i] + "n"; } return out; } nlapiLogExecution('DEBUG',"put hu id", dumpVars(dataIn)); if (!("huid" in dataIn)) throw 'no hu id'; var searchFilters = new Array(); searchFilters[0] = new nlobjSearchFilter('externalid', null, 'is', dataIn.huid); var searchColumns = new Array(); searchColumns[0] = new nlobjSearchColumn('internalid'); searchColumns[1] = new nlobjSearchColumn('name'); try { var searchObj = nlapiCreateSearch('customrecord_sea_hunit', searchFilters, searchColumns); var resultSet = searchObj.runSearch(); } catch (e) { nlapiLogExecution('ERROR', 'put hu search caught ' + e.name, e.toString()); return e; } var myHuId; var myRecType; resultSet.forEachResult(function(searchResult) { myHuId = searchResult.getValue('internalid'); myRecType = searchResult.getRecordType(); // process the search result return true; // return true to keep iterating }); var huRec = nlapiLoadRecord(myRecType,myHuId); for ( var fieldname in dataIn) { if (dataIn.hasOwnProperty(fieldname)) { var value = dataIn[fieldname]; if (value && (typeof value != 'object') && (fieldname != 'huid')) // ignore huid and other type of parameters { nlapiLogExecution('DEBUG', 'setting field ' + fieldname + ' value ' + value); huRec.setFieldValue(fieldname, value); } } } try { var recordId = nlapiSubmitRecord(huRec); } catch (e) { nlapiLogExecution('ERROR', 'updating hu caught ' + e.name, e.toString()); return e; } nlapiLogExecution('DEBUG', 'updated HU id=' + recordId); return huRec; }