run script delayed- missing spell in VSTools C# Coding
In Sanscript coding we have few commands, functions or procedures which are not provided by VSTools, so even though many people are now using VSTools as a customization tool for Dynamics GP instead of VBA and Modifier combination, sometimes even small functionalities will become very difficult to achieve.
Few days back similar kind of issue was shared by a developer on the forum where objective was to open the Purchasing Detail Entry window, set specific value of ship to address and then close the window. Sounds simple!
But actually it was not that simple. The issue in the beginning was that when setting value of ship to address with code, it wouldn’t actually update the ship to address and on closing and reopening of window bring the default value which was setting up by the application, and custom code was not able to change the ship to address value.
So I tried some C# and after some back and forth of lines of codes, it was actually changing the field value perfectly.
Dynamics.Forms.PopPoVendorDetailEntry.PopPoVendorDetailEntry.PrimaryShiptoAddressCode.Focus();
Dynamics.Forms.PopPoVendorDetailEntry.PopPoVendorDetailEntry.PrimaryShiptoAddressCode.Value = "WAREHOUSE";
Dynamics.Forms.PopPoVendorDetailEntry.PopPoVendorDetailEntry.PrimaryShiptoAddressCode.ForceValidate(true);
Dynamics.Forms.PopPoVendorDetailEntry.PopPoVendorDetailEntry.PrimaryShiptoAddressCode.RunValidate();
But now the issue was to automatically save the record and close the window. So this time the task was not that easy and back and forth of lines of codes were not enough.
The first thing which was tried is to RunValidate() for OK button, but it was actually not doing the complete task, it was closing the window but not saving the changes done by our earlier code for ship to address value. This Vendor Detail Entry window is a child window, so on save button it ask for confirmation if user really wants to save changes, this message pops when change flag of window gets true, so running two RunValidate() , one for ship to address field and another for okey button was messing up the code because of parallel running. In dexterity when we have to run two scripts we use delayed keywork so the delayed keyword then will delayed the called script to run and complete first before running the second script. But in VSTools there is no equivalent delayed keywork/function. So running two RunValidate was not doing what we needed
means RunValidate() — Ship to Address will run and complete the process
then RunValidate() — ok button will run and do the process.
Another issue was a pop up message, now we also have to close it automatically. After I tried many other things I thought why not use SendKey and close window calling OK button RunValidate(). It didn’t worked at first as still SendKey was running before Ship to address field RunValidate() gets finish its work. But then SendWait() slows down the things and made them perfect. And for pop up was handled by Modal Dialog events perfectly.
public class GPAddIn : IDexterityAddIn
{
// IDexterityAddIn interface
public static Microsoft.Dexterity.Applications.DynamicsDictionary.PopPoVendorDetailEntryForm.PopPoVendorDetailEntryWindow popEntryDetailWindow = Dynamics.Forms.PopPoVendorDetailEntry.PopPoVendorDetailEntry;
public void Initialize()
{
Dynamics.Forms.PopReceivingsEntry.OpenAfterOriginal += PopReceivingsEntry_OpenAfterOriginal;
Dynamics.Forms.PopPoEntry.PopPoEntry.VendorId.ValidateAfterOriginal += VendorId_ValidateAfterOriginal;
popEntryDetailWindow.BeforeModalDialog += PopEntryDetailWindow_BeforeModalDialog;
}
private void VendorId_ValidateAfterOriginal(object sender, EventArgs e)
{
//throw new NotImplementedException();
Dynamics.Forms.PopPoEntry.PopPoEntry.ExpansionButton4.RunValidate();
if (Dynamics.Forms.PopPoEntry.PopPoEntry.LocalStatus.Value == "New" || Dynamics.Forms.PopPoEntry.PopPoEntry.RevisionNumber.Value == 0)
{
Dynamics.Forms.PopPoVendorDetailEntry.PopPoVendorDetailEntry.PrimaryShiptoAddressCode.Focus();
Dynamics.Forms.PopPoVendorDetailEntry.PopPoVendorDetailEntry.PrimaryShiptoAddressCode.Value = "WAREHOUSE";
Dynamics.Forms.PopPoVendorDetailEntry.PopPoVendorDetailEntry.PrimaryShiptoAddressCode.ForceValidate(true);
Dynamics.Forms.PopPoVendorDetailEntry.PopPoVendorDetailEntry.PrimaryShiptoAddressCode.RunValidate();
SendKeys.SendWait("%{F4}");
}
}
private void PopEntryDetailWindow_BeforeModalDialog(object sender, BeforeModalDialogEventArgs e)
{
if (e.DialogType == DialogType.Ask)
{
if (e.Message.Contains("Do you want to save changes?"))
{
e.Response = DialogResponse.Button1;
}
}
}
This was originally posted here.
*This post is locked for comments