logic hooks popup alert

Hello,

Please help me solution to display popup alert by logic hooks

This logic hooks after save of Case, when Case set to Closed but Task in subpanel is not Completed, the logic hooks will popup alert and restrict save.

Thanks 

  • So, the logic hooks approach isn't going to work. For some reason I thought you were using Sugar 6/a BWC module. It used to work in Sugar 6, but seeing as you're not using a BWC module, there's no way to communicate back to front end interface using a logic hook in the manner you describe.

    The way to solve your problem is using javascript. The sugarcrm examples of using validation are here: http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.6/UI_Model/Views/Examples/Adding_Field… 

    The way to do it is to add a file in /custom/modules/Cases/clients/base/views/record/record.js

    The file should have this in it:

    ({
         extendsFrom: 'RecordView',
        initialize: function (options) {
            this._super('initialize', [options]);
              
              //add the validation task
            this.model.addValidationTask('validate_cases', _.bind(this._doValidateCases, this));
        },

        _doValidateCases: function (fields, errors, callback) {
            var case = this.model;
            if(case.attributes.status == 'Closed'){
                        var tasks = case.getRelatedCollection('case_tasks');
                        
                        //fetch the tasks
                        var fetchTasks = tasks.fetch({relate: true});
                        
                        //wait until the fetch finishes before executing the next bit
                        fetchAllocations.xhr.done(function () {
                             
                             tasksJSON = tasks.toJSON();
                             //loop through all the tasks
                             _.each(tasksJSON, function (task) {
                                  
                                  //if one of the tasks isn't completed, throw an error message, and add a validation problem
                                  if (task.status != 'Completed'){
                                       app.alert.show('message-id', {
                                            level: 'error',
                                            messages: 'The  Case has a related task that is not completed and therefore the Task cannot be closed',
                                            autoClose: true
                                       });
                                       errors['status'] = errors['status'] || {};
                                       errors['status'].required = true;
                                  }
                             });
                             
                             //proceed to the callbacks
                             callback(null, fields, errors);
                        });
                }                
            } else {
                   //proceed to the callbacks
                   callback(null, fields, errors);
            }
            
            
            


        },
    })

    BTW This will not prevent save on mobile. You'll need to do a repair and rebuild and delete your local cache (try it from incognito mode) to test it. Also, I've written all this from memory, so there might be a bug in the code. Let me know if you have any problems.

  • Hi Alan Apter,

    I updated record.js and then repari and rebuild, but my sugar is loading, it can not display home

    the apache is not error.

    Thank you very much

  • Sorry, made a few mistakes with the code - I wrote it all from memory as I don't have access to my developer environment. I've fixed the mistakes I can see, here is my updated code, try this. If it doesn't work, open up the javascript console (ctrl-shift-i, then click on console) and see if there are any errors.

    ({
        extendsFrom: 'RecordView',
        initialize: function (options) {
            this._super('initialize', [options]);
            //add the validation task
            this.model.addValidationTask('validate_cases', _.bind(this._doValidateCases, this));
        },
        _doValidateCases: function (fields, errors, callback) {
            var singleCase = this.model;
            if (singleCase.attributes.status == 'Closed') {
                var tasks = singleCase.getRelatedCollection('case_tasks');
                //fetch the tasks
                var fetchTasks = tasks.fetch({relate: true});
                //wait until the fetch finishes before executing the next bit
                fetchTasks.xhr.done(function () {

                    tasksJSON = tasks.toJSON();
                    //loop through all the tasks
                    _.each(tasksJSON, function (task) {

                        //if one of the tasks isn't completed, throw an error message, and add a validation problem
                        if (task.status != 'Completed') {
                            app.alert.show('message-id', {
                                level: 'error',
                                messages: 'The  Case has a related task that is not completed and therefore the Task cannot be closed',
                                autoClose: true
                            });
                            errors['status'] = errors['status'] || {};
                            errors['status'].required = true;
                        }
                    });
                    //proceed to the callbacks
                    callback(null, fields, errors);
                });
            } else {
    //proceed to the callbacks
                callback(null, fields, errors);
            }
        }
    })
  • Hi Alan Apter,

    Thank you very much

    I have some error from javascript console, I am debugging, I'll feedback soon

    Thanks

  • Hi Alan Apter

    I received error from console when I save Case set to Closed

    1. {error: "not_found", error_message: "Could not find a relationship named: case_tasks"}
      1. error:"not_found"
      2. error_message:"Could not find a relationship named: case_tasks"
  • Thanks for the logging information, it has helped me with the issue. It's a bit of an edge case, the problem appears to be with:

                var tasks = singleCase.getRelatedCollection('case_tasks');

    The relationship is called case_tasks in studio, but you fetch the related collection just using 'tasks'. It's annoyingly inconsistent, but so is life. Here is the fixed code:

    ({
        extendsFrom: 'RecordView',
        initialize: function (options) {
            this._super('initialize', [options]);
            //add the validation task
            this.model.addValidationTask('validate_cases', _.bind(this._doValidateCases, this));
        },
        _doValidateCases: function (fields, errors, callback) {
            var singleCase = this.model;
            if (singleCase.attributes.status == 'Closed') {
                var tasks = singleCase.getRelatedCollection('tasks');
                //fetch the tasks
                var fetchTasks = tasks.fetch({relate: true});
                //wait until the fetch finishes before executing the next bit
                fetchTasks.xhr.done(function () {

                    tasksJSON = tasks.toJSON();
                    //loop through all the tasks
                    _.each(tasksJSON, function (task) {

                        //if one of the tasks isn't completed, throw an error message, and add a validation problem
                        if (task.status != 'Completed') {
                            app.alert.show('message-id', {
                                level: 'error',
                                messages: 'The  Case has a related task that is not completed and therefore the Task cannot be closed',
                                autoClose: true
                            });
                            errors['status'] = errors['status'] || {};
                            errors['status'].required = true;
                        }
                    });
                    //proceed to the callbacks
                    callback(null, fields, errors);
                });
            } else {
    //proceed to the callbacks
                callback(null, fields, errors);
            }
        }
    })

    Let me know if this works, and if it doesn't, let me know what the console says. I'm hoping to get a development environment up soon so I can test it myself.

  • Bao Tran Hoang Did this work for you? Otherwise I'll finish setting up an environment to test it on.

  • Hi Alan Apter,

    I've just teted, It worked. You are my HERO, thank you very much Alan

  • Hi Alan Apter,

    The red alert display in sort time, how can display this alert more time

  • I'm glad I helped.

    If you want the alert to display forever, try changing 'autoClose: true' to false - that way the message will display until the user closes it.

    If you want to display it for a specific amount of time and then close it, you can check the documentation:

    http://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.7/User_Interface/Alerts/ 

    Basically you'd change autoclose to false, and then you'd add something below the alert that would close the alert after some time. E.g.:

    setTimeout(
    function() {
    app.alert.dismiss('message-id');
    }, 5000);

    That will dismiss the message after a specific amount of time.