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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Odd behavior on a Type...
Power Apps
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 !!

 

 

 

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 796 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 327 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard