Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This library is for testing purpose,
- * I split out all NS script and hope it will helps sometime in the future.
- */
- define(["N/record", "N/log"], function (recordAPI, logAPI) {
- /**
- * create a custom record.
- * {
- * type: String,
- * isDynamic: boolean(option),
- * values: {
- * key: value,
- * sublistId: [
- * {
- * key: value,
- * key: value
- * }, {
- * key: value,
- * key: value
- * }
- * ]
- * }
- * }
- * @param args
- */
- function createRecord(args){
- if(!args || !args.type) {
- logAPI.error({title: "argument 'type' is required but not found in args, args: ", details: args});
- return null;
- }
- try{
- var record = recordAPI.create({
- type: args.type,
- isDynamic: args.isDynamic || true
- });
- // assign all record.
- _assignValueToRecord(args.values, record);
- var recordId = record.save();
- logAPI.debug({ title: "create " + args.type + " result = ", details: recordId });
- return recordId;
- } catch(e) {
- logAPI.error("error create record ","error detail is :"+e.message);
- return null;
- }
- }
- function _assignValueToRecord(values, record, sublistId) {
- var fieldIds = Object.keys(values);
- fieldIds.forEach(function(fieldId, index){
- var value = values[fieldId];
- if(Array.isArray(value)) {
- // Means this is a sublist item.
- value.forEach(function(subItemObj) {
- // Recursive item.
- _assignValueToRecord(subItemObj, record, fieldId);
- });
- } else if (sublistId){
- if(index === 0) {
- // Need add a New Line item before set any sublist item value.
- record.selectNewLine({ sublistId: sublistId });
- }
- record.setCurrentSublistValue({ sublistId: sublistId, fieldId: fieldId, value: value });
- if (index === (fieldIds.length - 1)) {
- // After set all sublist item value, commit line.
- record.commitLine({ sublistId: sublistId });
- }
- } else{
- record.setValue({ fieldId: fieldId, value: value});
- }
- });
- }
- return {
- createRecord: createRecord
- }
- });
Add Comment
Please, Sign In to add comment