Auto populate field values based on a link entity selected

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • telecastg
    Active Community Member
    • Jun 2018
    • 907

    Auto populate field values based on a link entity selected

    Hi,

    s it possible to auto populate values in an entity detail view based on the value of a selected linked entity ?.

    For example, I have a "Maintenance Request" entity that has a "telephone" field and a link to "Contact", so when I am entering a new "Maintenance Request" I would like the "Maintenance Request" "telephone" value to be automatically filled by the telephone associated with the "Contact" and allow the user to continue filling all fields for "Maintenance Request" manually.

    Thanks in advance for your response
  • telecastg
    Active Community Member
    • Jun 2018
    • 907

    #2
    Found the solution, quite simple:

    Go to Administration >> Entity Manager >> "Maintenance Request" >> Fields and delete my current field "Telephone"

    Click on the "Add Field" button and create new field, type "foreign", then enter:
    "name" : "customerTelephone"
    "label" : "Telephone"
    "link" : "Contact"
    "field" : "Phone Number"

    Save the field, clear cache and rebuild.


    Comment

    • item
      Active Community Member
      • Mar 2017
      • 1476

      #3
      Hello telecastg,
      i have never use foreign type field.. .but espoCRM give many solution
      here one i have find :
      so when in meeting, user select a "parent" .. place field is autofilled by parent entity value.

      PHP Code:
      define('custom:views/meeting/detail', ['crm:views/meeting/detail'],  function (Dep) {
          return Dep.extend({
              setup: function () {
                  Dep.prototype.setup.call(this);
                  this.listenTo(this.model, 'change:parentId', function () {
                      if (this.model.get('parentId') && this.model.get('parentType') =='Patient' && this.model.hasChanged('parentId') ){
                              this.ajaxGetRequest('Patient/' + this.model.get('parentId')).then(function (patient) {
                                 this.model.set('place', patient['addressStreet'] +' ' + patient['addressPostalCode'] + ' ' + patient['addressCity'] );
                          }.bind(this));
                      }
                  }, this);
              },
              afterRender: function() {
                  Dep.prototype.afterRender.call(this);
                  //console.log('custom:views/meeting/record/detail' + 'AFTER RENDER');
                  // Custom code to be invoked right after rendering, when DOM is available.
                 // this.$el.find('label[data-name="myField"]').addClass('hidden');
              },        
          });
      }); 
      
      If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

      Comment

      • telecastg
        Active Community Member
        • Jun 2018
        • 907

        #4
        Nice solution item ! very clean and simple way to make an Ajax call.

        I think it works great when you are auto populating fields with a "native" value (varchar, text, float, boolean, etc) because the Ajax call only returns an array of attribute (field) values.

        Here's another idea when you need to retrieve the actual model object for example to auto populate a "select" type of field or to auto populate values from inside a view of an entity that is not directly related. .

        Use case:
        Have a Contact entity linked as One to Many to entity CollectionIssue

        Have an Sms entity linked as Many to One to Contact

        Sms detail small form has field contact type "select" and field targetCell type "varchar"

        Have an option in the "Edit" dropdown at the top of the detail view and the corresponding action function on the CollectionIssue detail view that triggers a modal form to enter a new Sms and the form has 3 fields: targetCell, contact and message

        When I create a new Sms to send to a Contact related to a CollectionIssue, I want the fields targetCell and contact to be auto populated with the related Contact data.

        Solution:
        client/custom/src/views/collection-issue/detail.js
        Code:
                setupActionItems: function () {
        [COLOR=#FF8C00]// perform the setupActionItems function from the parent script[/COLOR]
                    Dep.prototype.setupActionItems.call(this);  
        [COLOR=#FFA500]// add a button to send SMS messages[/COLOR]
                    this.dropdownItemList.push({
                        name: 'sendSms',
                        label: 'Send SMS'
                    });            
                },
        
                actionSendSms: function () {
                    var contactId = this.model.attributes.contactId;
                    var scope = 'Sms';
                    var smsComposeViewName = this.getMetadata().get(['clientDefs', 'Sms', 'modalViews', 'edit']) || 'views/modals/edit';
                    var createViewOptions = {};
                    createViewOptions.scope = "Sms";
                    createViewOptions.fullFormDisabled = true;                                
                    var link = recordView.getMetadata().get(['entityDefs', 'Sms', 'links', 'contact', 'foreign']);
        [COLOR=#FF8C00]// retrieve the Contact model [/COLOR]        
                    this.getModelFactory().create('Contact').then(
                        function (foreignModel) {
                            foreignModel.id = contactId;
                            foreignModel.fetch();
                            foreignLink = foreignModel.defs['links'][link].foreign;    
        [COLOR=#FF8C00]// relate the Contact information with the new Sms entity to pre-load the Sms contact field[/COLOR]
                            createViewOptions.relate = {model: foreignModel, link: foreignLink};                
        [COLOR=#FF8C00]// render the modal form to create the message[/COLOR]
                            this.createView('quickCreate', smsComposeViewName, createViewOptions, function (view) {
        [COLOR=#FF8C00]// write here code to do whatever you want with the entered data
                                ........[/COLOR]
                            });                          
                            view.render();                    
                        }.bind(this)
                    );            
                },

        Last edited by telecastg; 01-27-2020, 07:00 AM.

        Comment

        • item
          Active Community Member
          • Mar 2017
          • 1476

          #5
          Hello telecastg,
          not understand all (my skill).. but i have find a easy way to send SMS (shortMessageText to cell phone) .. with note entity (stream under entity) .. really very simple :

          user write : @sms this is the message on the stream of contact by sample..
          and on note hook beforeSave :

          PHP Code:
              public function beforeSave(Entity $entity, array $options = array())
              {
                  $post = $entity->get('post');
          
                  if(strpos($post, "@sms") === 0 && $entity->get('type') == 'Post' && $entity->isNew()) {
                      $entityManager = $this->getEntityManager();
                      $ent = $entityManager->getEntity($entity->get('parentType'), $entity->get('parentId'));
                      if ( $ent->get('phoneNumber') ){
                          $GLOBALS['log']->warning( "@SMS " .$ent->get('phoneNumber') .substr($post, 4) );
                          $countSend = $this->sendSms($ent->get('phoneNumber'), substr($post, 4));
                          $entity->set('post', $post .' (status=' .$countSend .')');
                      }
                  }
          
              } 
          
          $this->sendSms($ent->get('phoneNumber'), substr($post, 4)); is just a function who call api of provider sms.

          Another solution i have add, is for "reminder"... i have add "sms" to enum.. and a job check reminder and send sms
          it's not perfect because i don't use template.. and the message is "hard coded" but it's possible to use basic template

          Reminder entity is really a nice solution.. i think if we can create this type of field in custom way.. that would be great.

          Regards


          If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

          Comment


          • telecastg
            telecastg commented
            Editing a comment
            Thanks item, my sms "module" is very much under development.

            I like the idea of having a custom field type "sms" but I would also like to have an "sms" entity in the database, linked to a Contact that would be easier to search or manipulate with sql

            I would also like to find a way to send sms without having to subscribe to an sms provider, but that so far will require some hardware, and I am trying to find the cheapest possible way to do this :-)

            I will post any advances.

            Saludos :-)
            Last edited by telecastg; 01-26-2020, 05:22 PM.
        • esforim
          Active Community Member
          • Jan 2020
          • 2204

          #6
          Wow this thread is full of treasure trove. Need to take some time to learn more and add into my CRM too.

          Comment

          • rinorway
            Senior Member
            • Feb 2016
            • 179

            #7
            I would consider buying the advanced pack with workflows if you need to do this a lot (autofill/send HTTP requests)

            Comment

            Working...