Detect entry mode in Smart Office script

Here is a solution for a Script in Lawson Smart Office to detect the entry mode of an M3 panel: 1-Create, 2-Change, 3-Copy, 4-Delete, or 5-Display.

Scenario

This solution is useful for a scenario like this. Suppose your script dynamically adds an input field to an M3 panel. The input field could be used for example to enter a discount amount which will re-calculate a net value in another field. You would like to either enable that input field so that it becomes editable for the user, either disable it so that it is read-only. That will match the behavior of the M3 panel, for better usability. For that, you will need to tell apart if the user entered the M3 panel in Change mode or in Display mode.

Detect the entry mode

Ideally, we would like to use an API in Smart Office like GetEntryMode() that would return the entry mode. But there is no such API.

Otherwise, we would like to simply read the Control’s properties such as IsReadOnly or IsEnabled. But somehow they incorrectly return false.

The workaround is to read the LSTOPT value that is sent by the M3 panel when it makes the HTTP Request to the M3 server. That value is sent from panels A and B. Note that the option Create returns -1 (negative) instead of 1 (positive).

function OnRequesting(sender: Object, e: CancelRequestEventArgs) {
    if (e.CommandType == 'LSTOPT') {
        e.CommandValue // the entry mode: -1, 2, 3, 4, or 5
    }
}

Set the value

Once we get the value from panels A or B, we have to store it somewhere because Smart Office will unload the script and our value off memory when it executes the said Option and the value will not be available anymore in subsequent panels. We can use the InstanceCache to store the value in the current Smart Office session. The value will then be available in panels E, F, G, etc. In the following example I called my key LSTOPT but you can choose another one:

InstanceCache.Add(controller, 'LSTOPT', e.CommandValue);

Get the value

Once we are in panels E, F, G, etc. we retrieve the value from the InstanceCache like this:

if (InstanceCache.ContainsKey(controller, 'LSTOPT')) {
    var LSTOPT = InstanceCache.Get(controller, 'LSTOPT'); // get the entry mode
    if (LSTOPT == -1) {
        // Create
    } else if (LSTOPT == 2) {
        // Change
    } else if (LSTOPT == 3) {
        // Copy
    } else if (LSTOPT == 4) {
        // Delete
    } else if (LSTOPT == 5) {
        // Display
    } else {
        // entry mode unknown
    }
} else {
     // entry mode not set
}

Installation

This script has a getter and a setter:

  • The setter must be placed in the OnRequesting event handler of panel A or B.
  • The getter must be attached in the panel (E, F, G, etc.) where you want to know the entry mode.

Tests

I tried this technique in the following conditions:

  • The technique works whether the user selects the option from the Options menu, or from double-clicking a record in panel B, or by selecting the option in the right-click context menu.
  • Also, it works whether the users enters the M3 program via panel A or via panel B.
  • Also, it works with multiple instances of the M3 program running at the same time.
  • Also, it works with different M3 programs using the same script.

Sample source code

Here is a sample source code for the setter:

import MForms;

package MForms.JScript {
	class EntryModeSetter {
		var controller;
		public function Init(element: Object, args: Object, controller: Object, debug: Object) {
			this.controller = controller;
			controller.add_Requesting(OnRequesting);
			controller.add_Requested(OnRequested);
		}
		function OnRequesting(sender: Object, e: CancelRequestEventArgs) {
			if (e.CommandType == 'LSTOPT') {
				InstanceCache.Add(controller, 'LSTOPT', e.CommandValue); // set the entry mode
			}
		}
		function OnRequested(sender: Object, e: RequestEventArgs) {
			sender.remove_Requesting(OnRequesting);
			sender.remove_Requested(OnRequested);
		}
	}
}

Here is a sample source code for the getter:

import MForms;

package MForms.JScript {
	class EntryModeGetter {
		public function Init(element: Object, args: Object, controller: Object, debug: Object) {
			if (InstanceCache.ContainsKey(controller, 'LSTOPT')) {
				var LSTOPT = InstanceCache.Get(controller, 'LSTOPT'); // get the entry mode
				if (LSTOPT == -1) {
					// Create
				} else if (LSTOPT == 2) {
					// Change
				} else if (LSTOPT == 3) {
					// Copy
				} else if (LSTOPT == 4) {
					// Delete
				} else if (LSTOPT == 5) {
					// Display
				} else {
					// entry mode unknown
				}
			} else {
				// entry mode not set
			}
		}
	}
}

That’s it!

Published by

thibaudatwork

ex- M3 Technical Consultant

5 thoughts on “Detect entry mode in Smart Office script”

  1. UPDATE: Another technique is to get the XML response with controller.Runtime.Result, and to read the value of //Root/ControlData/Bookmark/@opt . Although not all programs have a bookmark.

    Like

Leave a comment