J.J.
J.J.

Reputation: 1118

How to display client-side alerts in Suitelet post context?

How can I display errors when entering data into this suitelet form, so that I could display messages such as the one thrown below regarding duplicate ID records? Does this need to be done in a client script, and does the suitelet need to have a button linked with the client script that carries out record creation and then redirects to another suitelet that displays the form info? It seems like there must be an easier way to go about displaying errors client side without needing to redirect twice.

Suitelet example:

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
        };
    });

Upvotes: 0

Views: 2659

Answers (1)

Emerson Minero
Emerson Minero

Reputation: 608

You do not need another button besides the 'submit' button. But you do need to link your suitelet with a client script:

form.clientScriptFileId = client_script_file_id;

Your client script does not need to be deployed, you just need it on the file cabinet. Once the user click on the submit button the saveRecord event is triggered on the client script linked to the suitelet.

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;
  }
}

Upvotes: 2

Related Questions