首页 > 解决方案 > 如何在 Suitelet 发布上下文中显示客户端警报?

问题描述

在将数据输入此套件表单时,如何显示错误,以便我可以显示如下有关重复 ID 记录的消息?这是否需要在客户端脚本中完成,套件是否需要有一个与执行记录创建的客户端脚本链接的按钮,然后重定向到另一个显示表单信息的套件?似乎必须有一种更简单的方法来显示客户端错误,而无需重定向两次。

套件示例:

define([ 'N/ui/serverWidget', 'N/record', 'N/cache'],
    function( serverWidget,  record, cache) {
        function onRequest(context) {
            var func = 'Suitelet';
            var request = context.request;
            if(request.method == 'GET'){
                try{
                    var form = serverWidget.createForm({
                        title: 'Add sample data'
                    });

                    var idField = form.addField({
                        id: 'custpage_sample_id',
                        type: serverWidget.FieldType.TEXT,
                        label: 'ID'
                    });

                    idField.isMandatory = true;

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

                    context.response.writePage(form);
                }
                catch(e){
                    log.error(func, JSON.stringify(e));
                }
            }
            else {
                try{
                    var id = request.parameters.custpage_id;

                    log.debug(func, id);

                    if(Id){
                        var existing = getExistingId(Id);
                        if(!existing){
                            var custID = createIdRecord(id);
                        } else {
                            throw 'ID already exists '+ id;
                        }
                    }

                    var form = serverWidget.createForm({
                        title: 'Form Submitted'
                    });

                    var displayField = form.addField({
                        id: 'custpage_my_display',
                        type: serverWidget.FieldType.LONGTEXT,
                        label: 'Display Data'
                    });
                    displayField.defaultValue = "ID: " + id;

                    context.response.writePage(form);
                }
                catch(e){
                    log.error(func, JSON.stringify(e));
                }
            }
        }

        return {
          onRequest: onRequest
        };
    });

标签: suitescriptsuitescript2.0

解决方案


除了“提交”按钮之外,您不需要其他按钮。但是您确实需要将套件与客户端脚本链接:

form.clientScriptFileId = client_script_file_id;

您的客户端脚本不需要部署,您只需要在文件柜上使用它。用户单击提交按钮后,将在链接到套件的客户端脚本上触发 saveRecord 事件。

function saveRecord(context) {
//put your duplicate id logic validation here
  if(you want to proceed){
     return true;
  }else {
     alert('duplicate id'); // or display an hidden field with any error message
     return false;
  }
}

推荐阅读