#GPPT Converting .Net Execute Scripts from C# to VB.Net


David Meego - Click for blog homepageFollowing on from the previous articles: #GPPT Speeding Up Referencing Modifier Added Fields and staying Web Client compatible and #GPPT Referencing Modifier Added Fields in the Web Client using .Net Execute, this article looks how we can use VB.Net rather than C# as the scripting language in the .Net Execute scripts.

Both VB.Net and C# use the .Net Common Language Runtime (CLR) and it is usually possible to write exactly the same code in either language. So we will duplicate our existing C# scripts and convert them to VB.Net, then we will duplicate the triggers to call the new scripts.

Please note that there does not seem to be much difference between the performance of C# and VB.Net code when using .Net Execute scripts. The compilation time seems to be about the same and the execution is the same. The only reason you might want to use VB.Net rather than C# is that the language syntax is probably closer to Dexterity sanScript (without the semicolons).

The steps in this article are not required in any way and are just to show that you can choose which .Net language you wish to use. It would have been possible to use VB.Net initially and later swap from VB.Net to C#. When using the .Net Execute script editor, the Insert and Helper buttons automatically adjust to the current language.

[Edit] The upcoming GP Power Tools Build 28 no longer uses “Continuum” and so no longer is affected by the limitations this caused.

We are going to continue using the example from the recent series of articles that started with #GPPT Beginner’s Guide to Adding Custom Fields – Introduction.

The steps involved from a high level are as follows:

  1. Duplicate the .Net Execute scripts and convert the code to VB.Net.
  2. Duplicate the Triggers and change them to call the VB.Net scripts.
  3. Disable the previous Triggers which called C# scripts.

Duplicate the .Net Execute Scripts

  1. Open the Project Setup window to our ITEM URL project and double click on the ITEM URL READ .Net Execute Script to open it. Click Duplicate and enter ITEM URL READ2 as the new Script ID and press Enter.
  2. Use Ctrl-A to select all of the script and then Ctrl-C to copy it to the clipboard. Open the handy Telerik Code Converter tool in a browser and paste in the C# code into the left pane. Click Convert Code to generate the VB.Net equivalent code. Click Copy To Clipboard.
  3. Back on the .Net Execute window, change the script language to Visual Basic .Net and say Yes to the warning dialog. Click on the References button and add the reference to the Application.Dynamics.ModifiedForms.dll file. Then press Ctrl-A to select the script and Ctrl-V to paste in the converted script. Click Save.
  4. Repeat this process to duplicate the ITEM URL SAVE C# .Net Execute script to create the ITEM URL SAVE2 VB.Net .Net Execute script.

Duplicate the Triggers

  1. From the Project Setup window, double click on the ITEM URL READ3 Trigger, click Duplicate and create the ITEM URL READ4 trigger. On the Script tab change the name of the .Net Script loaded from ITEM URL READ to ITEM URL READ2 and Click Save.
  2. Repeat this process to duplicate ITEM URL SAVE3 trigger to ITEM URL SAVE4 trigger which calls the ITEM URL SAVE2 .Net Execute script.

Disable the previous Triggers

  1. From the Project Setup window, open the ITEM URL READ3 Trigger, select the Disabled checkbox and click Save.
  2. Repeat for the ITEM URL SAVE3 trigger.

Below is the code from the two .Net Execute Scripts:

ITEM URL READ2 .Net Execute Script

Imports System
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Imports Microsoft.Dexterity.Bridge
Imports Microsoft.Dexterity.Applications
Imports Microsoft.Dexterity.Applications.DynamicsDictionary
Imports Microsoft.Dexterity.Applications.DynamicsModifiedDictionary
Imports Microsoft.Dexterity.Applications.GpPowerToolsDictionary

Namespace NetExecute

	Class Program
		Public Function Run() As Object
			' Add your code below this line
		
			Dim MBS_ObjectID As String = ""
			Dim MBS_Property As String = ""
			MBS_ObjectID = Dynamics.Forms.IvItemMaintenance.IvItemMaintenance.ItemNumber.Value
			Microsoft.Dexterity.Applications.GpPowerTools.Procedures.MbsDuosGet.Invoke("Item", MBS_ObjectID, "URL", MBS_Property)
			
			DynamicsModified.Forms.IvItemMaintenance.IvItemMaintenance.LocalUrl.Value = MBS_Property
		
			' Add your code above this line
		End Function
	End Class

End Namespace

ITEM URL SAVE2 .Net Execute Script

Imports System
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Imports Microsoft.Dexterity.Bridge
Imports Microsoft.Dexterity.Applications
Imports Microsoft.Dexterity.Applications.DynamicsDictionary
Imports Microsoft.Dexterity.Applications.DynamicsModifiedDictionary
Imports Microsoft.Dexterity.Applications.GpPowerToolsDictionary

Namespace NetExecute

	Class Program
		Public Function Run() As Object
			' Add your code below this line
		
			Dim MBS_ObjectID As String = ""
			Dim MBS_Property As String = ""
			MBS_ObjectID = Dynamics.Forms.IvItemMaintenance.IvItemMaintenance.ItemNumber.Value
			MBS_Property = DynamicsModified.Forms.IvItemMaintenance.IvItemMaintenance.LocalUrl.Value
			If MBS_Property <> "" Then
				Microsoft.Dexterity.Applications.GpPowerTools.Procedures.MbsDuosSet.Invoke("Item", MBS_ObjectID, "URL", MBS_Property)
			Else
				Microsoft.Dexterity.Applications.GpPowerTools.Procedures.MbsDuosDel.Invoke("Item", MBS_ObjectID, "URL")
			End If
		
			' Add your code above this line
		End Function
	End Class

End Namespace

Below is the code from the two new Triggers Scripts:

ITEM URL READ4 Trigger Script

in string IN_OldValue;
in string IN_NewValue;
out boolean OUT_Condition;
local integer MBS_Mode;
local text MBS_References;
local text MBS_Script;
local text MBS_Results;
local boolean MBS_Success;
local boolean MBS_Access;
local integer MBS_AltDictID;
local boolean MBS_Modified;
local text MBS_Text_Field;
local integer MBS_Dictionary;
local integer MBS_Status;

OUT_Condition = false;

if isopen(form IV_Item_Maintenance) then
	call with name "MBS_Security_Form_Check" in dictionary 5261, 0, "IV_Item_Maintenance" {Form}, 
		MBS_Access, MBS_AltDictID, MBS_Modified;
	if MBS_Access and MBS_AltDictID = 0 {Product} and MBS_Modified then 
		{ Code in here } 
		
		if IsWebClient() then
			call with name "MBS_Script_Load_Net" in dictionary 5261, 
				"ITEM URL READ2", MBS_Mode, MBS_References, MBS_Script;

			MBS_Success = false;
			call with name "MBS_Net_Execute" in dictionary 5261, 
				MBS_Mode, MBS_References, MBS_Script, MBS_Results, MBS_Success;
			if not MBS_Success then
				warning MBS_Results;
			end if;
		else
			call with name "MBS_Script_Load_Dex" in dictionary 5261, 
				"ITEM URL READ", MBS_Text_Field, MBS_Dictionary;
				
			call with name "MBS_Runtime_Execute_Modified" in dictionary 5261, 
				MBS_Text_Field, MBS_Dictionary, MBS_Status;
			if MBS_Status <> OKAY then
				warning MBS_Text_Field;
			end if;
		end if;
		
		OUT_Condition = true;
	end if;
end if;

ITEM URL SAVE4 Trigger Script

in string IN_OldValue;
in string IN_NewValue;
out boolean OUT_Condition;
local boolean MBS_Access;
local integer MBS_AltDictID;
local boolean MBS_Modified;
local integer MBS_Mode;
local text MBS_References;
local text MBS_Script;
local text MBS_Results;
local boolean MBS_Success;
local text MBS_Text_Field;
local integer MBS_Dictionary;
local integer MBS_Status;

OUT_Condition = false;

if isopen(form IV_Item_Maintenance) then
	if not 'Save Record' of window IV_Item_Maintenance of form IV_Item_Maintenance then
		abort script;
	end if;

	call with name "MBS_Security_Form_Check" in dictionary 5261, 0, "IV_Item_Maintenance" {Form}, 
		MBS_Access, MBS_AltDictID, MBS_Modified;
	if MBS_Access and MBS_AltDictID = 0 {Product} and MBS_Modified then 
		{ Code in here } 
		
		if IsWebClient() then
			call with name "MBS_Script_Load_Net" in dictionary 5261, 
				"ITEM URL SAVE2", MBS_Mode, MBS_References, MBS_Script;

			MBS_Success = false;
			call with name "MBS_Net_Execute" in dictionary 5261, 
				MBS_Mode, MBS_References, MBS_Script, MBS_Results, MBS_Success;
			if not MBS_Success then
				warning MBS_Results;
			end if;
		else
			call with name "MBS_Script_Load_Dex" in dictionary 5261, 
				"ITEM URL SAVE", MBS_Text_Field, MBS_Dictionary;

			call with name "MBS_Runtime_Execute_Modified" in dictionary 5261, 
				MBS_Text_Field, MBS_Dictionary, MBS_Status;
			if MBS_Status <> OKAY then
				warning MBS_Text_Field;
			end if;
		end if;

		OUT_Condition = true;
	end if;
end if;

Here is the updated exported Configuration Settings file for the example:

Hope you find this information useful.

David

08-Apr-2020: Added comment that GP Power Tools Build 28 no longer uses “Continuum”.

This article was originally posted on http://www.winthropdc.com/blog.

Please post feedback or comments

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.