Modify return data from related field

Hi,

I'm working on Sugar 7.5 Enterprise, i have a relation between accounts and oportunitties and i need to copy a content field from the accounts module and paste into a field in the opportunitties module, but keeping this field editable.

In SugarCE 6.5, i can modify the open_popup function in the related field from the opportunities module and add the required field:

open_popup(
  "Accounts", 
  600, 
  400, 
  "", 
  true, 
  false, 
  {"call_back_function":"set_return","form_name":"EditView","field_to_name_array":{"id":"account_id","name":"account_name", "my_account_field":"my_opportunity_field" }}, 
  "single", 
  true
);

But in Sugar 7.5 this process it's very different, i extends a record.js in the opportunities module, but i don't know how to modify the related field functionality.

Thanks for the help.

Parents
  • If I understand correctly you have an Account on an Opportunity.

    When you select an Account on the Opportunity you want to copy field my_account_field from Accounts to field my_opportunity_field on Opportunity but the user should be able to change the Opportunity field from that default if they wish to do so.

    I am assuming Account is a Relate Field on Opportunity so the Opportunity will have an account_id

    I would do this as an on.change on the account_id field on the opportunity in the Opportunity's record and create-actions view and check that the opportunity field is not already filled (you don't want to override what it was set to on an existing opportunity when it's edited).

    You will need something like:

    In custom/modules/Opportunities/clients/base/view/create-actions/create-actions.js

    and similar in  custom/modules/Opportunities/clients/base/view/record/record.js

    ({

      extendsFrom: 'CreateActionsView',

     

      initialize: function(options){

        this._super('initialize', [options]);

        this.model.on('change:account_id', this.populateMyOppField, this);

      },

    populateMyOppField: function(){

       //if the account field on the opportunity is not empty (we just changed it and it could have been deleted)

    if(!_.isEmpty(this.model.get('account_id')) {

         //if the field on the opp is not already set, you'll need this more on edit than create not to override prior values

          if(_.isEmpty(this.model.get('my_opportunity_field'))){

             //get the account bean

             var accountBean = app.data.createBean('Accounts', {id: account_id});

             requestAccount = accountBean.fetch();

             requestAccount.xhr.done(function(){

               //once you have the Account information you can copy my_account_field to my_opportunity_field

               this.model.set('my_opportunity_field', accountBean.get('my_account_field'));

             }

         }

       }

    },

    });

    Note the code is just intended to give you an idea, it is not tested, it's written off the cuff, so there could be syntax errors.

    There may be a better/easier way of doing this.

    HTH

    FrancescaS

  • Thanks a lot!! Worked perfectly . But i have a question, why extends the CreateActionsView? this manages the events in the record view?

Reply Children
  • In custom/modules/Opportunities/clients/base/view/record/record.js you extend record view and you need this for when you edit an existing record.

    You need the create-actions for when you create an Opportunity from scratch, a new one.

    So you need two pieces of code which are almost identical, the one above in

    custom/modules/Opportunities/clients/base/view/create-actions/create-actions.js

    and in

    custom/modules/Opportunities/clients/base/view/record/record.js

    ({

      extendsFrom: 'RecordView',

     

      initialize: function(options){

        this._super('initialize', [options]);

        this.model.on('change:account_id', this.populateMyOppField, this);

      },

    populateMyOppField: function(){

       //if the account field on the opportunity is not empty (we just changed it and it could have been deleted)

    if(!_.isEmpty(this.model.get('account_id')) {

         //if the field on the opp is not already set, you'll need this more on edit than create not to override prior values

          if(_.isEmpty(this.model.get('my_opportunity_field'))){

             //get the account bean

             var accountBean = app.data.createBean('Accounts', {id: account_id});

             requestAccount = accountBean.fetch();

             requestAccount.xhr.done(function(){

               //once you have the Account information you can copy my_account_field to my_opportunity_field

               this.model.set('my_opportunity_field', accountBean.get('my_account_field'));

             }

         }

       }

    },

    });

    HTH

    FrancescaS

  • Hello ,

    Francesca , do you know if this would be possible to do in reverse ? I guess calling model.set on the account.bean is not possible ? So would have the update it with an app.api.call ? app.api.call('update', app.api.buildURL('Accounts/'+accountid), {"accountfield" : "value" }, callbacks...  ?