1. Status change of a record by means of buttons

    Article: AN0001922Updated: 03.10.2018

    Some application enable to define rules for record status changes. In most cases there is a matrix defining from which status the record can move to another status and who can perform such a change. We can achieve such a behaviour in ObjectGears also easily.

    Our goal is to enable a change by means of buttons that are provided to users based on their role and current record status. We will replace selection of records on comboboxes or in a search form by this solution. This will simplify and accelerate users` work.

    Example: We can see a record with status Prepared (top right) in the below screen and the toolbar provides users with two buttons Cancel and Submit.

    We will achieve this behaviour in several steps:

    1. Think out which statuses our entity can achieve and what is the logic of moving between them
    2. Create class with statuses
    3. Create column Status referrering to the class with statuses
    4. Create class with allowed moves between statuses
    5. Create buttons for status changes
    6. Setup up column Status as read-only

    Overview of statuses and logic of moves between them

     

    Class with statuses

    Particular statuses can be recorded in a separate class. However, if we want to create more entities with various statuses (e.g. requests, orders, goods receipts, invoices etc.), we can use also a single class for all these statuses and distinguish records by the related class code.

     Definition of columns in the class.

    Column Status referring to the class with statuses

     We will define which records will be provided in our class in the column Status in the Filter setting in the Reference tab.

    Class with allowed moves between statuses

     Definition of allowed moves between status will be done in a class that will refer to the class Status in two columns. We can have several records for each Status that will differ in the column Next status. Each record can contain also Button code (see below), that shall be displayed.

     Definition of columns in the class.

    Buttons for status change

    We will create buttons of type Script in the cllass and provide them with codes stated in the class for status change (see above).

    We will define roles for each button depending who is allowed to perform such a change. We have to also ensure that there will be only such buttons displayed that will move the record to the allowed next statuses. This will be provided by a script for record detail defined in the class.

    function OnPreRender()
    {
      if ( OGDataRowId != null)
      {
        //hide buttons except those stated for the given status in the class status_flow
        //read records from "Status flow - status_flow"
        var f = OG.DataRow.GetDataRowFilter(OGModel.ClassDefs['status_flow'].Id);
        f['active'] = true;
        f['status'] = status[0].Id;
        f.Filter_OnlyActive = true;
        var drl = OG.DataRow.GetDataByFilter(f);

        //hide columns
        OGControlOperations['cancel'].Visible = false;
        OGControlOperations['submit'].Visible = false;
        OGControlOperations['withdraw'].Visible = false;
        OGControlOperations['reject'].Visible = false;
        OGControlOperations['create_po'].Visible = false;
        OGControlOperations['confirm_receipt'].Visible = false;

        //display columns according to status
        if ( drl != null && drl.Count > 0)
        {
          for( var i = 0; i < drl.Count; ++i)
          {
            var co = OGControlOperations[drl[i]['control']];
            if ( co != null)
            {
             co.Visible = true;
            }
          }
        }
      }
    }

     The change of the Status itself will be performed by a script defined in the button.

    Example: Button Submit

    /*Change the status of the request to 'submitted' and save the record*/

    var clStatus = OGModel.ClassDefs['status'];
    var f = OG.DataRow.GetDataRowFilter(clStatus.Id);
    f['code'] = 'submitted';
    f['related_class_code'] = '"' + OGDataParent.Code + '"';

    var drl = OG.DataRow.GetDataByFilter(f);
    if ( drl.Count == 1)
    {
        var ht = OG.CreateHashtable();
        ht['status'] = drl[0].Id;

        OGDataDetailForm.Save(false, ht);
    }

    Note: Above script is using command f['related_class_code'] = '"' + OGDataParent.Code + '"';. Class code according to which we will be filtering is inserted into double quotations in order exact match is ensured. Otherwise operator LIKE would be used and there could be returned also records with a similar class code. See object DataRowFilter and text filtering.

    Column Status as read-only

    We have to enable editing by record change in the definition of column Status, because status will be changed by buttons. However, we will disable its editing by a user in the control in order user can use button only. This will be ensured by a script for record detail defined in the class.

    function OnAfterLoadColumns()
    {
      OGColumns['status'].ReadOnly = true;
    }

×