web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

Can someone explain why this global variable will not work?

(0) ShareShare
ReportReport
Posted on by 189

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);

}

I have the same question (0)
  • Suggested answer
    Adrian Begovich Profile Picture
    1,027 Moderator on at

    Hi JFulfordMS,

    You need to declare the SomeDataValue variable.

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

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    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

  • JFulfordMS Profile Picture
    189 on at

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

  • JFulfordMS Profile Picture
    189 on at

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

  • JFulfordMS Profile Picture
    189 on at
    [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

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 74

#3
Martin Dráb Profile Picture

Martin Dráb 64 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans