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.









Fantastic post, Ayaz. I posted a link to it on the CEI blog:
http://blog.customereffective.com/blog/2010/09/a-closer-look-at-crm-2011-jscript-libraries.html
Thanks Joel, Appreciated!
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
Very good blog dude..
Hi Ayaz, I’ve heard that each JavaScript Common Library web resource can only contain a single function. Is that true?
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
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?
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!
You are right Anne but when it comes to outlook and outlook offline clients then managing those JavaScript libraries will be nightmare.
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.
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!
I love Big Time Rush! I cannot wait to see them in Concert!
Ayaz. Can you provide a code sample for hiding/showing sections based on values in an optionset/picklist?
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?
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?
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
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
hi Anubhav,
Thank you for your respon.
I will try it. and i’ll let you know later…
Thanks a lot.
de Sandoz
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!
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);
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!
My spouse and I stumbled over here from a different web page
and thought I may as well check things out. I like
what I see so i am just following you. Look forward to looking at your
web page for a second time.
I like the helpful info you provide in your articles.
I’ll bookmark your blog and check again here frequently. I am quite certain I will learn many new stuff right here! Good luck for the next!
I think this is one of the most vital info for me.
And i am glad reading your article. But should remark on some general
things, The website style is ideal, the articles is really great : D.
Good job, cheers
Hello, i think that i saw you visited my blog thus i came to
“return the favor”.I’m attempting to find things to improve my web site!I suppose its ok to use some of your ideas!!
Hello There. I discovered your weblog using msn. That is a really smartly written article.
I will make sure to bookmark it and come back to read more
of your useful info. Thanks for the post. I’ll certainly return.
I am regular visitor, how are you everybody? This article posted at this web site is in fact fastidious.
Hello there! Quick question that’s entirely off topic. Do you know how to make your site mobile friendly? My web site looks weird when browsing from my iphone 4. I’m trying to find a
theme or plugin that might be able to correct this issue.
If you have any suggestions, please share. With thanks!