How do you add js to a record edit view in Sugar7?

before, I will wrote some js files and include them in editviewdef.php,and everything was fine,like 'displayParams' , $.ajax to get values .However ,when I fall in Sugar7,it seems that even I want to alert('hello') is a problem.

Now, I hope someone kind could give an method to include js file . specifically,in account ,I can use formula to make field A = filed B+ field C, but the field A can not be edit for using formula.I would use js to calculate it in th pass ,and certainly it could be edit .Now, how should I do?

Thanks


Parents
  • If you customize the record view (clients/base/views/record) HandleBars for example and include a JavaScript tag there for example. A better solution is to extend the controller and try to add all module specific clientside functionality there.

    But for the issue you try to solve: you want that the value of field A is equal to B+C. This can be done by creating a formula for Field A, but you do not want to do this because it makes the field not editable anymore (so Field A is some sort of default value?) ? In that case then maybe a Logic Hook might be a better solution, for example a before_Save logic hook which sets the appropriate value for Field A before it is saved.
  • make a record.js file in
    and put this code

    ({
        extendsFrom: 'RecordView',
        initialize: function (options) {
            app.view.invokeParent(this, {type: 'view', name: 'record', method: 'initialize', args:[options]});
            alert ("done");
            alert (this.model.get('id'));// this will show the current record id i wrote this line to make u understand how to get any field value
           
        }

    })

  • Write the following code in record.js file

    ({
            extendsFrom: 'RecordView',     
            initialize: function (options) {
            app.view.invokeParent(this, {type: 'view', name: 'record', method: 'initialize', args:[options]});
         
            //Onchange of B and C fields call validation method
            this.model.on("change:FieldB",this.validation, this);
            this.model.on("change:FieldC",this.validation, this); 

    },

    validation:function() {
           //Write your logic here
    },

    })


  • If I'm not wrong , I think

    this.model.save();
    call after
    this.model.set('field_a_c',field_a_c);

    will save the value everytime I open the record ,and it will over write the value I saved before which meaning I can't save my custom value.


    I think an example will explain my meaning more clear.
    For example, when edit or create a record ,if I type A = 'aa' and B = 'bb' ,the C will auto be 'aabb'.After I edit the value of C to 'cc' and save,I can find the value of C is 'cc' already in database.However, when I open the record again,it does will show 'aabb' in field C,not the 'cc'.That's why I hope to know how to calculate the value only when I edit the value of A and B,not the time I open the record.

    Waiting for your reply.
Reply
  • If I'm not wrong , I think

    this.model.save();
    call after
    this.model.set('field_a_c',field_a_c);

    will save the value everytime I open the record ,and it will over write the value I saved before which meaning I can't save my custom value.


    I think an example will explain my meaning more clear.
    For example, when edit or create a record ,if I type A = 'aa' and B = 'bb' ,the C will auto be 'aabb'.After I edit the value of C to 'cc' and save,I can find the value of C is 'cc' already in database.However, when I open the record again,it does will show 'aabb' in field C,not the 'cc'.That's why I hope to know how to calculate the value only when I edit the value of A and B,not the time I open the record.

    Waiting for your reply.
Children
No Data