netsuiteuser-event

How to auto select trans line field sourced from vendor and item


I need to add a check in purchase order whether the item has any minimum order qty (moq) set by the vendor. If yes make sure order qty input is more than OR equal to moq if not prompt error to user.

I created a custom record "Item Vendor Setting" to store vendor item moq with below fields

  1. Vendor
  2. Item
  3. Min order qty

And I created a custom transaction line field "Item Vendor id" which is list record from "item vendor setting" with filter Vendor = trans Vendor and item = trans item.

When I create a PO, after enter the Vendor and item fields, the matched ID is not auto selected in the "item vendor setting" field. Do I need any scripting to achieve this ? Can anyone advise any sample code because I can read code but cannot code from scratch :( Thanks !


I have added the client script but not sure what's wrong there is no error prompt and I can't add any line. Can anyone advise ?

/**
 *@NApiVersion 2.1
 *@NScriptType ClientScript
 *@NModuleScope Public
 */
define(['N/currentRecord', 'N/search'],
    function (currentRecord, search) {

        function validateLine(context) {
            var currentRecord = context.currentRecord;
            var sublistName = context.sublistId;

            if(sublistName === 'item') {
                var recsub = currentRecord.getField({ fieldId: 'subsidiary' });
                var reclineitem = currentRecord.getCurrentSublistValue({ sublistId: sublistName, fieldId: 'item'});
                if (!recsub && !reclineitem) {
                        var subitemlinksearch = search.create({
                            type: "customrecord_fc_item_subsi_fields",
                            filters: [["custrecord_fc_isf_subsidiary", "is", recsub],'and',
                                    ["custrecord_fc_isf_item","is",reclineitem]]
                        });
                        subitemlinksearch.run().each(function (result) {
                            var subitemlink = result.getValue({ name: 'id' });
                            if (subitemlink) {
                                currentRecord.setCurrentSublistValue({ sublistId: sublistName, fieldId: 'custcol_fc_ir_isf_link', value: subitemlink });
                            };
                            return true;
                        });
                    };
            };
        }

        return {
            validateLine: validateLine
        };
    });

Solution

  • Your (!) negations if (!recsub && !reclineitem) {...} are incorrect. It should be the opposite.

    Here you've a sample of how I would handle it (without the help of the custom field). The code hasn't been tested, so there could be some bugs.

    /**
     *@NApiVersion 2.1
     *@NScriptType ClientScript
     *@NModuleScope Public
    */
    define(["N/search", "N/ui/message", "N/ui/dialog"], function (search, message, dialog) {
    
        function validateLine(context) {
            var currentRecord = context.currentRecord;
            var sublistId = context.sublistId;
    
            if (sublistId != 'item') return true;
            
            var subsidiary = currentRecord.getValue({ fieldId: 'subsidiary' });
            var vendor = currentRecord.getValue({ fieldId: 'entity' });
            var item = currentRecord.getCurrentSublistValue({ sublistId: sublistId, fieldId: 'item' });
            var quantity = currentRecord.getCurrentSublistValue({ sublistId: sublistId, fieldId: 'quantity' });
            
            if (!subsidiary) {
                message.create({
                    title: 'Missing Field',
                    type: message.Type.WARNING,
                    message: 'Subsidiary is missing...',
                }).show({ duration: 5000 });
                return false;
            }
            if (!vendor) {
                message.create({
                    title: 'Missing Field',
                    type: message.Type.WARNING,
                    message: 'Vendor is missing...',
                }).show({ duration: 5000 });
                return false;
            }
            if (!item) {
                message.create({
                    title: 'Missing Field',
                    type: message.Type.WARNING,
                    message: 'Item is missing...',
                }).show({ duration: 5000 });
                return false;
            }
            if (!quantity) {
                message.create({
                    title: 'Missing Field',
                    type: message.Type.WARNING,
                    message: 'Quantity is missing...',
                }).show({ duration: 5000 });
                return false;
            }
            var subitemlinksearch = search.create({
                type: "customrecord_fc_item_subsi_fields",
                filters: [
                    ["custrecord_fc_isf_subsidiary", "is", subsidiary],
                    'and',
                    ["custrecord_fc_isf_vendor", "is", vendor], // I don't know how you called this field..
                    'and',
                    ["custrecord_fc_isf_item", "is", item]
                ],
                columns: ["custrecord_fc_isf_minimun_order_quantity"]
            });
            var canProceeed = false
            var results = 0
            subitemlinksearch.run().each(function (result) {
                results++;
                var moq = result.getValue({ name: 'custrecord_fc_isf_minimun_order_quantity' }); // I don't know how you called this field..
                if (+quantity < +moq) {
                    dialog.confirm({
                        title: 'Minimum Order Quantity',
                        message: "The miniminum order quantity is " + moq + ", while you only order " + quantity + ". Do you want to update the quantity?"
                    }).then(function (confirmed) {
                        if (!confirmed) return false;
                        currentRecord.setCurrentSublistValue({ fieldId: 'quantity', sublistId: sublistId, value: moq })
                        currentRecord.commitLine({ sublistId: sublistId });
                    });
                    return false;
                }
                canProceeed = true
                return true;
            });
            if (!results) return true;
            return canProceeed;
        }
    
        return {
            validateLine: validateLine
        };
        
    });