无法通过按钮关闭销售订单

问题描述 投票:0回答:1

我正在尝试将参数发送到suitlet,它捕获参数但未能关闭订单我需要帮助我是suitscripts中的新蜜蜂

我添加了一个用户事件,用于在状态为待批准或待履行的销售订单上添加按钮。但是当我单击按钮时,它无法关闭订单,但它在 suitelet 页面上向我显示销售订单 ID 单击按钮之前单击按钮之后

客户端脚本:

/**
 * @NApiVersion 2.1
 * @NScriptType ClientScript
 */
define(['N/url', 'N/ui/dialog', 'N/log', 'N/currentRecord'], (url, dialog, log, currentRecord) => {
    function pageInit(scriptContext) {

    }

    function showPopup() {
        try {
            const currentRec = currentRecord.get();
            const salesOrderId = currentRec.id;
            const paramsId = {
                orderId : salesOrderId
            }

            const suiteletUrl = url.resolveScript({
                scriptId: 'customscript_bits_ss_close_sales_order',
                deploymentId: 'customdeploy_bits_ss_close_sales_order',
                params: paramsId
            });
            dialog.create({
                title: 'Cancel/Close Order',
                message: `<iframe src="${suiteletUrl}" style="width: 100%; height: 200px; border: none;"></iframe>`,
                buttons: [
                    {
                        label: 'Cancel',
                        value: 'close',
                        action: () => dialog.close()
                    }
                ]
            });

        } catch (error) {
            log.error('Error in showPopup', error.message);
            alert('An error occurred while opening the popup. Check the logs for details.');
        }
    }

    return {
        pageInit,
        showPopup
    };
});

套装脚本:

/**
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */
define(['N/record', 'N/ui/serverWidget', 'N/log'], (record, serverWidget, log) => {
    const onRequest = (scriptContext) => {
        const request = scriptContext.request;
        const response = scriptContext.response;

        const salesOrderId = request.parameters['orderId'] ;
        log.debug('Sales Order ID Received', salesOrderId);

        if (!salesOrderId) {
            response.write('<p>Error: Sales Order ID is missing.</p>');
            return;
        }

        if (request.method === 'GET') {
            try {
                const salesOrder = record.load({
                    type: record.Type.SALES_ORDER,
                    id: salesOrderId
                });
                log.debug('Sales Order Loaded Successfully', salesOrder);

                const form = serverWidget.createForm({
                    title: `Close Sales Order - ID: ${salesOrderId}`,
                    hideNavBar: true
                });

                form.addField({
                    id: 'custpage_message',
                    type: serverWidget.FieldType.INLINEHTML,
                    label: 'Message'
                }).defaultValue = `<p>Are you sure you want to cancel Sales Order ID: ${salesOrderId}?</p>`;

                form.addSubmitButton({ label: 'Confirm Cancellation' });

                form.addButton({
                    id: 'custpage_cancel',
                    label: 'Cancel',
                    functionName: 'window.close();'
                });

                response.writePage(form);
            } catch (error) {
                log.error('Error Loading Sales Order', error);
                response.write('<p>Error: Unable to load the Sales Order. Check the ID and try again.</p>');
            }
        } else if (request.method === 'POST') {
            try {
                const salesOrder = record.load({
                    type: record.Type.SALES_ORDER,
                    id: salesOrderId
                });

                const lineCount = salesOrder.getLineCount({ sublistId: 'item' });
                for (let i = 0; i < lineCount; i++) {
                    salesOrder.setSublistValue({
                        sublistId: 'item',
                        fieldId: 'isclosed',
                        line: i,
                        value: true
                    });
                }

                salesOrder.save();
                response.write('<script>alert("Sales Order canceled successfully."); window.close();</script>');
            } catch (error) {
                log.error('Error Cancelling Sales Order', error);
                response.write('<script>alert("Failed to cancel the Sales Order."); window.close();</script>');
            }
        }
    };

    return { onRequest };
});

请帮我通过销售订单上的按钮关闭销售订单

netsuite suitescript2.0
1个回答
0
投票

当我对 Suitelet 代码进行以下更改时,它对我有用。客户端脚本无需更改:

  1. 删除条件检查 在第 14 行,删除检查销售订单 ID 的条件块:
       if (!salesOrderId) {
           response.write('<p>Error: Sales Order ID is missing.</p>');
           return;
        }
  1. 在 GET 块中添加隐藏字段 在 GET 块中,创建一个额外的隐藏字段来存储销售订单内部 ID:
let recordIdField = form.addField({
    id: 'custpage_record_id',
    type: serverWidget.FieldType.TEXT,
    label: 'Record ID'
});
recordIdField.defaultValue = salesOrderId;
recordIdField.updateDisplayType({
    displayType: serverWidget.FieldDisplayType.HIDDEN
});
  1. 检索 POST 块中的隐藏字段值 在 POST 块中,检索隐藏字段中存储的销售订单内部 ID 的值:
       const recordInternalId = scriptContext.request.parameters.custpage_record_id;
  1. 加载销售订单记录 使用检索到的 ID 加载销售订单记录:
       const salesOrder = record.load({
                type: record.Type.SALES_ORDER,
                id: recordInternalId
            });

Check the full modified code:

    /**
     * @NApiVersion 2.1
     * @NScriptType Suitelet
     */
    define(['N/record', 'N/ui/serverWidget', 'N/log'], (record, serverWidget, log) => {
        const onRequest = (scriptContext) => {
            const request = scriptContext.request;
            const response = scriptContext.response;
    
            const salesOrderId = request.parameters['orderId'] ;
            log.debug('Sales Order ID Received', salesOrderId);
            log.debug("request.method",request.method);
            // if (!salesOrderId) {
            //     response.write('<p>Error: Sales Order ID is missing.</p>');
            //     return;
            // }
    
            if (request.method === 'GET') {
                try {
                    const salesOrder = record.load({
                        type: record.Type.SALES_ORDER,
                        id: salesOrderId
                    });
                    log.debug('Sales Order Loaded Successfully', salesOrder);
    
                    const form = serverWidget.createForm({
                        title: `Close Sales Order - ID: ${salesOrderId}`,
                        hideNavBar: true
                    });
                    let RecordIdField = form.addField({
                        id: 'custpage_record_id',
                        type: serverWidget.FieldType.TEXT,
                        label: 'Record Id'
                    });
                    RecordIdField.defaultValue = salesOrderId;
                    RecordIdField.updateDisplayType({
                        displayType: serverWidget.FieldDisplayType.HIDDEN
                    });
    
                    form.addField({
                        id: 'custpage_message',
                        type: serverWidget.FieldType.INLINEHTML,
                        label: 'Message'
                    }).defaultValue = `<p>Are you sure you want to cancel Sales Order ID: ${salesOrderId}?</p>`;
    
                    form.addSubmitButton({ label: 'Confirm Cancellation' });
    
                    form.addButton({
                        id: 'custpage_cancel',
                        label: 'Cancel',
                        functionName: 'window.close();'
                    });
    
                    response.writePage(form);
                } catch (error) {
                    log.error('Error Loading Sales Order', error);
                    response.write('<p>Error: Unable to load the Sales Order. Check the ID and try again.</p>');
                }
            } else if (request.method === 'POST') {
                try {
                    const RecordInternalId = scriptContext.request.parameters.custpage_record_id;
                    log.debug("RecordInternalId",RecordInternalId);
                    if (!RecordInternalId) {
                        response.write('<p>Error: Sales Order ID is missing.</p>');
                        return;
                    }
                    const salesOrder = record.load({
                        type: record.Type.SALES_ORDER,
                        id: RecordInternalId
                    });
    
                    const lineCount = salesOrder.getLineCount({ sublistId: 'item' });
                    log.debug('lineCount', lineCount);
                    for (let i = 0; i < lineCount; i++) {
                        salesOrder.setSublistValue({
                            sublistId: 'item',
                            fieldId: 'isclosed',
                            line: i,
                            value: true
                        });
                    }
    
                    salesOrder.save();
                    response.write('<script>alert("Sales Order canceled successfully."); window.close();</script>');
                } catch (error) {
                    log.error('Error Cancelling Sales Order', error);
                    response.write('<script>alert("Failed to cancel the Sales Order."); window.close();</script>');
                }
            }
        };
    
        return { onRequest };
    });
© www.soinside.com 2019 - 2024. All rights reserved.