CRM 2011 – Javascript Xrm.Page Basics

I have been looking at the CRM 2011 Javascript SDK, samples. Comparing it to CRM 4 code and getting very angry.  It just wasn’t the same and I wasn’t understanding the SDK, it was written in an odd way and the examples were just much more complex than I needed to get started.  I have also written a good blog entry about the comparison between Javascript CRM 2011 and CRM 4

I could hide sections and tabs ok, using the Page ui but setting variables was really starting to bug me. I could get it too work using crmForm but I wanted to move on and use the new code.

I think the problem is highlighted looking at the new Javascript layout from the technet really shows how Microsoft have split the functionality into logical sections, The picture from Technet shows the logical groups of functionality

The UI bit makes sense but looking at this I couldn’t see how to get the actual fields!!!

there is also a big difference with the actual Javascript you write everything has to get something



once you get the variable you can then getValue or setValue

Xrm.Page.getAttribute(“hosk_display2”).setValue(“HOSK SET THIS CRM 2011 STYLE”);


JavaScript CRM 4 to CRM 2011 converter tool:

Here are some common Javascript functions you will need

Get the value from a field

Xrm.Page.getAttribute(“hosk_field”).getValue() ;

Set the value on a field

Xrm.Page.getAttribute(“hosk_field”).setValue(“new value”) ;

Setting requirement level for a field


There are three requirement levels “none”, “required”, “recommended”.  Required means it must be filled in (red asterisk), recommended is the blue cross and none is obvious.  It’s useful to be able to change the requirement settings dynamically because you can then make users fill in fields after they changed another field or make fields not required.

Hide/show tab


hide = setVisable(false)

show = setVisable(true)

You can hide and show a section as well

Some interesting Javascript functions  from this blog tidbits at this blogs:

Call the onchange event of a field


Get the selected value of picklist


Set the focus to a field


Stop an on save event
event.returnValue = false;

Return array of strings of users security role GUIDs:


god bless the boys from Power Objects

74 thoughts on “CRM 2011 – Javascript Xrm.Page Basics

  1. Jon March 16, 2011 / 6:37 am


    You might want to update your post to correct the casing here:




  2. Hosk March 16, 2011 / 8:51 am

    updated, thanks for the comment. Syntax changes can be very frustrating


  3. Val April 13, 2011 / 7:29 pm

    Can anyone tell me how to setValue to a variable?
    I’m trying to create Name as the person’s full name. I don’t know how to get lookupFirst + ‘ ‘ + lookupLast into the “name” attribute.

    Here is what I have so far.

    var lookupLast = Xrm.Page.getAttribute(“new_lastname”).getValue();
    var lookupFirst = Xrm.Page.getAttribute(“new_firstname”).getValue();



  4. Hosk April 13, 2011 / 11:47 pm

    there is an example below
    Xrm.Page.getAttribute(“po_CRMFieldSchemaName”).setValue(‘My New Value’);

    but for your example

    Xrm.Page.getAttribute(“name”).setValue(‘Hosk Name’);

    Xrm.Page.getAttribute(“name”).setValue(lookupFirst + ‘ ‘ + lookupLast);

    my advice would be press F12 and debug the javascript, this will show you if the syntax is correct and tell if you anything is failing with a syntax error


  5. Alex May 16, 2011 / 11:15 am

    Many thanks for sharing this Hosk. In crm 4.0 you could change the display formatting of attributes e.g. = “center”;
    crmForm.all.new_copyaddress1button.vAlign = “middle”;
    crmForm.all.new_copyaddress1button.attachEvent(“onmousedown”, color1);
    crmForm.all.new_copyaddress1button.attachEvent(“onmouseup”, color2);

    like the example at
    Any idea how to do this in Crm 2011? Thanks in advance.


  6. Houseedition May 19, 2011 / 12:22 am

    Hi Hosk,

    Do you know how can I get the records in a subgrid?

    Xrm.Page.ui.controls.get(‘subgrid) return null

    I hope somebody can help me.


  7. swazimodo May 19, 2011 / 5:26 pm

    can you show and hide controls the same way you did with the tabs?




    • mlp July 19, 2011 / 5:54 pm

      could help me with the code to hide and display sections?


  8. Bhanu Bharadwaj June 1, 2011 / 9:58 am

    Hi Every one,
    I m new in CRM World, Can u Pls tell me that where can we write code in java script for a field
    in CRM 2011.


    • Hosk June 1, 2011 / 10:19 am

      you need to add a jscript file to the web resources folder and then add that javascript file to the entity when editing the entity form


      • Jim Daly June 6, 2011 / 3:05 pm

        I’m pretty sure you didn’t mean to actually add a file to the web resources folder. You should add a Jscript library as a JScript Web resource and then include that library in the form.

        Also, be sure to check out the CRM SDK solution to get IntelliSense for JScript form libraries. This is summarized at JScript IntelliSense for Form Script Libraries.

        This project provides IntelliSense and in-line documentation that should help you understand how to write scripts using Xrm.Page.


  9. Batman August 22, 2011 / 5:31 am

    // If I have a control ‘myCTL’ that is null, I want to hide the section it’s in.
    // (probably zero business value, but the example works)

    var myCTL = Xrm.Page.getControl(“myCTL”);
    var myATTR = myCTL.getAttribute();
    var myAttrVal = myATTR.getValue();

    myCTL.setVisible(false); //<— sets the control visibility to false
    myCTL.getParent().setVisible(false); //<— sets the visibility of the control's PARENT (e.g., the section) to false

    //hope this helps.


  10. Martin September 1, 2011 / 1:24 pm

    I try to get value from a field but everytime I get this error:
    “Unable to get value of the property ‘getValue’: object is null or undefined”.

    I am using Xrm.Page.getAttribute(“myfield”).getValue().


    • Hosk September 1, 2011 / 2:28 pm

      You have to remember to use the actual name of the variable e.g. new_testvariable and not the display name.

      here is an example

      Also has the field actually got a value, you should check to see if the variable is null before getting the value

      one piece of advice is to press F12 on the form and then debug the script. you can then step through the code

      if you assign it too a variable

      var hosk = Xrm.Page.getAttribute(“meta_contacttype”).getValue();

      you can then view what the variable is in the local variables display.

      The only other thing I can think of if it’s maybe a type of variable where you wouldn’t use a getValue and you should look up the javascript cheat sheet


      • Martin September 2, 2011 / 11:26 am

        Thanks for hint!
        The issue was related to wrong type of variable… so simple:)


      • Bryce September 12, 2011 / 10:25 pm

        I am having the same problem getting the value from a field in the header of a form. I have looked at the cheat sheet and even spit out all the attributes on the page to see if I have the name wrong but it doesn’t show up there either. Is there something special I have to do to get header values?


      • Hosk September 12, 2011 / 10:35 pm

        I think the header is different from the form variables.

        I think the header is set before the rest of the form variables.

        I’m not sure how you set it, I think you will have to do some more googling to find that answer


  11. D September 15, 2011 / 9:14 am

    I have seen the same blogpost running around on the crm circle about attaching a function to the onRefresh event of a Form sub-grid. but can the records from one Sub-grid(only related records) be fetched and displayed on another entities subgrid?


  12. Metaphorix September 21, 2011 / 2:23 am

    Excellent blog entry. The Javascript changes are great once you get used to them


  13. Saulo Ramon September 30, 2011 / 7:24 pm

    How do I use the function uppercase () in crm11? Where can I find a library with functions for javascript crm11?


      • Saulo Ramon October 3, 2011 / 8:30 pm

        I found lots of interesting things but nothing about uppercase (). This was the function that used in CRM 4.0 in the subject field:
        var = oField event.srcElement;
        oField.value oField.value.toUpperCase = ();
        How do I do this in CRM 11??


    • Saulo Ramon October 5, 2011 / 6:52 pm

      I managed to apply a function that resolved several outstanding issues that had in my customizations. Created a JScript library containing the function below:

      var oField = event.srcElement;
      oField.value = oField.value.toUpperCase();

      Hook up the OnChange Event to the JScript Library Function.


      • eve October 31, 2011 / 4:32 am

        hi Saulo Ramon,
        I tried this but eventually when I save the form, the uppercase is not saved into db, the page when reload returns to small case.


  14. peter October 5, 2011 / 10:54 am

    How can I dynamically add a checkbox to the ui.controls collection?


  15. Sandy December 8, 2011 / 2:44 pm

    hi ,
    kindly i need to know how to create a unique field that never has duplicate values in CRM 2011.
    I’ve found the below answer on the net, can anyone give me the right code please.
    “Develop validation plugin which will handle Create message in pre-mode and validate that those unique field is really unique”


      • Sandy December 11, 2011 / 8:13 am

        Thanks alot for your fast reply Hosk, my problem is that i dont need an autonumbering solution … i already have serial numbers on products , i just need to use them once in the system without being duplicated .. so i needed a javascript code to check the serial number before saving .
        But realllly thankss alot for your help. and i’ll try searching more in CRM forums


  16. athulmt February 9, 2012 / 4:40 am

    Hi Could anyone tell how i debug the JavaScript for CRM 2011 and whether any tools are available for debugging JavaScript ?
    Thanks 🙂


  17. Anni February 22, 2012 / 9:53 am

    Hi all.

    I try to get value from a field, i try using below code but its not getting value of text field.

    var phone = Xrm.Page.ui.controls.get(“TestMobile”).getvalue();
    Here TestMobile is display name of the field.


    • Hosk February 22, 2012 / 10:12 am

      why don’t you try getting an attribute rather than a control

      Xrm.Page.getAttribute(“name”).getValue(‘Hosk Name’);


      • Anni February 22, 2012 / 10:19 am

        Thanks for your response Hosk.. Im new bud in CRM.Actually as the input is given i need to change the field value. here is Javascript function is called by onchange Event
        function validatePhone()

        var phone = Xrm.Page.getAttribute(“new_testmobile”).getvalue();
        phone.value=phone.value.substr(0, 3) + “-” + phone.value.substr(3,10);


        new_testmobile is name of the field.Any suggestion or correct me if im wrong


      • Hosk February 22, 2012 / 10:27 am

        is the var phone variable set with a value, does the field have a value in it?

        I don’t think you need to put phone.value, just phone would do.

        why have you got extra brackets in there?

        I would press F12 on the form and start debugging the javascript by putting a breakpoint on the line

        var phone = Xrm.Page.getAttribute(“new_testmobile”).getvalue()

        start with the basics, first try and assign the field value to a variable.

        Also look at the CRM SDK for examples.


  18. Anni February 22, 2012 / 10:37 am

    Okay. Im facing a error in script INVALID Character in the line, character 36 near “new”

    var phone = Xrm.Page.getAttribute(“new_testmobile”).getvalue().


  19. Edson February 23, 2012 / 7:22 pm

    I uncheck the check of a checkbox that is inside a tab sections, I have something like this:

    var tabGeneral=Xrm.Page.ui.tabs.get(“tab1”);
    var seccionCheck=tabGeneral.sections.get(“section1”);


    but i can’t. appreciate your comments.


    • Hosk February 24, 2012 / 2:08 am

      if you want to just set a field value then just set the field value



      • Edson February 28, 2012 / 5:19 pm

        I can’t because my checkbox is in the section, for this i access to seccionCheck.controls.get, do you understand?

        PD : im sorry, i don’t speak english so much.


  20. Helecho March 6, 2012 / 2:55 pm

    Hello Hosk!
    I’m looking for SOAP/fetchxml code (CRM 2011) that do UPGRADE to specify field.

    Thank you, advance!


    • Helecho March 6, 2012 / 3:00 pm

      Hello Hosk!
      I’m looking for SOAP/fetchxml code (CRM 2011) that does UPGRADE to specific field.

      Thank you, advance!

      (fixed version SORRY!)


  21. kamal April 2, 2012 / 11:54 am

    s’il vous plait je veux un peu d’aide sur comment intégrer bing maps dans crm2011 avec l’affichage d’un point sur la carte avec l’attitude et longitude dans le même temps faire dessiner un polygone en reliant ces points entre eux merci pour votre aide


  22. kamal April 3, 2012 / 9:48 am

    Please I want some help on how to integrate bing in crm2011 maps with display of a point on the map with longitude and attitude at the same time to draw a polygon by connecting the dots between them thank you for your help


  23. iambigred April 26, 2012 / 4:46 pm

    Loads of the segments of JavaScript you have posted are incorrect! Your blogging platform (WordPress?) has changed any quotes to left and right angled quotes, which are invalid in JavaScript!


  24. Anonymous May 5, 2012 / 5:44 am

    Hi i am new to crm 2011.
    i have a problem. i have set a field value default during on load . but i want to change the value dynamic during ON SAVE event, can u send me java script code for it.

    this is my java script
    Xrm.Page.getAttribute(“po_CRMFieldSchemaName”).setValue(‘My New Value’);


  25. rama September 21, 2012 / 5:30 am

    How to get label of radio button options in javascript in DCRM 2011 and how to change the labels onload evrytime?? ex;- if i have created an optionset with 2 options yes,no. and i want yes label to be changed to some number onload of this form??


  26. Ola July 26, 2013 / 4:52 pm

    How Can I map field for a custom field in order to transfer data from one source entity attribute to the target entity attribute in CRM.


  27. firoze October 25, 2013 / 5:44 am

    By default i kept all tabs in collapse state. After page load i need to expand them, and i need the syntax code for all tabs, for not place in each and every tab. I kept for loop and looped it, but i need the count of all tabs by default. and if tabs are increased or decreased the code must not be changed. So please tell me how to get the count of tabs in a form.


  28. Muneeb January 31, 2014 / 12:10 pm

    Can you please tell me how can i align my fields of a tab on index change i want to hide or show some fields on form i done it but they are not properly allign…


  29. sell domain name March 5, 2014 / 8:10 pm

    My brother suggested I would possibly like this
    web site. He was once totally right. This put up actually made my day.
    You cann’t imagine just how much time I had spent for this information!
    Thank you!


  30. May 31, 2014 / 10:03 pm

    It took me a long time to figure out that which you were talking about.


  31. Amit Goswami August 21, 2014 / 1:01 pm

    hi! sir

    What about if we want 2 fields hide on the selection of the picklist(option set) purticular value .


  32. safna August 26, 2014 / 11:41 am

    Can anyone tell me how to get the id of a primary entity in related entity. For eg how to get order ID in order Product ID.


  33. Firoz Rahman Shaik April 30, 2015 / 2:08 pm

    How to set value to a field located in footer of the form……?


    • Hosk April 30, 2015 / 2:38 pm

      You can change the value like you would changing any other field. The only problem is the header and the footer won’t display the changed value until the form is refreshed


  34. Anu October 7, 2015 / 3:47 pm

    Hello Hosk,

    Is there a way to auto populate fields when there is one lookup field in one entity and the second entity has a text field.

    For Example: I have two entities A and B. Entity B has a lookup field on Entity A, and if the lookup field matches the field in Entity A then it should auto populate the rest of the fields in Entity B such as name, address etc..


Leave a Reply

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

You are commenting using your 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.