You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在SuiteScript 2.0中让Inventory Detail脚本仅在Item Receipt上下文运行?

Solution for Running Inventory Detail Client Script Only from Item Receipt

Hey there, let's break down how to make your Inventory Detail client script run only when opened from an Item Receipt, plus handle field defaults and user changes using SuiteScript 2.x.

1. Identify the Parent Record Type (Core Logic)

Inventory Detail is a subrecord, so we can use the createdfrom field on the Inventory Detail record to trace back to its parent. Here's the reliable approach:

  • In the pageInit event, grab the current Inventory Detail record.
  • Fetch the createdfrom field value (this is the internal ID of the parent record).
  • Load the parent record using the N/record module and check its type.

We'll store the parent type in a script-level variable so we don't have to reload the parent record in other events (like fieldChanged), saving unnecessary API calls.

2. Full SuiteScript 2.1 Example

Here's a complete client script that implements this logic, sets default fields, and captures user edits:

/**
 * @NApiVersion 2.1
 * @NScriptType ClientScript
 */
define(['N/record', 'N/ui/message'], (record, message) => {
    let parentRecordType; // Script-level variable to store parent record type

    const pageInit = (context) => {
        const invDetailRec = context.currentRecord;
        const createdFromId = invDetailRec.getValue({ fieldId: 'createdfrom' });

        if (!createdFromId) return; // Exit if no parent record (unlikely for Inventory Detail)

        // Load parent record to check its type
        const parentRec = record.load({
            id: createdFromId,
            isDynamic: false
        });

        parentRecordType = parentRec.type;

        // Only execute custom logic if parent is an Item Receipt
        if (parentRecordType === record.Type.ITEM_RECEIPT) {
            // Example: Set default value for the 'location' field
            invDetailRec.setDefaultValue({
                fieldId: 'location',
                value: 123 // Replace with your target location's internal ID
            });

            // Optional: Show a test confirmation message
            message.create({
                title: 'Script Active',
                message: 'Inventory Detail loaded from Item Receipt',
                type: message.Type.CONFIRMATION
            }).show();
        }
    };

    const fieldChanged = (context) => {
        const invDetailRec = context.currentRecord;
        const changedField = context.fieldId;

        // Skip logic if parent isn't an Item Receipt
        if (parentRecordType !== record.Type.ITEM_RECEIPT) return;

        // Example: React when user modifies the 'quantity' field
        if (changedField === 'quantity') {
            const newQty = invDetailRec.getValue({ fieldId: 'quantity' });
            // Add your custom validation or update logic here
            console.log(`Quantity updated to ${newQty} on Item Receipt Inventory Detail`);
        }
    };

    return {
        pageInit: pageInit,
        fieldChanged: fieldChanged
    };
});

3. Key Details to Note

  • Performance: Loading the parent record once in pageInit and storing its type avoids redundant work in other events.
  • UI Configuration Limitation: There's no way to set up this parent-type check directly in NetSuite's UI—you need a client script to handle the dynamic logic.
  • Field Defaults vs. Overwrites: Use setDefaultValue in pageInit to set pre-interaction defaults. If you need to overwrite existing values (not just set defaults), use setValue instead.
  • Event Scope: For events like fieldChanged or validateField, always check the stored parent type first to ensure your logic only runs for Item Receipt-derived Inventory Details.

4. Deployment Steps

  • Deploy this client script to the Inventory Detail record type (inventorydetail).
  • Configure the script to run in the appropriate contexts (e.g., create, edit) where Inventory Detail is opened from Item Receipts.

内容的提问来源于stack exchange,提问作者Ivan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:26:41