如何在Suitelet中设置context.currentRecord?

时间:2018-11-17 20:56:12

标签: netsuite suitescript suitescript2.0

我正在创建一个简单的Suitelet,在该页面上,用户输入订单号并单击“搜索”。这将重定向到将订单项信息加载到子列表的页面。在保存这些更改之前,用户可以从那里编辑一些订单项字段。一切都可以保存更改...

我的Suitelet脚本如下:

/**
* @NApiVersion 2.x
* @NScriptType Suitelet
*/
define(['N/search', 'N/ui/serverWidget', 'N/format', 'N/redirect', 'N/https', 'N/record', 'N/error'],
function(s, ui, format, redirect, https, record, error) {

    function onRequest_(context) {  
        try {
            if (context.request.method === 'GET') {
                showForm(context);
            } else {
                showResults(context);
            }
        } catch (e) {
            log.error('onRequest_', 'ERROR : ' + e.message);

            var errObj = error.create({
                name : 'SL ERROR',
                message : e.message,
                notifyOff : true
            });
            throw 'ERROR: ' + e.message;
        }
    }

    function showForm(context) {

        var form = ui.createForm({
            title : 'Update PO'
        });

        var req = context.request;

        var poSearch = form.addField({
            id : 'po_search',
            type : ui.FieldType.TEXT,
            label : 'PO# SEARCH'
        });
        poSearch.isMandatory = true;

        form.addSubmitButton({
            label : 'Search'
        });

        context.response.writePage(form);
    }

    function showResults(context) {

        var form = ui.createForm({
            title : 'Updating PO#' + context.request.parameters['po_search']
        });

        var req = context.request;
        form.clientScriptFileId = 5310184;


        var poSearch = form.addField({
            id : 'po_search',
            type : ui.FieldType.TEXT,
            label : 'PO# Search'
        });
        poSearch.isMandatory = true;
        poSearch.defaultValue = context.request.parameters['po_search'];

        form.addSubmitButton({
            label : 'Search'
        });

        // Button to update the Purchase Order
        form.addButton({
            id : 'custpage_updaterecord',
            label : 'Update Record',
            functionName: 'updateRecord'
        });

        // Create the item sublist
        var itemSublist = form.addSublist({
            id : 'custpage_item',
            type : ui.SublistType.LIST,
            label : 'Item(s)'
        });

        // Item
        var itemCol = itemSublist.addField({
            id : 'custpage_item_item',
            label : 'Item',
            type : ui.FieldType.SELECT,
            source : 'item'
        }).updateDisplayType({
            displayType : ui.FieldDisplayType.INLINE
        });

        // Description
        var descCol = itemSublist.addField({
            id : 'custpage_item_desc',
            label : 'Description',
            type : ui.FieldType.TEXT
        });

        // Purchase Price
        var priceCol = itemSublist.addField({
            id : 'custpage_item_price',
            label : 'Purchase Price',
            type : ui.FieldType.CURRENCY
        }).updateDisplayType({
            displayType : ui.FieldDisplayType.ENTRY
        });

        // Expected Date
        var dateCol = itemSublist.addField({
            id : 'custpage_item_date',
            label : 'Expected Date',
            type : ui.FieldType.DATE
        }).updateDisplayType({
            displayType : ui.FieldDisplayType.ENTRY
        });

        // Vendor SO#
        var vendorsoCol = itemSublist.addField({
            id : 'custpage_item_vendorso',
            label : 'Vendor SO#',
            type : ui.FieldType.TEXT
        }).updateDisplayType({
            displayType : ui.FieldDisplayType.ENTRY
        });

        // Run a search on the PO#, to find the internal Id
        s.create({
            type: s.Type.PURCHASE_ORDER,
            columns: [
                'internalid'
            ],
            filters: [
                s.createFilter({
                    name: "tranid",
                    operator: s.Operator.IS,
                    values: "PO" + context.request.parameters['po_search']
                }),
                s.createFilter({
                    name: "mainline",
                    operator: s.Operator.IS,
                    values: true
                })
            ]
        }).run().each(function(result) {
            // There should be no more than 1 result
            if (result.id) {
                // Load the PO
                var rec = record.load({
                    type: record.Type.PURCHASE_ORDER,
                    id: result.id
                });

                // Loop through the items sublist to create our list
                var lineCount = rec.getLineCount({
                    sublistId: 'item'
                });


                for (i = 0; i < lineCount; i++) {
                    // Get then set the Item
                    var item = rec.getSublistValue({
                        sublistId: 'item',
                        fieldId: 'item',
                        line: i
                    });

                    itemSublist.setSublistValue({
                        id: 'custpage_item_item',
                        line: i,
                        value: item
                    });

                    // Get then set the Description
                    var desc = rec.getSublistValue({
                        sublistId: 'item',
                        fieldId: 'description',
                        line: i
                    });

                    if (desc) {
                        itemSublist.setSublistValue({
                            id: 'custpage_item_desc',
                            line: i,
                            value: desc
                        });
                    }

                    // Get then set the Purchase Price
                    var price = rec.getSublistValue({
                        sublistId: 'item',
                        fieldId: 'amount',
                        line: i
                    });

                    if (price) {
                        itemSublist.setSublistValue({
                            id: 'custpage_item_price',
                            line: i,
                            value: price
                        });
                    }

                    // Get then set the Expected Date
                    var date = rec.getSublistValue({
                        sublistId: 'item',
                        fieldId: 'custcol_mts_expected_date',
                        line: i
                    });

                    if (date) {
                        var formattedDate = format.format({
                            value: date,
                            type: format.Type.DATE
                        });

                        itemSublist.setSublistValue({
                            id: 'custpage_item_date',
                            line: i,
                            value: formattedDate
                        });
                    }

                    // Get then set the Vendor Sales Order #
                    var vendorso = rec.getSublistValue({
                        sublistId: 'item',
                        fieldId: 'custcolcustcol_mts_vendsonumb',
                        line: i
                    });

                    if (vendorso) {
                        itemSublist.setSublistValue({
                            id: 'custpage_item_vendorso',
                            line: i,
                            value: vendorso
                        });
                    }
                }

                return false;
            }
        });

        context.response.writePage(form);
    }

    return {
        onRequest : onRequest_
    }
});

要保存记录,我首先创建了此客户端脚本:

/**
*@NApiVersion 2.x
*@NScriptType ClientScript
*/
define(['N/record'],
function(record) {
    function pageInit(context) {
        try {
            var cr = context.currentRecord;
        } catch (e) {
            console.log('pageInit_ ERROR : ' + e.message);
        }
    }
    function updateRecord(context) {
        console.log("foo");
    }
    return {
    pageInit: pageInit,
    updateRecord: updateRecord
    };
});

在pageInit函数中,我很困惑如何获取正在处理的记录作为context.currentRecord,以便进行更改并提交?

2 个答案:

答案 0 :(得分:4)

对于在Suitelet上运行的客户端脚本,pageInit不会传递任何上下文。您可以通过加载N/currentRecord模块并使用其get()方法在上下文中检索对表单的引用来从当前表单中检索数据。从那里,您可以像普通的Record实例一样使用它。

答案 1 :(得分:1)

有两种方法可以在客户端获取当前记录数据

1)使用N / currentRecord模块

示例:

var currentRecObj = currentRecord.get();
var entity = currentRecObj.getValue('entity');

2)使用客户端SuiteScript函数中的scriptContext参数

示例:

function pageInit(scriptContext) {
    var currentRecObj = scriptContext.currentRecord;
    var entity = currentRecObj.getValue('entity');
}