Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

Capturing field value change using C#

Posted on by 75,730

I have some C# code that allows the user to change the batch ID on the Sales Transaction Entry screen. I am doing this because I need to execute code behind the scenes as the batch ID is changed. The problem I have encounter is that the screen does not sense the change so as the user moves passed the batch ID field the user does not get the prompt about batch not existing, do you want to add it. Is there a way to trigger that routine so they will be prompted? If not, I will move my code to an event that senses the the batch ID being changed.

Categories:
  • Richard Wheeler Profile Picture
    Richard Wheeler 75,730 on at
    RE: Capturing field value change using C#

    Thank you both. Maybe I will get lucky and have a shark drag me out to sea next week. This particular project should have been done months ago but the goal posts keep changing. I will come back with a clearer mind and dig deep into Dexterity.

  • Almas Mahfooz Profile Picture
    Almas Mahfooz 11,003 User Group Leader on at
    RE: Capturing field value change using C#

    I  second  you , dexterity is the foundation. If learnt it , then work on all the respective tools.

    Quote of the day

    "Understanding how and why GP works the way it does because of the way Dexterity works is invaluable." 

  • David Musgrave MVP GPUG All Star Legend Moderator Profile Picture
    David Musgrave MVP ... 13,928 Most Valuable Professional on at
    RE: Capturing field value change using C#

    Ha ha. Good one Almas. Very funny.

    Enjoy your trip Richard.

    I still firmly believe that anyone working with Visual Studio Tools will be twice as efficient as a developer once they have completed Dexterity training even if they never code in Dexterity. Understanding how and why GP works the way it does because of the way Dexterity works is invaluable.

  • David Musgrave MVP GPUG All Star Legend Moderator Profile Picture
    David Musgrave MVP ... 13,928 Most Valuable Professional on at
    RE: Capturing field value change using C#

    Hi Almas

    Focusing to a field before changing it can be helpful when the change script using old() or diff() commands.

    However, I would disagree with clearing a field before setting it as this could then give the old() and diff() commands an incorrect previous value.

    FYI: as per page 154 of VSTDGPProgrammersGuide.pdf

    ForceValidate(true) is the equivalent of force change.

    ForceValidate(false) is the equivalent of clear force change.

    RunValidate is the equivalent of run script.  

    I agree that clearing a table before using it is best practice, but not clearing a field.

    Regards

    David

  • Almas Mahfooz Profile Picture
    Almas Mahfooz 11,003 User Group Leader on at
    RE: Capturing field value change using C#

    @Richard, good thing is that you don't need to take Dexterity Manuals to the annual fishing trip (I feel  your issue is solved).

    You can focus() all your dexterity in 'Fishing'.

    :D

  • Suggested answer
    Almas Mahfooz Profile Picture
    Almas Mahfooz 11,003 User Group Leader on at
    RE: Capturing field value change using C#

    Thank you Richard, I got it and will look into it more. But  you can try using these two lines as mentioned by David, it will change batch id value and also prompt for adding batch. Let us know if it is also working for you.

    Dynamics.Forms.SopEntry.SopEntry.BatchNumber.Value = "Test";
    Dynamics.Forms.SopEntry.SopEntry.BatchNumber.RunValidate();

    Even in earlier case when you were pressing the save button it has to prompt the new batch message as when you focus on any other field , you actually tab out from batch field (means focus out ) and then message should be prompted. I think it may not be prompting because of the event you are setting batch value from, I will try to run your code to confirm this.

    About using Focus() and Clear() 

    Their use is not inessential, but I would say they are significant and good practice. As it is not known exactly when you are setting up the batch ID value and at that time where you current focus is, so it's better to focus the field your are going to set a value for.

    • The focus statement can move the focus to the fields, and always runs the post script of the current field and the pre script for the new field so in this way we are save to complete all the necessary script/codes of current field and pre script of new field (if there is any). 
    • If also help to work correctly when we don't know exactly where the current field lead us as per tab sequence defined,  like sometimes when we return value from lookups they don't ended up on the lookup field  but set focus to some other field.
    • The focus statement moves the focus to the specified field. This statement can be used to skip unnecessary fields in a window, or to place the focus in a specific field after an event.
    • It is normal practice in dexterity to clear fields before setting new values and throughout you can see in dexterity manual.

    RunValidate() is equivalent of dexterity 'force change'. ( as appears to me).

    The force change statement causes the field change script to be run when the user moves the focus out of the field, whether or not the field has changed. This statement should be used only for the field where the focus is currently positioned.

  • Suggested answer
    David Musgrave MVP GPUG All Star Legend Moderator Profile Picture
    David Musgrave MVP ... 13,928 Most Valuable Professional on at
    RE: Capturing field value change using C#

    Hi Richard

    Almas code is correct, except there is no need to have the initial Focus() and Clear() commands, however it is providing a slightly different functionality to what you are asking for.

    Her code is defaulting the value and waiting for you to tab off the field which will then force the Batch Number Change script to execute.

    What you need is just

    Dynamics.Forms.SopEntry.SopEntry.BatchNumber.Value = "Test";
    Dynamics.Forms.SopEntry.SopEntry.BatchNumber.RunValidate();

     

    I have written up a blog that explains everything an published it today.

    winthropdc.wordpress.com/.../

    Regards

    David

  • Richard Wheeler Profile Picture
    Richard Wheeler 75,730 on at
    RE: Capturing field value change using C#

    I just sent you the entire bock of code. Thanks for your time. I can get it to display the change but not accept the change. If I were to click Save and pull the sales order up it would have the old batch ID.

  • Almas Mahfooz Profile Picture
    Almas Mahfooz 11,003 User Group Leader on at
    RE: Capturing field value change using C#

    I am getting prompt for new batch.No idea why you are not getting.If yoou feel okey email me your code so I will review the difference.

  • Richard Wheeler Profile Picture
    Richard Wheeler 75,730 on at
    RE: Capturing field value change using C#

    Almas, running this code does update the batch ID to VSTOOLS but when I move past the batch ID field I do not get prompted to create new batch. I need it to sense that the batch does not exist and get prompted to create it.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans