如何使用Suitelet表单更新销售订单?

时间:2019-04-17 21:25:54

标签: netsuite suitescript2.0 clientscript

我创建了一个自定义表单,该表单类似于Netsuite中的SalesOrder。我想使用此自定义表单更新销售订单。

第一部分:我创建了一个自定义表单,其中包含一个名为“销售订单编号”和“提交”按钮的自定义字段。当用户在表单上输入现有的销售订单编号并提交表单时,它将显示与salesorder相关的所有字段。 我已经成功完成了第一部分。我创建了套件并加载了销售订单,并在自定义表单上填充了商品和正文级别字段。

第二部分:当用户在自定义表单上添加项目或更新现有项目并提交cusotm表单时,需要在销售订单记录上对其进行更新。 第二部分我不成功。我尝试创建客户端脚本并与suitlet链接。现在,我无法更新销售订单上的自定义表单字段和项目信息。 请找到以下代码

// Suitlet:

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
        });
        log.debug("errObj: ",errObj);
        throw 'ERROR: ' + e.message;
    }
}
function showForm(context) {

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

    var req = context.request;

    var soSearch = form.addField({
        id : 'so_search',
        type : ui.FieldType.TEXT,
        label : 'SO# SEARCH'
    });
    soSearch.isMandatory = true;

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

    context.response.writePage(form);
 function showResults(context) {

    var form = ui.createForm({
        title : 'Updating SO#' + context.request.parameters['so_search']
    });
   var req = context.request;
   log.debug("req: ",req);
    form.clientScriptFileId = 29546;
    log.debug("form.clientScriptFileId: ",form.clientScriptFileId);
 var soSearch = form.addField({
        id : 'so_search',
        type : ui.FieldType.TEXT,
        label : 'SO# Search'
    });
    soSearch.isMandatory = true;
    soSearch.defaultValue = context.request.parameters['so_search'];

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

    // Button to update the Purchase Order
    form.addButton({
        id : 'custpage_updaterecord',
        label : 'Update Record',
        functionName: 'updateRecord'
    });
 var itemSublist = form.addSublist({
        id : 'custpage_item',
        type : ui.SublistType.INLINEEDITOR,
        label : 'Item(s)'
    });

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

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

    // Price
    var priceCol = itemSublist.addField({
        id : 'custpage_item_price',
        label : 'Price',
        type : ui.FieldType.CURRENCY
    }).updateDisplayType({
        displayType : ui.FieldDisplayType.ENTRY
    });
s.create({
        type: s.Type.SALES_ORDER,
        columns: [
            'internalid'
        ],
        filters: [
            s.createFilter({
                name: "tranid",
                operator: s.Operator.IS,
                values: context.request.parameters['so_search']//"SO" + context.request.parameters['so_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 SO
             log.debug("result.id: ",result.id);
            var rec = record.load({
                type: record.Type.SALES_ORDER,
                id: result.id
            });
            log.debug("rec: ",rec);
            // 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
                });
                log.debug("item: ",item);
                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
                });
                log.debug("desc: ",desc);
                if (desc) {
                    itemSublist.setSublistValue({
                        id: 'custpage_item_desc',
                        line: i,
                        value: desc
                    });
                }

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

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

            return false;
        }
        });

    context.response.writePage(form);
}
return {
    onRequest: onRequest
};

});

//客户端脚本:

function updateRecord() {
     try{
 var currentRecObj = currentRecord.get();
 var cust_clalimnumber = currentRecObj.getValue({fieldId:'so_search'});
 var lineCount = currentRecObj.getLineCount({
          sublistId: 'custpage_item'
      });
      for(var i=0;i<lineCount;i++){
      var cust_itemName = currentRecObj.getSublistValue({
          sublistId: 'custpage_item',
          fieldId: 'custpage_item_item',
          line: i
      });
      alert("cust_itemName: "+cust_itemName);
      var cust_itemDesc = currentRecObj.getSublistValue({
          sublistId: 'custpage_item',
          fieldId: 'custpage_item_desc',
          line: i
      });
      alert("cust_itemDesc: "+cust_itemDesc);
      var cust_itemAmount = currentRecObj.getSublistValue({
          sublistId: 'custpage_item',
          fieldId: 'custpage_item_price',
          line: i
      });
      alert("cust_itemAmount: "+cust_itemAmount);
    var salesorderSearchObj = search.create({
           type: "salesorder",
           filters:
           [
              ["type","anyof","SalesOrd"], 
              "AND", 
              ["mainline","is","T"], 
              "AND", 
              ["numbertext","is",cust_clalimnumber]
           ],
           columns:
           [
               search.createColumn({name: "internalid", label: "Internal ID"}),
               search.createColumn({name: "tranid", label: "Document Number"})

           ]
        });


       var runSearch=salesorderSearchObj.run();
      var results = runSearch.getRange({start: 0, end:999});
      var searchResultCount = salesorderSearchObj.runPaged().count;

        for (var i = 0; results != null && i < results.length; i++){
     // There should be no more than 1 result
            if (result.id) {
                // Load the SO                  
var salesOrderInternalId = results[i].getValue({name:'internalid'});
 }catch(e) {
     alrt("pageInit_ ERROR : " + e.message);
 }
}
}
}
return {

    updateRecord: updateRecord
};

});

0 个答案:

没有答案