Announcement

Collapse
No announcement yet.

Get Attributes from Parent Entity and fill the fields before saving

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Get Attributes from Parent Entity and fill the fields before saving

    Hi,
    With Sales Pack installed, when I create a Quote from the quotes panel linked to the Opportunity, the creation form inherits the Account, the Amount and the name of the Opportunity. What is the method to do the same with another fields shared between both entities?. I want to show the value of this fields before saving. I tried to do it with a hook, but this method only works after saving the new quote. I guess that I have to extend the view with some *.js file to do this but I've tried several times with no success.
    Is there any document o paper with information about it?

    Thank you in advance for your time.​

  • #2
    This is what you need

    https://docs.espocrm.com/development...teattributemap below an example contacts on account entity

    PHP Code:
    "relationshipPanels": {
            
    "contacts": {
                
    "filterList": [
                    
    "all",
                    
    "accountActive"
                
    ],
                
    "layout""listForAccount",
                
    "orderBy""name",
                
    // This is the part you need createAttributeMap 
                
    "createAttributeMap": {
                    
    "billingAddressCity""addressCity",
                    
    "billingAddressStreet""addressStreet",
                    
    "billingAddressPostalCode""addressPostalCode",
                    
    "billingAddressState""addressState",
                    
    "billingAddressCountry""addressCountry",
                    
    "id""accountId",
                    
    "name""accountName"
                
    }
            }
    }
    ​ 

    Just follow the same concept and it should work fine
    Rabii
    Web Dev

    Comment


    • #3
      Thank you rabii, but I did this in /custom/Espo/Custom/Resources/metadata/clientDefs/Opportunity.json and it doesn't work (well, I did it with assignedUserId just for testing but the attribute I want to inherit is another one. I thought to do it with this attribute would be easiest).

      Code:
      "relationshipPanels": {
        "quotes": {
          "layout": null,
          "selectPrimaryFilterName": null,
          "createAttributeMap": {
            "assignedUserId": "assignedUserId"
          }
        }
      },​
      The field Assigned User is not filled with the value of the same field in the Opportunity.

      The example you use is between two native EspoCRM entities, maybe the problem is having the Sales Pack installed?

      Thank you for your help
      Last edited by jtubee; 10-09-2024, 03:46 PM.

      Comment


      • #4
        Add new information. I found this view in client/custom/modules/sales/src/views/quote/fields/opportunity.js. I think this is where you get the attributes of the linked opportunity, so maybe I need to create and apply a custom view adding other attributes that I need to get from the opportunity. Do you agree?

        Code:
        define('sales:views/quote/fields/opportunity', ['views/fields/link'], function (Dep) {
        
        return Dep.extend({
        
        // @todo Use handler.
        getSelectFilters: function () {
        if (this.model.get('accountId')) {
        return {
        'account': {
        type: 'equals',
        attribute: 'accountId',
        value: this.model.get('accountId'),
        data: {
        type: 'is',
        nameValue: this.model.get('accountName'),
        },
        },
        };
        }
        },
        
        select: function (model) {
        Dep.prototype.select.call(this, model);
        
        if (!this.model.isNew()) {
        return;
        }
        
        Espo.Ajax.getRequest('Quote/action/getAttributesFromOpportunity', {
        opportunityId: model.id,
        }).then(attributes => {
        const attributesToSet = {};
        
        for (const item in attributes) {
        if (~['amountCurrency'].indexOf(item)) {
        continue;
        }
        
        if (~['name'].indexOf(item)) {
        if (this.model.get(item)) {
        continue;
        }
        }
        
        attributesToSet[item] = attributes[item];
        }
        
        this.model.set(attributesToSet);
        this.model.set('amountCurrency', attributes.amountCurrency, {ui: true});
        });
        },
        });
        });​

        Comment


        • #5
          Hey,

          I have tested it and it works fine. Just add this without layout and selectPrimaryFilters

          PHP Code:
          "relationshipPanels": {
            
          "quotes": {
              
          "createAttributeMap": {
                
          "assignedUserId""assignedUserId"
              
          }
            }
          }
          ​​ 

          It should work 100% but remember you have to clear the cache and rebuild the system. Also can you share all the code in /custom/Espo/Custom/Resources/metadata/clientDefs/Opportunity.json

          NB: if the field is not available on the UI it won't work. you need to have the fields on the editQuick modal when creating a new quote from opportunity. Also this works only when you create a quote from the quotes panel on the opportunity detail view.
          Rabii
          Web Dev

          Comment


          • #6
            Hi rabii and thank you for your time. Sure it works 100% with account and contacts linked. I tried to do it with the field emailAddress and the new contact inherits the value of the field with the same name in account. The code I used in Account.json is this:

            Code:
            "relationshipPanels": {
              "contacts": {
                "createAttributeMap": {
                  "emailAddress": "emailAddress"
                }
              }
            },
            But, It doesn't work between Opportunity and Quote. I tried to use the same code in Opportunity.json but no way. (I tried with assignedUser but it doesn't work with any field)

            Code:
            "relationshipPanels": {
              "quotes": {
                "createAttributeMap": {
                  "assignedUser": "assignedUser"
                }
              }
            },


            I've found out there is a diference between both scenarios in the estructure of the default json file. While in the default Account.json the estructure is like the custom file I've created, the estructure of the default Opportunity.json includes a "creationAction" and a "view":

            Code:
            "relationshipPanels": {
              "quotes": {
                "createAction": "createRelatedQuote",
                "view": "sales:views/opportunity/record/panels/quotes",
                "layout": "listForAccount",
                "selectDisabled": true,
                "unlinkDisabled": true
            },​
            It seems this code uses a different method to get the attributes of the parentEntity.

            Also I've tried to add code with the fields mapping directly in the default Opportinity.json but It doesn't work neither.

            I've read several post with the same issus but always dealing with native entities. I haven't been able to find information to do this with entities created by the Sales Pack.

            Comment


            • #7
              You are right. There is a logic which retrieve attributes from the opportunity hence why createAttributeMap doesn't work (i guess it is a race). You can still use hooks which will execute in the backend but still the data will be saved in db. Another way is to customise the whole thing which will be difficult as you will need to override the controller method and then service logic.
              Rabii
              Web Dev

              Comment

              Working...
              X