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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Odd behavior on a Type...
Power Apps
Suggested Answer

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

 

 

 

I have the same question (0)
  • Suggested answer
    kalluu91 Profile Picture
    39 on at
    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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 335 Most Valuable Professional

#2
11manish Profile Picture

11manish 166

#3
sannavajjala87 Profile Picture

sannavajjala87 71 Super User 2026 Season 1

Last 30 days Overall leaderboard