The newlook scripts are run in the detail code behind in the method am_Newlook_ProcessCommandScript. This method is called after the data is loaded which also occurs after the detail window is shown.  The user will see a Navigating message while the script is running.

You can run the newlook scripts prior to the detail window showing. For example, you may want to write code to close the window if the script does not navigate to the correct screen and prompt the user with a message box instead. This can easily be achieved with the following code below.

In the am_BeforeProcessCommand method in the detail code behind, you can trap individual commands or trap all commands. When this method is called, the detail window is currently not shown.  This allows you to call the am_Newlook_ProcessCommandScript method prior to the detail window showing.

This example calls am_Newlook_ProcessCommandScript prior to the detail window showing for the "NEW" and "OPEN" commands. The screen messages are checked and if it contains a certain message, the user is prompted with a message box and the detail is closed without ever showing.  Otherwise, the detail window will show as usual.

protected override void am_BeforeProcessCommand(AB_Command command, System.Windows.RoutedEventArgs e)
{

    switch (command.ap_CommandID)
    {
                case "NEW":
                case "OPEN":

                    am_Newlook_ProcessCommandScript(command.ap_CommandID, ap_CurrentCommand, ap_EntityToPerformCommandOn);
                    if (ap_NewlookReference.ap_ScreenMessages.Contains("You are not authorized"))
                    {
                        MessageBox.Show(ap_NewlookReference.ap_ScreenMessages, "Error", MessageBoxButton.OK);
                        e.Handled = true;
                        Close();
                    }


                    break;
    }

}

In the method am_Newlook_ProcessCommandScript in the detail code behind, you need to make sure of 2 things:

1) That you call the base method in am_Newlook_ProcessCommandScript. This has logic to ensure that the framework does not call the method am_Newlook_ProcessCommandScript in the data loaded event if you have already called this method in the am_BeforeProcessCommand method. Remember that the default behavior is to call am_Newlook_ProcessCommandScript when the data is loaded. We don't want to call this method twice. Add this as the first line of code in the method am_Newlook_ProcessCommandScript.

 base.am_Newlook_ProcessCommandScript(scriptCommand, command, EntityToPerformCommandOn);

2) The default when playing a transaction is to call the transaction in the completed event of a background worker. The parameter is called _RunWorkerAsync and by default is true. If  this is true, the ap_NewlookReference variable will not be updated with the screen name, title, messages etc. immediately following the am_PlayTransaction.  If you want to wait for the script to complete, then you need to set _RunWorkerAsync to false. The last parameter in this code snippet is false which will wait for the script to complete. This will allow you to access the screens and messages immediately following the am_PlayTransaction.

ap_NewlookReference.am_PlayTransaction("<TransactionName>", true, scriptKeyInput, false);