Interacting with MES
- Last UpdatedNov 06, 2025
- 5 minute read
After you have a user logged into a session and on to an entity, you can start calling methods to interact with the application.
Note: It is assumed that the reader and the application programmer have a solid understanding and working knowledge of the Stateful API.
Example: Show Entity Data Script
You can trigger the ShowEntityData script after setting the UDA Trigger (me.EntityName) with the Entity Name you want to obtain information.
You must be logged on to the entity. You can use the ShowEntityData script as an Execute or OnTrue script with a UDA trigger (me.ShowEntityDataTrig).
' ShowEntityData Script
dim clientSession As aaFactMES.aaClientSession;
dim result As aaFactMES.Result;
dim entity As aaFactMES.aaEnt;
' reset the trigger
me.ShowEntityDataTrig = false;
' Get the singleton client session
result = aaFactMES.aaClientSession.GetInstance();
clientSession = result.Value;
if ( clientSession <> null ) then
if ( clientSession.curUser <> null and me.EntityName <> "" ) then
' get entity from passed in name - Note: we must be logged into entity
entity = null;
dim ii as integer;
dim tmpEnt as aaFactMES.aaEnt;
for each ii in clientSession.curUser.EntList.Keys
tmpEnt = clientSession.curUser.EntList[ii];
if ( tmpEnt.EntName == me.EntityName ) then
entity = tmpEnt;
exit for;
endif;
next;
if ( entity <> null ) then
' print info about entity
LogMessage("Info: "+entity.EntName+" id=" + entity.EntId );
LogMessage("Entity Capabilities: ");
LogMessage("CanRunJobs: "+entity.CanRunJobs.ToString() );
LogMessage("CanCaptureUtil: "+entity.CanCaptureUtil.ToString() );
LogMessage("CanTrackOEE: "+entity.CanTrackOEE.ToString() );
LogMessage("Entity Shift Information:");
LogMessage("CurShift: " + entity.CurShiftDesc);
LogMessage("Start Time: "+entity.CurShiftStartTime.ToString() );
if ( entity.JobExec <> null ) then
LogMessage("Job State: "+entity.JobExec.CurJobState.ToString() );
else
LogMessage("Job State: n/a" );
endif;
if ( entity.OEEExec <> null ) then
LogMessage("Current OEE:"+entity.OEEExec.CurrentOEE.ToString() );
LogMessage("Target OEE:"+entity.OEEExec.TargetOEE.ToString() );
LogMessage("Perform.: "+entity.OEEExec.CurrentPerf.ToString() );
LogMessage("Target Perf.: "+entity.OEEExec.TargetPerf.ToString() );
LogMessage("Curr. Quality:"+entity.OEEExec.CurrentQual.ToString() );
LogMessage("Target Qual.: "+entity.OEEExec.TargetQual.ToString() );
else
LogMessage("Current OEE: n/a" );
LogMessage("Target OEE: n/a" );
LogMessage("Current Performance: n/a" );
LogMessage("Target Performance: n/a" );
LogMessage("Current Quality: n/a" );
LogMessage("Target Quality: n/a" );
endif;
if ( entity.UtilExec <> null ) then
LogMessage("Utilization:");
LogMessage("Current: "+entity.UtilExec.CurrentUtil.ToString() );
LogMessage("Target: "+entity.UtilExec.TargetUtil.ToString() );
LogMessage("Reason Code: "+entity.UtilExec.UtilReasCd.ToString() );
LogMessage("Start Time: "+entity.UtilExec.UtilReasStart );
LogMessage("State Code: "+entity.UtilExec.UtilStateCd.ToString() );
' List reason codes available to entity
result = entity.UtilExec.GetUtilReasons();
if ( result.Success == true ) then
dim dts As System.Data.DataTableCollection;
dim dr As System.Data.DataRow;
dim dt As System.Data.DataTable;
dts = result.Value.Tables;
dt = dts[0];
for each dr in dt.Rows
' show each column in DS (uncomment lines below to
' see what column are available)
' dim dc As System.Data.DataColumn;
' for each dc in dt.Columns
' LogMessage("Column: " + dc.ColumnName);
' LogMessage(" Value: " + dr[dc] );
' Next;
' Show Reason Code and Desc columns
LogMessage("Reason Code: " + dr["reas_cd"] );
LogMessage("Reason Description: " + dr["reas_desc"]);
Next;
endif;
else
LogMessage("Current Utilization: n/a" );
LogMessage("Target Utilization: n/a" );
LogMessage("Utilization Reason Code: n/a" );
LogMessage("Utilization Start Time: n/a" );
LogMessage("Utilization State Code: n/a" );
LogMessage("Utilization Reasons: n/a" );
endif;
else
LogMessage("Could not find entity " + me.EntityName + ".");
LogMessage("Does it exist? Is the current user logged on to it?" );
endif;
endif;
endif;
Example: GetCurrentUserInfo Script
The GetCurrentUserInfo script writes information about the current user to the Logger. You should mark this as an asynchronous script.
You can use the GetCurrentUserInfo script as an Execute or OnTrue script with the UDA trigger (me.GetCurUserInfoTrig).
' GetCurrentUserInfo Script
dim clientSession As aaFactMES.aaClientSession;
dim result As aaFactMES.Result;
dim entityID as Integer;
' reset the trigger
me.GetCurUserInfoTrig = false;
' Get the singleton client session
result = aaFactMES.aaClientSession.GetInstance();
clientSession = result.Value;
if ( clientSession <> null ) then
dim i as integer;
dim ent as aaFactMES.aaEnt;
dim entNameList as string;
LogMessage("***********************************************");
'Current User Name
LogMessage("Current User Name: " + clientSession.curUser.UserName);
'Current User ID
LogMessage("Current User ID: " + clientSession.curUser.UserId);
'Current User Display Name
LogMessage("Current User Display Name: " + clientSession.curUser.UserDisplay);
'Show the entities the user is logged onto
for each i in clientSession.CurUser.EntList.Keys
'Seperate the Entity Names
if (entNameList.Length > 0) then
entNameList = entNameList + ", ";
endif;
'Get the entity which matches the Key.
ent = clientSession.CurUser.EntList[i];
'Append the entity name to the list.
entNameList = entNameList + ent.EntName;
Next;
if (entNameList.Length > 0) then
LogMessage("The user is logged onto the following Entities: " + entNameList);
else
LogMessage("The user is not logged onto any entities");
endif;
LogMessage("***********************************************");
endif;
Example: Starting a Job
The following sample code represents a method to start a job. There are two options for this basic command. This method is exposed at both the entity and job_exec levels. Both the method calls the same underlying transaction and the choice of which to use (consistency/convenience).
The following example represents the code to start a job using a method from the entity class.
Dim clientSession As aaFactMES.aaClientSession;
Dim result As aaFactMES.Result;
Dim EntList AS System.Collections.IDictionary;
Dim tempEnt AS aaFactMES.aaEnt;
Dim id as string;
Dim entity as aaFactMES.aaEnt;
' get the client session
result = aaFactMES.aaClientSession.GetInstance();
clientSession = result.Value;
if (clientSession <> null and clientSession.curUser <> null) then
' find the entity the user is logged into
EntList = clientSession.curUser.EntList;
for each id in EntList.Keys
tempEnt = EntList[id];
if (tempEnt.entName == me.entityName) then
entity = tempEnt;
exit for;
endif;
next;
if (entity <> null) then
' start the job
result = entity.StartJob(me.workOrderId, me.operationId, me.sequenceNo, "Auto Job Start");
if (result.Success) then
me.resultText = "Started job #" + me.sequenceNo + " for work order " + me.workOrderId + " at " + me.operationId;
else
me.resultText = result.Exception.Message;
me.resultIsError = true;
endif;
else
me.resultText = "User is not logged into entity '" + me.entityName + "'";
me.resultIsError = true;
endif;
else
me.resultText = "No Factelligence session started or user not logged on";
me.resultIsError = true;
endif;
Example: Querying the MES Database
It is often necessary to query the database for information. Whenever possible, it is best to use one of the supplied Get methods such as GetJobBOMData to retrieve BOM information for a Job. However, this is not always readily available so there is a generic method off the Client Session object to build a SQL statement and make a call using the Client Session connection to the database.
The following example demonstrates this with a SELECT statement.
'Query with one generic work order based on a specific operation
Dim clientSession As aaFactMES.aaClientSession;
Dim result As aaFactMES.Result;
Dim DataSet as System.Data.DataSet;
SQL = "SELECT wo.item_id, wo.wo_id, process_id, wo.state_cd, qty_reqd, qty_prod FROM wo JOIN job on wo.wo_id = job.wo_id WHERE wo.state_cd < 2 AND process_id = 'Production Process' ORDER BY state_cd desc";
Result = ClientSession.GetDSbySQL(sql);
DataSet = result.DataSet_Value;
if me.debug then
LogMessage( "Work order lookup data set row count for table 0: " + DataSet.Tables(0).Rows.Count);
LogMessage( "Work order lookup data set column count for table 0: " + DataSet.Tables(0).Columns.Count);
endif;
count = DataSet.Tables(0).Rows.Count;
if count == 0 then
me.ResultText = "No work order found for Production Process.";
me.resultIsError = true;
else 'get values from data set object
me.resultIsError = false;
me.ProduceItem = DataSet.Tables(0).Rows(0).Item("item_id");
me.WorkOrderID = DataSet.Tables(0).Rows(0).Item("wo_id");
state_cd = DataSet.Tables(0).Rows(0).Item("state_cd");
RequiredQty = DataSet.Tables(0).Rows(0).Item("qty_reqd");
ProduceQty = DataSet.Tables(0).Rows(0).Item("qty_prod");
endif;