Guest User

Untitled

a guest
Oct 21st, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. /**
  2. * This library is for testing purpose,
  3. * I split out all NS script and hope it will helps sometime in the future.
  4. */
  5. define(["N/record", "N/log"], function (recordAPI, logAPI) {
  6.  
  7. /**
  8. * create a custom record.
  9. * {
  10. * type: String,
  11. * isDynamic: boolean(option),
  12. * values: {
  13. * key: value,
  14. * sublistId: [
  15. * {
  16. * key: value,
  17. * key: value
  18. * }, {
  19. * key: value,
  20. * key: value
  21. * }
  22. * ]
  23. * }
  24. * }
  25. * @param args
  26. */
  27. function createRecord(args){
  28. if(!args || !args.type) {
  29. logAPI.error({title: "argument 'type' is required but not found in args, args: ", details: args});
  30. return null;
  31. }
  32.  
  33. try{
  34. var record = recordAPI.create({
  35. type: args.type,
  36. isDynamic: args.isDynamic || true
  37. });
  38.  
  39. // assign all record.
  40. _assignValueToRecord(args.values, record);
  41.  
  42. var recordId = record.save();
  43. logAPI.debug({ title: "create " + args.type + " result = ", details: recordId });
  44. return recordId;
  45. } catch(e) {
  46. logAPI.error("error create record ","error detail is :"+e.message);
  47. return null;
  48. }
  49. }
  50.  
  51. function _assignValueToRecord(values, record, sublistId) {
  52. var fieldIds = Object.keys(values);
  53. fieldIds.forEach(function(fieldId, index){
  54. var value = values[fieldId];
  55. if(Array.isArray(value)) {
  56. // Means this is a sublist item.
  57. value.forEach(function(subItemObj) {
  58.  
  59. // Recursive item.
  60. _assignValueToRecord(subItemObj, record, fieldId);
  61. });
  62. } else if (sublistId){
  63. if(index === 0) {
  64. // Need add a New Line item before set any sublist item value.
  65. record.selectNewLine({ sublistId: sublistId });
  66. }
  67.  
  68. record.setCurrentSublistValue({ sublistId: sublistId, fieldId: fieldId, value: value });
  69.  
  70. if (index === (fieldIds.length - 1)) {
  71. // After set all sublist item value, commit line.
  72. record.commitLine({ sublistId: sublistId });
  73. }
  74. } else{
  75. record.setValue({ fieldId: fieldId, value: value});
  76. }
  77. });
  78. }
  79.  
  80. return {
  81. createRecord: createRecord
  82. }
  83. });
Add Comment
Please, Sign In to add comment