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

Xrm.Page.getAttribute(“hosk_display2”)

Xrm.Page.ui.tabs.get(“hosk_tab1”)

once you get the variable you can then getValue or setValue

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

Xrm.Page.ui.tabs.get(“hosk_tab1”).sections.get(“Permanent”).setVisible(false);

JavaScript CRM 4 to CRM 2011 converter tool:

http://crm2011scriptconvert.codeplex.com/

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

Xrm.Page.getAttribute(“hosk_field”).setRequiredLevel(“none”);

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

Xrm.Page.ui.tabs.get(“hosk_tab1”).sections.get(“Permanent”).setVisible(false);

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

Xrm.Page.getAttribute(“CRMFieldSchemaName”).fireOnChange();

Get the selected value of picklist

Xrm.Page.getAttribute(“CRMFieldSchemaName”).getSelectedOption().text;

Set the focus to a field

Xrm.Page.getControl(“CRMFieldSchemaName”).setFocus(true);

Stop an on save event
event.returnValue = false;

Return array of strings of users security role GUIDs:

Xrm.Page.context.getUserRoles()

god bless the boys from Power Objects

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

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

    Hi,

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

    Xrm.Page.ui.tabs.get(5).setVisible(false);
    Xrm.Page.ui.tabs.get(5).setVisible(true);

    Cheers

    Like

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

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

    Like

  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();

    Xrm.Page.getAttribute(“name”).SetValue?????

    Like

  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

    Like

  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.

    crmForm.all.new_copyaddress1button.style.textAlign = “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 http://crmstuff.blogspot.com/2008/02/create-button-on-your-form.html
    Any idea how to do this in Crm 2011? Thanks in advance.

    Like

  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.

    Like

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

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

    Xrm.Page.ui.tabs.get(5).setVisible(false);
    Xrm.Page.ui.tabs.get(5).setVisible(true);

    Xrm.Page.getAttribute(fieldName).setVisible(false);
    Xrm.Page.getAttribute(fieldName).setVisible(true);

    Like

    • mlp July 19, 2011 / 5:54 pm

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

      Like

  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.

    Like

    • 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

      Like

      • 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.

        Like

  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();

    if(myAttrVal==null)
    {
    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.

    Like

  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().

    Like

    • 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
      Xrm.Page.getAttribute(“meta_contacttype”).getValue();

      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

      http://danielcai.blogspot.com/2011/04/microsoft-dynamics-crm-2011-javascript.html

      Like

      • Martin September 2, 2011 / 11:26 am

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

        Like

      • 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?

        Like

      • 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

        Like

  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?

    Like

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

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

    Like

  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?

    Like

      • 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??

        Like

    • 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.

      Like

      • 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.

        Like

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

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

    Like

  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”

    Like

      • 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

        Like

  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 🙂

    Like

  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.

    Like

    • 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’);

      Like

      • 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

        Like

      • 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.

        Like

  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().

    Like

  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”);

    seccionCheck.controls.get(“mycheckbox”).setValue(false);
    Xrm.Page.getAttribute(“mycheckbox”).setSubmitMode(“always”);

    but i can’t. appreciate your comments.

    Like

    • Hosk February 24, 2012 / 2:08 am

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

      Xrm.Page.getAttribute(“mycheckbox”).setValue(false);

      Like

      • 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.

        Like

  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!

    Like

    • 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!)

      Like

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

    Bonjours,,
    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

    Like

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

    Hi,,
    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

    Like

  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!

    Like

  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’);

    Like

  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??

    Like

  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.

    Like

  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.

    Like

  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…

    Like

  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!

    Like

  30. wordpress.com May 31, 2014 / 10:03 pm

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

    Like

  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 .

    Like

  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.

    Like

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

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

    Like

    • 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

      Like

  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..

    Like

Leave a comment

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