Automate Inter-company Purchase Order creation

Jira Code: UN-332 Inter-company automation

NetSuite One world with multi subsidiaries may have selling and purchasing between the subsidiaries. In order to facilitate this selling and purchasing, intercompany transactions are to be created. At present, the creation of these intercompany transactions is a manual process.

When a sales order is placed by a customer in NetSuite and if the item is back ordered, then an inter-company purchase order is automatically created against another subsidiary to buy the item. A user event script is used for the creation of purchase order.

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
define([],

    function () {

      
        function afterSubmit(scriptContext) {
            
            var so_id=scriptContext.newRecord.id;
            var salesorderSearchObj = search.create({
                type: "salesorder",
                filters: [
                    ["type", "anyof", "SalesOrd"],
                    "AND",
                    ["internalid", "anyof", so_id],
                    "AND",
                    ["item.quantitybackordered", "greaterthan", "0"]
                ],
                columns: [
      search.createColumn({
                        name: "quantitybackordered",
                        join: "item",
                        label: "Back Ordered"
                    }),
      search.createColumn({
                        name: "itemid",
                        join: "item",
                        label: "Name"
                    }),
      search.createColumn({
                        name: "quantity",
                        label: "Quantity"
                    }),
      search.createColumn({
                        name: "custcol_jj_length_mm",
                        label: "Length(mm)"
                    }),
      search.createColumn({
                        name: "custcol_jj_item_pieces",
                        label: "Pcs"
                    }),
     search.createColumn({
         name: "internalid",
         join: "item",
         label: "Internal ID"
      })
   ]
            });
            var result = salesorderSearchObj.run().getRange({
		      start : 0,
		      end : 1000
	       });
            if(result.length<1)
                return;
            else{
                var subsidiary=scriptContext.newRecord.getValue({
                                fieldId:'subsidiary'
                });
                if(subsidiary==2){
                    var vendor=2441;
                    var location=7;
                }else if(subsidiary==4){
                    var vendor=2447;
                    var location=9;
                }else{
                    return;
                }
                
                var objRecord = record.create({
                type: record.Type.PURCHASE_ORDER, 
                isDynamic: true,
                defaultValues: {
                entity: vendor
                } 
                });
                
                objRecord.setValue({
                fieldId: 'location',
                value: location,
                ignoreFieldChange: false
                });
                for(var i=0;i<result.length;i++){
                    var item=result[i].getValue({
                              name: "internalid",
                              join: "item"
                    });
                    var quantity=result[i].getValue({
                                name: "quantitybackordered",
                                join: "item"
                    });
                    var lineNum = objRecord.selectNewLine({
                    sublistId: 'item'
                    });
                    objRecord.setCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'item',
                    value: item,
                    ignoreFieldChange: false
                    });
                    objRecord.setCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'quantity',
                    value: quantity,
                    ignoreFieldChange: false
                    });
                    objRecord.commitLine({
                    sublistId: 'item'
                    });
                }
                
                var recordId = objRecord.save({
                enableSourcing: true,
                ignoreMandatoryFields: true
                });
                log.debug("recordId",recordId);
            }


        }

        return {
          afterSubmit: afterSubmit
        };

    });

Leave a comment

Your email address will not be published. Required fields are marked *