我正在尝试将参数发送到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 };
});
请帮我通过销售订单上的按钮关闭销售订单
当我对 Suitelet 代码进行以下更改时,它对我有用。客户端脚本无需更改:
if (!salesOrderId) {
response.write('<p>Error: Sales Order ID is missing.</p>');
return;
}
let recordIdField = form.addField({
id: 'custpage_record_id',
type: serverWidget.FieldType.TEXT,
label: 'Record ID'
});
recordIdField.defaultValue = salesOrderId;
recordIdField.updateDisplayType({
displayType: serverWidget.FieldDisplayType.HIDDEN
});
const recordInternalId = scriptContext.request.parameters.custpage_record_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 };
});