Guest User

Untitled

a guest
Feb 18th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. /**
  2. * @NApiVersion 2.x
  3. * @NScriptType UserEventScript
  4. */
  5. define([
  6. 'N/log'
  7. ], function(
  8. log
  9. ) {
  10. const exports = {};
  11. function afterSubmit(context) {
  12. var newRecord = context.newRecord
  13. switch (context.type) {
  14. case context.UserEventType.CREATE:
  15. return ;
  16. case context.UserEventType.EDIT:
  17. var payload = getSalesOrderItems(newRecord);
  18. log.debug(payload);
  19. break;
  20. default:
  21. throw 'Invalid event type';
  22. }
  23. }
  24.  
  25. exports.afterSubmit = afterSubmit;
  26. return exports;
  27. });
  28.  
  29. function getSalesOrderItems(salesRecord)
  30. {
  31. var items = [];
  32. var itemLength = salesRecord.getLineCount({
  33. sublistId : 'item'
  34. });
  35.  
  36. if (itemLength === 0) throw 'Order does not have any valid item';
  37.  
  38. for (var index = 0; index < itemLength; index++) {
  39. var item = {};
  40.  
  41. var itemId = salesRecord.getSublistValue({
  42. sublistId: 'item',
  43. fieldId: 'item',
  44. line: index
  45. });
  46.  
  47. try {
  48. var itemRecord = record.load({
  49. type: record.Type.SERIALIZED_INVENTORY_ITEM,
  50. id: itemId,
  51. });
  52. } catch (ex) {
  53. if (JSON.parse(ex).name == "SSS_RECORD_TYPE_MISMATCH") {
  54. itemRecord = record.load({
  55. type: record.Type.KIT_ITEM,
  56. id: itemId,
  57. });
  58. }
  59. }
  60.  
  61. if (!itemRecord) throw ('Item with id ' + itemId + ' does not exist');
  62.  
  63. item.sku = itemRecord.getValue('itemidorig');
  64. item.quantity_committed = salesRecord.getSublistValue({
  65. sublistId: 'item', fieldId: 'quantitycommitted', line: index
  66. });
  67. item.quantity = salesRecord.getSublistValue({
  68. sublistId: 'item', fieldId: 'quantity', line: index
  69. });
  70.  
  71. items.push(item)
  72.  
  73. return items;
  74. }
  75. }
  76.  
  77. {
  78. [{
  79. "sku":"EMOST00405",
  80. "quantity":2
  81. }]
  82. }
  83.  
  84. {
  85. [{
  86. "sku":"EMOST00405",
  87. "quantity_committed": 1,
  88. "quantity":2
  89. }]
  90. }
Add Comment
Please, Sign In to add comment