Auto populate an enum field by condition

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shalmaxb
    Senior Member
    • Mar 2015
    • 1605

    Auto populate an enum field by condition

    Hello,
    I am creating an administration app for artist-work database. As always with Espocrm by working come more ideas, because this system is so incredible versatile.
    I have the following task to solve:

    The physical artwork is stored in certain places, which is determined by an enum field (Room 1, Room 2, Cabinet 1, Cabinet 2 and so on). In case of an artwork is sold, it doesn`t need a storage place anymore. I already got the conditions to work, that in that case, I cannot choose anyone of the physical places, but only the value "sold". Unfortunately, if the work was stored first under a room and cabinet, this value will stay in the field even if I triggered the condition, which excludes these values to choose.

    I would like to have the following:

    1. Artwork is still with the artist -> storage in room, cabinet etc. By condition not possible to choose sold.
    2. Artwork is sold -> delete the former values of room, cabinet etc. and substitute with value "sold".

    I would really prefer the enum field, as there I can show the values as label.

    Is that possible?
  • telecastg
    Active Community Member
    • Jun 2018
    • 907

    #2
    Hello shalmaxb ,

    Yes you can do this through the dynamic handler class https://docs.espocrm.com/development/dynamic-handler/

    Here's the code that we use to make changes to a WorkOrder entity enum options based on certain constrains, I think it is similar to your use case so hopefully it will show you how you can implement your solution.

    Code:
    define('custom:work-order-dynamic-handler', ['dynamic-handler'], function (Dep) {
    
        return Dep.extend({
    
        init: function () {
            this.controlFields();
            // invoke controlFields methods every time status or serviceTechId get changed
            this.recordView.listenTo(this.model, 'change:status', this.statusControlField.bind(this));
            this.recordView.listenTo(this.model, 'change:serviceTechId', this.serviceTechIdControlField.bind(this));
        },
    
        controlFields: function () {
            this.statusControlField();
            this.serviceTechIdControlField();
        },
    
        statusControlField: function () {
            // limit the choices for status if the Service Tech field is empty
            if (!this.model.get('serviceTechId')) {
                this.recordView.setFieldOptionList('status', [
                    'Unassigned',
                    'Canceled'
                ]);
            }
        },
    
        serviceTechIdControlField: function () {
            // limit the choices for status if the Service Tech field is not empty
            if (this.model.get('serviceTechId')) {
                this.recordView.setFieldOptionList('status', [
                    'Assigned',
                    'Completed',
                    'Canceled'
                ]);
                // if a Service Tech has been selected, and the status was Unassigned, change the status to "Assigned"
                if(this.model.get('status') === 'Unassigned') {
                    this.model.set('status','Assigned');
                    // persist the change
                    this.model.save();
                    // refresh the display
                    this.model.fetch();
                }
            } else {
                // if the Service Tech is erased and the status is Assigned, change it to "Unassigned"
                if(this.model.get('status') === 'Assigned') {
                    this.model.set('status','Unassigned');
                    // persist the change
                    this.model.save();
                    // refresh the display
                    this.model.fetch();
                }
            }
        }
    
    });
    });

    Comment

    Working...