Home > CRM Development, General, MSCRM General > JavaScript Libraries in Microsoft Dynamics CRM 2011

JavaScript Libraries in Microsoft Dynamics CRM 2011


In Microsoft Dynamics CRM 4, it is a very tedious task for system customizers to manage and track JavaScript. System Customizers/Developers have to write JavaScript on every event handler on the form. But Microsoft Dynamics CRM 2011 provides JavaScript libraries that can be utilized on events and forms across the solution. Now common JavaScript functions can be written at one centralized JavaScript file and utilized across solution.

Moreover, multiple JavaScript libraries can be created. For example, one common library for general CRM Service calls and one common library for UI manipulation and so on.

Let’s create a very simple JavaScript library and create a function to hide/show a tab/section on the form. JavaScript libraries are added as web resources in solution as shown below:

Now let’s create a JavaScript Common Library by adding a web resource in the solution. Then attach a JavaScript file this web resource.

Now let’s open the file in Text Editor available in web resource to add a function to show/hide a section/tab on form.

Microsoft Dynamics CRM 2011 provides new xrmPage model to access UI and data at client side. Here is a snapshot of the hierarchy.

Now let’s go to the account form in solution and then go to form properties to set JavaScript library and function to execute on form load event handler as shown below:

Now let’s add library from solution library.

Now let’s call the HideShowSection function in JavaScript Library by selecting appropriate JS library and typing the right function.  Enable the event handler by setting Enable flag to true. Also pass parameter to function by comma separated list of parameters i.e. 1, false which in turn sets tab 1 to visible false.

You can see that Details tab is now hidden and alert.

About these ads
  1. September 26, 2010 at 12:10 am | #1
  2. September 26, 2010 at 7:58 am | #2

    Thanks Joel, Appreciated!

  3. October 20, 2010 at 3:46 am | #3

    I’ve been waiting for a CRM 2011 JS Library post, I was going to do one myself, but it looks like you’ve taken care of it. Thanks for the good post; we should link share sometime.
    -Paul Reitmeyer

  4. Dilan
    November 6, 2010 at 6:21 am | #4

    Very good blog dude..

  5. January 10, 2011 at 11:57 am | #5

    Hi Ayaz, I’ve heard that each JavaScript Common Library web resource can only contain a single function. Is that true?

  6. MohammadFarook
    January 12, 2011 at 9:10 am | #6

    Hi Ayaz,
    Iam using crm 2011. On form load of an entity ” object doesnot support this property” error is coming.
    I haven’t written any javascripst and nor attached websresoures.
    Need help.
    Thank you

  7. Abdul Majid
    January 26, 2011 at 12:13 am | #7

    I have a web resource which is a HTML page, on clicking a button on the html page I would like to assign a value to text box control on the parent CRM 2011 form. Could you please advise?

  8. May 9, 2011 at 11:41 am | #8

    You can setup CRM v4 to more efficiently share Javascript across entities, it just takes a bit more planning, thinking and design. You are correct that CRM v2011 makes bundling much more efficient and slick!

  9. May 9, 2011 at 11:29 pm | #9

    You are right Anne but when it comes to outlook and outlook offline clients then managing those JavaScript libraries will be nightmare.

  10. July 1, 2011 at 2:24 am | #10

    This blog appears to recieve a large ammount of visitors. How can you obtain traffic to this? It gives a nice individual twist upon issues. I guess getting something authentic or substantial to give info on is an essential factor.

  11. July 17, 2011 at 4:47 am | #11

    Hello would you mind sharing which blog platform you’re working with? I’m going to start my own blog in the near future but I’m having a hard time deciding between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I’m looking for something completely unique. P.S My apologies for getting off-topic but I had to ask!

  12. July 17, 2011 at 7:39 am | #12

    I love Big Time Rush! I cannot wait to see them in Concert!

  13. Kyle
    August 19, 2011 at 10:17 pm | #13

    Ayaz. Can you provide a code sample for hiding/showing sections based on values in an optionset/picklist?

  14. October 6, 2011 at 3:33 pm | #14

    Ayaz, i’m trying to do a library that contains all the common functions that i use to customize, but i’m not able to call a function put on “Library A” from “Library B”. Do you know if that is posible?

  15. yao
    October 19, 2011 at 3:15 am | #15

    Hi, i’ve 2 forms in an entity, and i want the system to auto change to certain form on the first time it load based on field’s value. I’ve tried Xrm.Page.ui.formSelector.items.get(1).navigate(); , but the ribbon will be disabled after it load to another form. Any idea?

  16. November 24, 2011 at 8:44 pm | #16

    How can i call a function from the same library with different parameter more than once. Because when i do so only one of them works

  17. de Sandoz
    November 27, 2011 at 1:09 am | #17

    Hi Ayaz,

    I need help.
    in my case, I must enter the “field of due date”.
    e.g. a case with product A, to be completed during 2 working days.
    let say case created on Friday, 25 Nov 2011 (03:00 AM), the due date = 2 days of work.
    means the due date shall be automatically filled = Tue, 29 Nov 2011 (03:00 PM).
    My question is:
    - Whether the above can be done by mscrm workflow feature? (if yes, how…? ^.^). or
    - Whether the jscript can perform the calculation of due date. with validation work hours and holidays? (if yes, how…? ^.^).
    please advice and help …

    Thank you in advance.

    de Sandoz

  18. April 4, 2012 at 6:11 pm | #18

    de Sandoz :
    Hi Ayaz,
    I need help.
    in my case, I must enter the “field of due date”.
    e.g. a case with product A, to be completed during 2 working days.
    let say case created on Friday, 25 Nov 2011 (03:00 AM), the due date = 2 days of work.
    means the due date shall be automatically filled = Tue, 29 Nov 2011 (03:00 PM).
    My question is:
    - Whether the above can be done by mscrm workflow feature? (if yes, how…? ^.^). or
    - Whether the jscript can perform the calculation of due date. with validation work hours and holidays? (if yes, how…? ^.^).
    please advice and help …
    Thank you in advance.
    de Sandoz

    Hi,
    you may try the below javascript:

    duedate, and createdate are the variables used for the care create date and case due date

    var duedate = new Date(createdate.setDate(createdate.getDate() + 1));
    duedate = duedate.setHours(23, 59, 59);
    Xrm.Page.getAttribute(“duedate attribute”).setValue(duedate);

    I hope it helps,
    Thanks,
    Anubhav

  19. de Sandoz
    April 5, 2012 at 5:55 am | #19

    hi Anubhav,

    Thank you for your respon.
    I will try it. and i’ll let you know later…

    Thanks a lot.
    de Sandoz

  20. Buscher
    May 23, 2012 at 3:33 pm | #20

    Hey Ayaz,

    I am trying to find the best way to do simple calculations automatically inside the system. If there is a javascript available to perform this action I would greatly appreciate it.
    Example: If I insert a value into “Annual Unit Impact” and another value into “Unit Selling Price” then the two values would be added together and the result would appear in a third box “Profit Improvement”
    Once I have a basic code, I can manipulate it on my own to perform other tasks like multiplying two fields into a third, etc.
    I am just wondering if there is a javascript available that can perform that function.

    Thank you so much for your help. Great blog!

  21. May 24, 2012 at 8:55 am | #21

    Here we go Buscher!

    1: function calculate()
    2: {
    3: var val1 = Xrm.Page.entity.attributes.get['new_val1'].getValue();
    4: var val2 = Xrm.Page.entity.attributes.get['new_val2'].getValue();
    5:
    6: if(val1==null)return;
    7: if(val2==null)return;
    8:
    9: var result = val1 * val2;
    10: Xrm.Page.entity.attributes.get['new_result'].setValue(result);

  22. June 21, 2012 at 8:34 pm | #22

    Hey Ayaz,

    I have a field that is a picklist with 4 different options. I also have four different date and time fields. I need a jscript that will insert the “today date” into one of the boxes based on which option is chosen from the picklist.
    I have the following script:

    1: set_date = function () {
    2: if(Xrm.Page.getAttribute(“new_stage”).getSelectedOption().text==”Innovate Solution”);
    3: if(Xrm.Page.getAttribute(“new_stage”) == null) return;
    4: Xrm.Page.getAttribute(“new_stage2″).setValue(new Date);
    5: }

    Could you provide me with some insight as to where I am going wrong?
    I was planning on just writing 4 different codes and switch out the “Innovate Solution” option with the others.
    Let me know if you can help!

    Thanks so much I love the blog!

  23. sindhu
    July 16, 2013 at 2:23 pm | #23

    HI Ayaz,

    Iam new to mscrm….actually am facing some telephonic interviews right now….i have some queries which i couldn`t answer….please suggest me how to answer these type of questions….

    Please help me….

    What is the code that u write in javascript to access mscrm?

    How can we access mscrm with javascript?

    where do you deploy a plugin?

    How can we hide system views?

    How can we add a new user to MSCRM?

    who can add a new user to mscrm?

    Requirement: An user from one business unit should not be able to see the another business unit users and there data .what do u do?

    How do u done data migration?

    Body of the crm page gets hang why?

    After registering new step in plugin what happens?

    On page load am getting an error message on behalf of jscript code. when i press ok it was gone.I don`t want to get that message again and again on page loading what do u do?

    when crm home page loads i want to get the data according to my requirement like location, status,centre…..How can we do it by crm?

  24. October 21, 2013 at 4:02 pm | #24

    Can you tell how to create a js library file

  1. September 26, 2010 at 5:13 pm | #1
  2. September 27, 2010 at 4:01 pm | #2
  3. October 12, 2010 at 9:25 pm | #3
  4. October 20, 2010 at 11:57 pm | #4
  5. October 21, 2010 at 12:17 am | #5
  6. December 12, 2010 at 11:08 pm | #6
  7. February 12, 2011 at 10:34 am | #7
  8. September 1, 2011 at 9:46 pm | #8

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 58 other followers

%d bloggers like this: