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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to use type script in Dynamics CRM

(0) ShareShare
ReportReport
Posted on by 6,212

Hi 
I would like to implement typescript in dynamics CRM. I have created a new simple type script.

Type script file

class Student {
//Function

age() {
return 20;
}
}

and also i have converted typescript  to javascript file.

After I don't know how should i cal age() function in the onchange event of Name field in the account entity.

Could you please any one help?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    JohnAnonymous Profile Picture
    5,241 on at

    If I look at the generated javascript code it looks like this:

    var Student = (function () {

       function Student() {

       }

       Student.prototype.age = function () {

           return 20;

       };

       return Student;

    }());

    So in the onchange event this should work:

    Student.age

  • Alagunellaikumar Profile Picture
    6,212 on at

    Module is namespace in typescript. If i use module in type script and then how should i call in the onchange event.

  • Alagunellaikumar Profile Picture
    6,212 on at

    /// <reference path="typings/Xrm/Xrm-2015.d.ts" />

    module KTI {

        class Student {

           //Function

                   OnNameChange() {

               alert("Testdata");

           }

           age() {

               return 20;

           }

       }

    }

    My TS file

  • Suggested answer
    JohnAnonymous Profile Picture
    5,241 on at

    The Javascript code:

    var KTI;

    (function (KTI) {

       var Student = (function () {

           function Student() {

           }

           //Function

           Student.prototype.OnNameChange = function () {

               alert("Testdata");

           };

           Student.prototype.age = function () {

               return 20;

           };

           return Student;

       }());

    })(KTI || (KTI = {}));

    So I would think that this should work in the onchange event:

    KTI.Student.age

    or:

    KTI.Student.OnNameChange

  • Alagunellaikumar Profile Picture
    6,212 on at

    Thanks Martin.

    If I want to call external javascript file inside ts how should i do

  • Suggested answer
    JohnAnonymous Profile Picture
    5,241 on at

    To be precise, you don't. You can't call a .ts file, you must call a .js file.

    There are two paths:

    - Either you use something like requirejs to load the required javascript files

    - Or you create all your modules and classes in separate files and use the typescipt compiler to build one javascript file.

    For CRM I would use the last option.

    An example from a project of mine:

    {

     "compilerOptions": {

       "module": "amd",

       "noImplicitAny": false,

       "noEmitOnError": true,

       "removeComments": true,

       "sourceMap": true,

       "target": "es5",

       "outDir": "./client",

       "outFile": "./client/app.js",

       "sourceRoot": "./client"

     },

     "exclude": [

       "node_modules",

       "wwwroot"

     ]

    }

    the outdir, outfile and sourceroot are the ones that tell where to look and where to place the js file.

  • Alagunellaikumar Profile Picture
    6,212 on at

    Hi Martin,

    Thanks for your time. I am very new to the type script, please tell me "An example from a project of mine:" code where should i put how should i use it. If you tell step by step that would be more helpful for me. 

    More over your second point combine all module class in a single .js file, how should i achieve it.

  • JohnAnonymous Profile Picture
    5,241 on at

    Please look at www.typescriptlang.org, there you can find a sample of the command line command (tsc) to compile your TypeScript to JS.

    tsc also has an option of using a tsconfig.json file in the root of your project. The example above is from one of my tsconfig.json files. Depending on your toolset there are two options:

    - Visual Studio -> you can add the tsconfig.json and/or set some typescript build properties. If you do so, building your solution will also build your ts and compile it to js.

    - Text editors (Visual Studio Code) -> you need to use the command line version. The tsconfig.json works the same.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans