Set up a Naming Rule Script

When new objects are created, the object names do not have to be entered manually. You can define rules to automatically name these objects. Customised automatic naming allows users to reduce the amount of time in renaming objects from the default naming structure to a more meaningful name. Ideally the system should allocate the next name of an object, with the users only needing to confirm the name. This makes following correct business rules easy and produces consistency in naming.

A simple naming change would be to alter the mask of the naming rule or the latest used counter value, and a more complex naming change would be to use a script to determine the next name for an object, in accordance with the individual customer's requirements. See Set up Naming Rules.

Example: Naming Script for Trains

The following script generates a name for trains using the format R-YY-####, where:

  • R= Name prefix
  • YY = 2-digit year
  • #### = 4-digit number with leading zeros, starting at 1

For example, R-15-0001 is the name for the first train generated in the year 2015.

Copy
using System;
using Mincom.MineMarket.DAL;
using Mincom.MineMarket;
using System.Collections;
using Mincom.MineMarket.Server;
/* 
This file includes standard naming scripts for the following objects:
Version 1.0
Date last Modified : 16/07/2015 to compile and run with V4.7 MineMarket
Modified by : Datamine
Train TrainNaming : Using the following format R-YY-####
*/
/// <summary>
/// This is simply an example or template class and should not be used
/// </summary>
public class TrainNaming : INameGenerator
{
    public TrainNaming() { }
    /// <summary>
    /// Returns a list of strings describing the parameters required. Used for testing purposes mainly.
    /// This method is part of the required implementation of the INameGenerator interface
    /// </summary>
    /// <returns></returns>
    public string[] GetParameterMap()
    {
        return null;
    }
    /// <summary>
    /// Generates and returns a name based on a list of strings described in the GetParameterMap() function.
    ///    Mainly called by the script test form.
    /// This method is part of the required implementation of the INameGenerator interface
    /// </summary>
    /// <param name="paramList">The list of strings used to generate a name for an object in the same order that the GetName() function described them</param>
    public string GetName(string[] paramList)
    {
        return null;    
    }
    /// <summary>
    /// Generates the name of the "obj" BTBusinessObject. This method is called by the NameGenerator class.
    /// This method is part of the required implementation of the INameGenerator interface
    /// </summary>
    /// <param name="obj">The business object for which a name is required</param>
    /// <param name="info">The validation info object which may be null and allows validation messages to be returned if conditions are not met to generate a valid name</param>
    /// <param name="nameGenerated">Returns whether the name was generated by this script</param>
    public string GetName(BTNamedBusinessObject obj, ValidationInfo info, out ScriptResult scriptResult)
    {
        Train train = obj as Train;
        if (train != null && train.Route != null)
        {
            scriptResult = ScriptResult.NameGenerated;
            string trainName = "R-" + DateTime.Now.ToString("yy") + "-" ;

            ArrayList temp;
            Criteria crit1 = new Criteria(typeof(Train), "Name", QueryOperator.Like, trainName + "%");
            QueryBuilder qryb = new QueryBuilder(crit1,null);
            qryb.OrderByList.Add(new OrderByClause(typeof(Train), "Name", false));

            temp = Train.GetFactory().GetByCriteria(qryb);
            int number = 0;
            if (temp.Count > 0)
            {
                string nameTemp = ((Train)temp[0]).Name;
                int.TryParse(nameTemp.Substring(trainName.Length, 
                nameTemp.Length - trainName.Length),out number);
            }

            number++;
            trainName = trainName + number.ToString("0000");
            return trainName;
        }
        else if (info != null && train != null && train.Route == null
        {
            info.AddError(obj, "Train Route cannot be null");
        }

        scriptResult = ScriptResult.NameNotGenerated;
        return null;
    }
}

Security Note: You need the Allow script maintenance and Allow the user to maintain naming script security right in the Scripting user group security rights group for this activity.

Activity Steps

  1. Open the Named Object Generator (script editor).

    The Scripting Ribbon Tab displays.

  2. Edit the C# script.
  3. Append the public class code after any other public classes in the naming rule code.
  4. Ensure that script references are selected:
    1. Note the DLLs that are referenced in the script. The DLLs are listed at the top of the script, after the word "using".
    2. Select the References tab.
    3. Select the required DLLs.
  5. Click the Build icon on the Scripting ribbon tab to compile the script.
  6. Click the Save icon on the Scripting ribbon tab.
  7. To apply the naming rule script to the object:
    1. Open the Naming Rules.
    2. Locate and select the item to be changed.
    3. Check Use Script.

      Whether to use script to manage the object name generation.

    4. Select the Script to be used.

      Select from all naming scripts coded on the Named Object Generator (script editor) screen.

    5. If object names generated using this script are not to be edited, check Disallow Name Editing.

      If checked, when a script is run, the name generated cannot be edited. The renaming of the object will be disabled on all screens (for example, via right-click rename, or click to edit). Any nodes with a Rename right-click option have that option removed when name editing is not allowed.

      Example: If TruckDespatch has a naming script, when a truck despatch name is generated, if a rename is attempted for it on all screens, a warning is given that this cannot be done.

    6. Click Save.