Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Power Apps Pro Dev & ISV
Unanswered

Odd behavior on a Typescript Client-side script

(1) ShareShare
ReportReport
Posted on by 40

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 !!

 

 

 

  • Suggested answer
    kalluu91 Profile Picture
    32 on at
    Odd behavior on a Typescript Client-side script
    I'm a bit late. But I see that you are using `async` function without `await` which is not correct.
     
    You can remove the `async` from both methods. For the first method, change the return type from `Promise<void>` to `void`.
     
    Check this article for more details on how to setup correctly a Typescript project for web resources
     
    Cheers,
    Kim Anh

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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Leaderboard > Power Apps - Power Apps Pro Dev & ISV

#1
WarrenBelz Profile Picture

WarrenBelz 109 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 82 Super User 2025 Season 1

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 72

Overall leaderboard