Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum
Suggested answer

Can someone explain why this global variable will not work?

Posted on by 185

Can someone explain how to persist global variables in the following situation? The on save method in this code will throw an error of 'undefined'.

SomeDataValue = "Hello";

function OnFormLoad(executionContext) {

}

function OnFormSaved(executionContext) {

alert(SomeDataValue);

}

  • JFulfordMS Profile Picture
    JFulfordMS 185 on at
    RE: Can someone explain why this global variable will not work?
    [quote user="Adrian Begovich"

    Hi JFulfordMS,

    You need to declare the SomeDataValue variable.

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    var SomeDataValue = "Hello";
    function OnFormLoad(executionContext) {
    }
    function OnFormSaved(executionContext) {
    alert(SomeDataValue);
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    [/quote

    The issue I was facing was that the variables were staying 'undefined'. Take the following example.

    var variableOne = false;
    var variableTwo = false;
    
    function OnLoad(executionContext) {
        formContext.getAttribute("my_variableOne").addOnChange(VariableOneChanged);
        formContext.getAttribute("my_variableTwo").addOnChange(VariableTwoChanged);
    }
    
    function VariableOneChanged(executionContext) {
        variableOne = true;
    }
    function VariableTwoChanged(executionContext) {
        variableTwo = true;
    }
    function OnSave(executionContext) {
        alert("VariableOne is: "   variableOne);
        alert("VariableTwo is: "   variableTwo);
    }

    In this example, assume that only the variableOne field has been changed.

    The output of this example would be.

    VariableOne is: true

    VariableTwo is: undefined

  • JFulfordMS Profile Picture
    JFulfordMS 185 on at
    RE: Can someone explain why this global variable will not work?

    Bipin, that actually didn't work. The variables didn't persist to the executionContext passed to the onsave event.

  • JFulfordMS Profile Picture
    JFulfordMS 185 on at
    RE: Can someone explain why this global variable will not work?

    That seems to have worked. Still seeing some unusual behavior though. I'm doing some testing and will post findings back shortly.

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,962 Moderator on at
    RE: Can someone explain why this global variable will not work?

    Hi,

    You can also use shared variable in your Javascript to pass data between different event.

    See below link for more details.

    docs.microsoft.com/.../setsharedvariable

    ExecutionContextObj.setSharedVariable(key, value)

    ExecutionContextObj.getSharedVariable(key)

    As suggested in above post you should declare your variable using var keyword then it will work.

    Please mark my answer verified if i were helpful

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 21,009 Super User 2024 Season 2 on at
    RE: Can someone explain why this global variable will not work?

    Hi JFulfordMS,

    You need to declare the SomeDataValue variable.

    var SomeDataValue = "Hello";
    
    function OnFormLoad(executionContext) {
    
    }
    
    function OnFormSaved(executionContext) {
        alert(SomeDataValue);
    }

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 9th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,277 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,137 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans