0

While updating all our javascript files for D365 CE (Online), I noticed we have many JS files that are not been called from the form events like OnSave, OnLoad or OnChange. These files are only been called from an internal references by other JS libraries.

Since these files are not been called on the form event, I have to pass the execution context when the function is called. (Which is don't prefer to do as it we I don't know how many other JS libraries are calling these functions. Only 1 solution is part of this sprint at the moment)

My question is, if I have a JS library that is loaded on the form first, and if this file have a global execution context, can I just use that in all the following JS libraries?

for example: JSFile1- Function XYZ(eContext) - Runs OnLoad of the form. (execution context passed as parameter)

JSFile2 - Function ABC() - NOT called from any form events but need execution context.

In JSFile1, I am declaring a global var eContext and initialising it within the XYZ(eContext) function.

Can I then use this global eContext var in any way in my JSFile2?

Kmria
  • 30
  • 5

1 Answers1

1
  1. Approach not correct Reason: At the end you will have to use Globally defined form context in your dependent Js because you will need to have this context in your dependent Js to do some coding/transaction
  2. As you are already in Process of Refactoring, why not pass this Context as Parameter to your other js, so that you have this context to make your transaction.
  3. You will also have to take care of order of your Js on Form, i.e the one which gets context should be the first one to load. i.e

    JSFile1- Function XYZ(eContext)

    SFile2 - Function ABC(eContext)

AnkUser
  • 5,421
  • 2
  • 9
  • 25
  • Thanks @AnkUser. The 2nd point you made, is what I have been doing for all other projects. However, in this case, I am working on 1 of many solutions that might be calling these common functions from JSFile2. Which means, I have to change calling function in all solutions. For now, I am just creating a dummy function in each of this JSFiles2 and loading execution context on load. – Kmria May 17 '19 at 10:56