Hi I have something a bit odd happening on a Typescript Form Library.
I'm setting a static class variable for the formContext in the onLoad event, so that it can be re-used by various methods in the class.
But the "this" accessor doesn't work in any method except the onLoad method (which is where it is set)
Using the ClassName dot notation as the accessor, is OK.
See this code:
export class AccountForm {
static formContext: Xrm.FormContext;
static async onLoad(context: Xrm.Events.EventContext): Promise<void> {
this.formContext = context.getFormContext(); // set the static formContextvariable
this.formContext.getAttribute("name").addOnChange(this.onFieldChanged);
console.log(this.formContext) // This works fine
}
static async onFieldChanged() {
console.log(this.formContext); // This errors - if I break in debugger, this.formContext is undefined
console.log(AccountForm.formContext); //This is fine
}
}
I tried a simple sample Typescript app, which works fine:
class myClass {
static somevariable: string;
static onFirstClick() {
this.somevariable = "Hello World"; //initialise
}
static onSecondClick() {
console.log(this.somevariable); // works fine
}
}
Has anyone else experienced this ?
Not sure if its a Typescript issue, a Dynamics thing or I'm doing something wrong !!
WarrenBelz
109
Most Valuable Professional
Michael E. Gernaey
82
Super User 2025 Season 1
MS.Ragavendar
72