1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【実験】SuiteScriptの自動生成 ーその3ー

Last updated at Posted at 2025-05-13

✅ 成功例③「入力フォームで指定した注文書IDの承認待ち情報をJSONで出力」

概要

フォーム画面を通じてユーザーに「注文書ID」の入力を求め、指定された注文書が承認待ちであれば、その情報をJSONで表示するスクリプト。

実行したプロンプト

#NetSuiteのダッシュボード上に「注文書 承認待ち」とき記載があります
#この情報をJSONで出力するスクリプトを生成してください
#出力したい"id"を出力前に入力する仕組みにしてください

生成されたスクリプト

/**
 * @NApiVersion 2.1
 * @NScriptType Suitelet
 */
define(['N/search', 'N/ui/serverWidget'], (search, serverWidget) => {

    const onRequest = (context) => {

        if (context.request.method === 'GET') {

            const form = serverWidget.createForm({ title: '注文書IDを入力してください' });

            form.addField({
                id: 'po_id',
                type: serverWidget.FieldType.TEXT,
                label: '注文書ID'
            }).isMandatory = true;

            form.addSubmitButton({ label: '検索' });

            context.response.writePage(form);

        } else if (context.request.method === 'POST') {

            const poId = context.request.parameters.po_id;

            const purchaseOrderSearch = search.create({
                type: search.Type.PURCHASE_ORDER,
                filters: [
                    ['status', 'anyof', 'PurchOrd:A'], // 「承認待ち」ステータス
                    'AND',
                    ['internalid', 'is', poId]
                ],
                columns: [
                    'tranid',
                    'entity',
                    'trandate',
                    'amount',
                    'status'
                ]
            });

            const results = [];

            purchaseOrderSearch.run().each(result => {
                results.push({
                    id: result.id,
                    tranId: result.getValue('tranid'),
                    vendor: result.getText('entity'),
                    date: result.getValue('trandate'),
                    amount: result.getValue('amount'),
                    status: result.getText('status')
                });
                return true;
            });

            context.response.setHeader({ name: 'Content-Type', value: 'application/json' });
            context.response.write({ output: JSON.stringify(results, null, 2) });
        }
    };

    return { onRequest };
});

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?