Field validation using JScript in OIS100/A

In this post I’ll give an example of how you can validate a field in OIS100 using JScript. The scenario is as follows: All orders with order type “E50” has to be from faclity “FC5”. How can you validate input in OIS100/A? This was the question from one of our readers. The script is pretty straightforward as you have all the data that you need to check on the panel so I created a small example to illustrate the use of OnRequesting. Please note that you could make this script better by using script arguments.

The scripts illustrates the following:

  • Cancelling navigation to the next panel
  • How to set a field value
  • How to read a field value
  • How to show a message (in the status bar or dialog depending on setting)
  • How to log to the client log file
  • How do disconnect the event handler

If the order type and the facility does not match the required condition the facility is updated and a message is displayed.

OIS100A

Below is the script:

import System;
import System.Windows;
import System.Windows.Controls;
import MForms;
import Mango.UI.Services;
import Mango.Core.Util;

package MForms.JScript {
  class FieldValidation {
    var logger : log4net.ILog = Mango.Core.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
      var controller,
    debug,
    content;
    var checkOrderType = "E50"; // Should be a parameter
    var mandatoryFacility = "FC5" // Should be a parameter

      public function Init(element : Object, args : Object, controller : Object, debug : Object) {
      debug.WriteLine("Script Initializing.");
      this.controller = controller;
      this.debug = debug;
      this.content = controller.RenderEngine.Content;

      // Attach event handlers to be able to detach event handlers and list to page down requests.
      controller.add_Requesting(OnRequesting);
    }

    public function OnRequesting(sender : Object, e : CancelRequestEventArgs) {
      try {
        LogDebug("onrequesting " + e.CommandType + " " + e.CommandValue);
        if (e.CommandType == "KEY" && e.CommandValue == "ENTER") {
          // Do not disconnect events on page down.
          var orderType = GetValue("OAORTP");
          LogDebug("Order type: " + orderType);

          if (orderType != null && orderType == checkOrderType) {
            var facilityElement = ScriptUtil.FindChild(content, "OAFACI");
            if (facilityElement != null) {
              var value = MFormsUtil.GetControlValue(facilityElement);
              LogDebug("Facility " + value);
              if (value != mandatoryFacility) {
                LogDebug("Set facilty to " + mandatoryFacility);
                facilityElement.Text = mandatoryFacility;
                e.Cancel = true;
                // Here we could show a dialog as well
                var message = "When using order type " + checkOrderType + " you must use facility " + mandatoryFacility;
                controller.RenderEngine.ShowMessage(message);
                // Return so we don't disconnect since we cancelled the request
                return
              }

            }
          }
          
        }
      } catch (ex) {
        LogDebug(ex);
      }
      controller.remove_Requesting(OnRequesting);
    }

    private function GetValue(fieldName) {
      var element = ScriptUtil.FindChild(content, fieldName);
      if (element != null) {
        var value = MFormsUtil.GetControlValue(element);
        return value;
      }
      debug.WriteLine("Could not find a field named " + fieldName + " on the current panel");
      return null;
    }

    private function LogDebug(message : String) {
      if (message != null) {
        logger.Debug(message);
        if (debug) {
          debug.WriteLine(message);
        }
      }
    }
  }
}

6 thoughts on “Field validation using JScript in OIS100/A

  1. Lode

    Hello,

    I was wondering how i can add a Listbox to a panel using Jscript. I have tried to add a listbox the same way as I would add a textbox or a button, but this does not seem to work, am i missing something?

    Kind regards

    Reply
    1. karinpb Post author

      Hi,
      There is nothing special with the listbox except that you need to give it a set of values. Perhaps you can try and set a fixed width / minwidth? Are you sure the script is running and that it is placed in a visible area on the screen?

      Reply
  2. Truus Swartjes

    I would like to use a procedure like this, but it should not go off when you press to Enter key. In stead it should run when you change the field that should trigger the checks.
    Is this possible, and how do I do this?

    Reply
    1. karinpb Post author

      Hi, you can use the LostFocus event and compare the value once the focus is lost. But if you would like to prevent a potential request you still need to have the code that is in this post as well to cancel the request. Because one possible scenario is that the user has entered the value in the field and then press Next which means that LostFocus will be triggered but also an request.

      I’m afraid I don’t have a ready example so you would have to know how to add an eventhandler or find that info on this blog or online.

      Reply
  3. Kesavan Suparaajuthan

    HI, Many thanks for guiding us in many ways. I have tried following script for doing mforms automation to add the departure date in MWS410 via excel.
    I got MNS921 popup box, and from that point onwards i am not able to access the text/date filed in the pop up box. How can i proceed or what is the correct name for the MNS921 pop box text/date field(WWVAL1)?

    import System;
    import System.Windows;
    import System.Windows.Controls;
    import MForms;
    import Mango.UI.Core;
    import Mango.UI.Core.Util;
    import Mango.UI.Services;
    import System.Threading.Thread;

    package MForms.JScript {

    class InactiveItems {

    public function Init(element: Object, args: Object, controller : Object, debug : Object) {
    var fso, f1, ts, s, rline;
    var ForReading = 1;

    fso = new ActiveXObject(“Scripting.FileSystemObject”);

    ts = fso.OpenTextFile(“C:\Users\KesavanS\Desktop\OIS300 dep date\UpdateCO.csv”, ForReading);
    while( !ts.AtEndOfStream ){
    s = ts.ReadLine();
    rline = new Array();
    rline = s.split(“,”);
    if(!trim(rline[0])== “”){

    reclassify(debug, rline);
    }
    else{
    }
    }

    ts.Close();
    MessageBox.Show(“done”);

    }

    public function reclassify(debug : Object, data : Object){

    try {

    var auto = new MFormsAutomation();

    auto.AddStep(ActionType.Run, ‘MWS410’);
    auto.AddStep(ActionType.Key, ‘ENTER’);
    auto.AddField(‘WWQTTP’,’7′);
    auto.AddStep(ActionType.Key, ‘ENTER’);
    auto.AddField(‘W1OBKF’,trim(data[0]));
    auto.AddStep(ActionType.Key, ‘ENTER’);
    auto.AddStep(ActionType.ListOption, ‘2’);
    auto.AddStep(ActionType.Key, ‘ENTER’);
    auto.AddStep(ActionType.Key, ‘ENTER’);
    auto.AddStep(ActionType.Key, ‘F16’);
    auto.AddField(‘WWVAL1’,trim(data[1]));
    auto.AddStep(ActionType.Key, ‘ENTER’);

    var uri = auto.ToUri();
    DashboardTaskService.Manager.LaunchTask(new Task(uri));

    } catch (e) {
    debug.WriteLine(e);
    }
    }

    function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g,””);
    }

    }
    }

    Thank you in advance.

    Kesavan

    Reply
    1. norpe

      The MNS921 dialog uses generic fields named FIELD1, FIELD2 etc. Which fields are actually used depends on the M3 program so it is not possible to just count the number of visible fields in the dialog. The easiest way to see which fields are used is to run Fiddler and check the response XML in Inspectors > Response (lower pane) > WebView.

      In this specific case:
      Departure date = FIELD5
      Departure time = FIELD6

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s