Announcement

Collapse
No announcement yet.

Custom Entity pass query parameter on create

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

  • Custom Entity pass query parameter on create

    Hello,

    I have a custom entity MyCustomEntity; this entity is available in a portal.

    Front-end user can create a new record of MyCustomEntity by visiting :

    {portalUrl}/#MyCustomEntity/create


    I would like to pass a query variable to this controller so that a field on the 'create' form gets automatically set on the front-end

    For example, {portalUrl}/#MyCustomEntity/create?role=operator

    Visiting the above URL would require the portal user to login and, after, it would show the create new record form for MyCustomEntity with the 'role' field filled in with the value 'operator'


  • #2
    Hi,

    Passed params are available in the controller action. You can define a custom frontend controller extended from controllers/record.js. Method 'actionView'.

    Comment


    • #3
      Originally posted by yuri View Post
      Hi,

      Passed params are available in the controller action. You can define a custom frontend controller extended from controllers/record.js. Method 'actionView'.
      Thank you for this. I was able to get this working by setting up custom frontend controller as you suggest. The following code sets an attribute called myFieldName to the query variable passed to this controller. I see the field is set in GUI when visiting the url:

      {portalUrl}/#MyCustomEntity/create?myFieldName='abc'


      Code:
      define('custom:controllers/my-custom-entity', ['controllers/record'], function (Dep) {
      
      actionCreate: function (options) {
      
      options = options || {};
      options.attributes = options.attributes || {};​
      
      if (options && options.myFieldName) {
          options.attributes.myFieldName = options.myFieldName;
      }
      Dep.prototype.actionCreate.call(this, options);
      
      }
      });
      Follow-up question, the above code worked because I set options.attributes.{fieldName} and {fieldName} already existed in the entity. What if I just want to pass some query variable to this controller and be able to access it as a property of recordView ? For example, When viewing the entity described below, I want to write some custom code in a dynamic handler so it starts from a specific 'tab' in the detail recordView. I can't seem to figure out how to set the query variable to something I can access from this.recordView in the dynamic handler.


      Example Scenario:

      {portalUrl}/#MyCustomEntity/view/{entityId}?startingTabName='abc'
      Code:
      define('custom:controllers/my-custom-entity', ['controllers/record'], function (Dep) {
      
      actionView: function (options) {
      
      options = options || {};
      options.attributes = options.attributes || {};​
      
      if (options && options.startingTabName) {
          options.startingTabName = options.startingTabName; ?? what should I name this or set to have it available in recordView ?
      }
      Dep.prototype.actionView.call(this, options);
      
      }
      });


      Lets say i have a custom dynamic handler
      Code:
      define('custom:my-custom-entity-dynamic-handler', 'dynamic-handler', function (Dep) {
      
      return Dep.extend({
      
      // called on initialization
      init: function () {
      
      this.recordView.listenTo(
      this.recordView,
      'after:render',
      this.afterRender.bind(this)
      );
      
      },
      
      // put things that depending on rendering inside here
      afterRender: function() {
      console.log('this',this);
      console.log('recordView',this.recordView);
      // HOW DO I ACCESS startingTabName
      },
      
      
      });
      });​
      Last edited by czcpf; 02-15-2024, 02:55 PM.

      Comment


      • #4
        There are in the options of the parent view of the recordView. Try this.recordView.getParentView().options.

        Comment


        • czcpf
          czcpf commented
          Editing a comment
          Thank you! Worked like a charm. In the second case there is no need for a custom controller. the query variable(s) will be located in this.recordView.getParentView().options.params. I suppose even in the first use case scenario; if properly constructed one could even pass a query variable like ?attributes={some properly serialized Object holding variable names and variable values} instead of creating a custom controller.. Not sure if that is possible.

      • #5
        For anyone wanting to use a query variable to set the starting middleTab of a view, here is a complete dynamic handler example:

        visiting the URL like {espocrmUrl}/{#MyCustomEntity}/view/{id}?startingMiddlePanelName=panel-1 for example would start the record view with the '2nd' panel selected initially instead of the first. I am using external url's like this one to guide user directly to certain parts of GUI based on some other logic.

        client/custom/src/my-custom-entity-dynamic-handler

        Code:
        define('custom:{my-custom-entity-dynamic-handler}', 'dynamic-handler', function (Dep) {
        
        return Dep.extend({
        
        // called on initialization
        init: function () {
        
        this.recordView.listenTo(
        this.recordView,
        'after:render',
        this.afterRender.bind(this)
        );
        
        },
        
        // put things that depending on rendering inside here
        afterRender: function() {
        
        this.setStartingTab();
        },
        
        setStartingTab: function() {
        
        //check if queryVar is set
        if( ('startingMiddlePanelName' in this.recordView.getParentView().options.params ) ){
        
        let panelName = this.recordView.getParentView().options.params['startingMiddlePanelName'];
        
        // check if panel name is in middlePanelDefs
        if( panelName in this.recordView.middlePanelDefs ) {
        let tabNumber = this.recordView.middlePanelDefs[panelName].tabNumber || null; //get tab number from tab name
        this.recordView.showPanel(panelName); //show the panel in case it is hidden
        this.recordView.selectTab(tabNumber); //select the starting tab
        }
        }
        },
        
        });
        });​

        custom/Espo/Custom/Resources/ClientDefs/MyCustomEntity.json
        Code:
        ...
        "dynamicHandlerList": [
        "__APPEND__",
        "custom:my-custom-entity-dynamic-handler"
        ],​

        Comment


        • czcpf
          czcpf commented
          Editing a comment
          What do you mean view setup? Do you mean creating a custom detail view ?

          I like dynamic handler because it gives me more flexibility and I started using those first

        • rabii
          rabii commented
          Editing a comment
          this is what i mean https://docs.espocrm.com/development...etup-handlers/

        • czcpf
          czcpf commented
          Editing a comment
          Nice. Hadn’t seen or considered that. Thanks for sharing.
      Working...
      X