Inherit a linked field value from another linked field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jtubee
    Junior Member
    • Jun 2024
    • 21

    Inherit a linked field value from another linked field

    Hi,

    I need some help with this...

    I've created a new entity called PROJECTS with two one-to-many relationships, one with the entity OPPORTUNITY and another with the entity QUOTE (I have the Sales Pack installed). I need to inherit the value of the field "project" from the OPPORTUNITY in the field with the same name in the form QUOTE when I create a new QUOTE and select the OPPORTUNITY.
    I need to do that in the moment I select the OPPORTUNITY as it does with the field ACCOUNT on default way, not after save the form.

    I've tried to create a new view in client/custom/src/views/quote/fields/opportunity.js, and register it in custom/Espo/Custom/Resources/metadata/entityDefs/Quote.json, but it doesn't work so I'm doing something wrong.

    Thank you in advance for your help.

    Code:
    // file: client/custom/src/views/quote/fields/opportunity.js
    
    define('custom:views/quote/fields/opportunity', ['views/fields/link'], function (Dep) {
    
    return Dep.extend({
    
    setup: function () {
    Dep.prototype.setup.call(this);
    
    // Listen for changes to the opportunity field
    this.listenTo(this.model, 'change:opportunityId', this.handleOpportunityChange);
    },
    
    handleOpportunityChange: function () {
    var opportunityId = this.model.get('opportunityId');
    
    if (opportunityId) {
    var self = this;
    
    // Fetch the selected opportunity data
    this.ajaxGetRequest('Opportunity/' + opportunityId).then(function (data) {
    if (data && data.cCprojectId) {
    // Set the project link field in the QUOTE entity with the value from OPPORTUNITY
    self.model.set('cCprojectId', data.cCprojectId);
    self.model.set('cCprojectName', data.cCprojectName); // Assuming the project name field is also needed
    }
    });
    } else {
    // Clear the project link field if no opportunity is selected
    self.model.set('cCprojectId', '');
    self.model.set('cCprojectName', '');
    }
    },
    
    ajaxGetRequest: function (url) {
    return new Promise(function (resolve, reject) {
    Espo.Ajax.getRequest(url).done(function (response) {
    resolve(response);
    }).fail(function () {
    reject();
    });
    });
    }
    
    });
    });​

    Code:
    {
    "clientDefs": {
    "Quote": {
    "fields": {
    "opportunity": {
    "view": "custom:views/quote/fields/opportunity"
    }
    }
    }
    }
    }
    ​
    EspoCRM version 8.2.5
    Sales Pack 2.3.4
  • jtubee
    Junior Member
    • Jun 2024
    • 21

    #2
    Is it possible to do the same thing with an Script Formula in the entity Quote?

    Comment

    Working...