One of the noticeable changes between CRM 4.0 and CRM 2011 is the JavaScript object model. It has changed a bit. Below are some of the commonly used functions used to manipulate CRM forms. Let us know if you have a commonly used function that should be included in the list of CRM 2011 useful JavaScript tidbits below.

One of the tools we highly recommend is the JavaScript CRM 4 to CRM 2011 converter tool:

http://crm2011scriptconvert.codeplex.com/
Here are common tidbits:

Get the value from a CRM field

var varMyValue = Xrm.Page.getAttribute(“CRMFieldSchemaName”).getValue() ;

Set the value of a CRM field

Xrm.Page.getAttribute(“po_CRMFieldSchemaName”).setValue(‘My New Value’);

Hide/Show a tab/section

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

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 requirement level

Xrm.Page.getAttribute(“CRMFieldSchemaName”).setRequiredLevel(“none”);
Xrm.Page.getAttribute(“CRMFieldSchemaName”).setRequiredLevel(“required”);
Xrm.Page.getAttribute(“CRMFieldSchemaName”).setRequiredLevel(“recommended”);

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

Hide/Show Tabs and Sections

function setVisibleTabSection(tabname, sectionname, show) {
    var tab = Xrm.Page.ui.tabs.get(tabname);
    if (tab != null) {
        if (sectionname == null)
            tab.setVisible(show);
        else {
            var section = tab.sections.get(sectionname);
            if (section != null) {
                section.setVisible(show);
                if (show)
                    tab.setVisible(show);
            }
        }
    }
}
  • Alper

    Xrm.Page.ui.tabs.get(5).SetVisible(false); doesn’t work

    Xrm.Page.ui.tabs.get(5).setVisible(false); it’s working

    • uma

      it will work ….
      crmonce.com

  • Dev

    // Hide or Show field based on selected option

    function fnc_Hide_Show_Field()
    {
    var Option = Xrm.Page.getAttribute(“CRMFieldSchemaName”).getSelectedOption().text;
    if (Option == “Option_xyz”)
    //example: based on the option “Individual” show field relevant to Individual
    //or based on the option “Team” show field relevant to Team.
    {
    Xrm.Page.getControl(“VisibleField_CRMFieldSchemaName”).setVisible(true);
    Xrm.Page.getControl(“HiddenField_CRMFieldSchemaName”).setVisible(false);
    }
    }

    //Happy CRM 2011

  • http://crmbusiness.wordpress.com hosk

    you legends. I have been looking at the CRM 2011 Javascript SDK, samples. Comparing it to CRM 4 code and getting very angry.

    I could hide stuff ok 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.

    Finally I found you blog, awesome.

    Also how can you subscribe to the blog on an RSS feed?

  • Mike Karls

    How do you hide a section? I have a section called Vision but I am unable to hide it. I can hide a tab with Xrm.Page.ui.tabs.get(tabnumber).setVisible(visible); but I cannot hide a section using this same syntax. Can anyone help?

    • http://www.PowerObjects.com alexf

      Hi Mike,

      Here’s the function you need:

      function setVisibleTabSection(tabname, sectionname, show) {
      var tab = Xrm.Page.ui.tabs.get(tabname);
      if (tab != null) {
      if (sectionname == null)
      tab.setVisible(show);
      else {
      var section = tab.sections.get(sectionname);
      if (section != null) {
      section.setVisible(show);
      if (show)
      tab.setVisible(show);
      }
      }
      }
      }

    • Abhishek

      you can hide the section in one line through this

      Xrm.Page.ui.tabs.get(“tab_name”).sections.get(“section_name”).setVisible(false);

  • Rob

    You guys are awesome! Thank you so much.

    I’ve been trying to hide a navigation submenu which gets renamed with document.getElementById('_NA_MA')
    However I’ve been unable to hide it completely.

    The function being used to rename is as follows in case it helps anyone:

    /** Renames Nearside Navigation Pane options. **/
    cmnLeftNavBarRename = function(navBarEntry, newName) {
    if (navBarEntry != null) {
    var navBarEntryName=navBarEntry.getElementsByTagName("NOBR");
    navBarEntryName[0].innerHTML = newName;
    }
    }

    and it is called by cmnLeftNavBarRename(document.getElementById('_NA_MA'),'My New Name');

  • Rob

    I just found something that works for me, but now am wondering if there is a better way.
    What I found is document.getElementById(‘_NA_MA’).style.display = “none”;

  • Tom

    Hi Guys,
    This may seem like a really simple issue, but I’m struggling to use any of the above examples to grab the text value of a “Single Line of Text” field.

    Basically, i need to grab the text value of “Topic” in an opportunity – then add the text value of “Potential Customer” to the end of it…

    Tried to use…

    var topicText = Xrm.Page.getAttribute(“Name”).getValue();

    …to set my variable, but I get errors saying the system doesn;t understand the “getValue” part.

    If anyone can point me in the right direction that’d be great.

    Thanks

    Tom

  • Ajmal

    var originalValue = crmForm.all.contosa_name.defaultValue;

    what is the equivalent value in CRM 2011

  • http://bingsoft.wordpress.com Rhett Clinton

    Try my CRM 4 to CRM 2011 JavaScript Converter tool on Codeplex, it might help you. http://bingsoft.wordpress.com/2010/09/09/crm-4-to-crm-2011-javascript-converter-tool/

    Cheers,
    Rhett

  • jz

    hi,
    any one knows the equivalent of this in 2011
    $(“span.ms-crm-Form-Title”).append(tittle);

  • Tim

    How would you set a property on one form based on a related record field value. For example: Make a field Read-Only on the “Quote Product” from based on the value of the isSubmitted field on the related “Quote” record. Note: I cannot use “parent” to enable this as it goes through a view in between.

  • Daniel

    I’m having trouble getting a Telephone number field within the Case Entity to pull through the Telephone number from the Contact Entity based on the contact that has been selected.

    Does anyone have any ideas?

  • Yao

    Hi, i’ve 2 forms in an entity, and i want the form to auto change to another base on the value in one field. I’ve tried Xrm.Page.ui.formSelector.items.get(1).navigate(); , but the ribbon is diabled after the form load to another. Any idea?

  • manoj

    how to hide attribute on particular date in crm 2011

  • Atul

    Hi,
    How to freeze “lookup for” picklist of lookup record window. I don’t want to show other entities in that picklist.

  • Surya

    Hi,
               i am trying to hide some fields based on a picklist value. I get the following error
    Xrm.Page.getAttribute(…) is null or not an object
    i have added the complete script below for your reference
    function form_onchange()
    {
    var facility = Xrm.Page.getControl("new_Facility");
    var volume = Xrm.Page.getControl("new_Volume");
    var rentalarea = Xrm.Page.getControl("new_RentalArea");
    var areaunit = Xrm.Page.getControl("new_AreaUnit");
    var commitment = Xrm.Page.getControl("new_Commitment");
    var commitmentunit = Xrm.Page.getControl("new_CommitmentUnit");
    var levelrate = Xrm.Page.getControl("new_LevelRate");
    var linecode = Xrm.Page.getControl("new_LineCode");
    var amount = Xrm.Page.getControl("new_Amount");
    var amounttype = Xrm.Page.getControl("new_AmountType");
    var acctype = Xrm.Page.getAttribute
     
    ("new_AgreementType").getSelectedOption().text;
     
    if(acctype =="Lease Agreement")
    {
    volume.setVisible(false);
    commitment.setVisible(false);
    commitmentunit.setVisible(false);
    levelrate.setVisible(false);
    linecode.setVisible(false);
    }
    else
    if(acctype == "General Cargo Agreement") { volume.setVisible(false); commitment.setVisible(false); commitmentunit.setVisible(false); levelrate.setVisible(false); linecode.setVisible(false); }
    else
    if(acctype =="Volume Agreement") { facility.setVisible(false); rentalarea.setVisible(false); areaunit.setVisible(false); amount.setVisible(false); amounttype.setVisible(false); } }
    Thanks,
    Surya N

    • Akhila

      you are not passing the attribute for the acctype,u try it.
      var acctype = Xrm.Page.getAttribute("new_acctype");
       

  • Pingback: Use a Dialog Process in Microsoft Dynamics CRM to replace the Case Resolution Form - PowerObjects- Bringing Focus to Dynamics CRM - CRM Technical Blogs - Microsoft Dynamics Community

  • Pingback: CRM 2011 – Javascript Xrm.Page Basics « Srikanth Reddy's Blog

  • Fahmeeda Yaseen

    Xrm.Page.getControl(“CRMFieldSchemaName”).setFocus(true);
    I use this JavaScript.but this not worked for to set the focus on the desire field. any one have another solution????

  • Parthiban.T
  • Leanne

    Hi I am trying to fill a custom field on one form from the custom field of the other. Do I need to try to use a SOAP envelope for this or is there an easier way? Thanks.

    • Alex Fagundes

      Hi Leanne – Yep – you would have to use soap to get the field form entity A and then set it on entity B. Once you do the first few of these, it is really straight foward code.

  • Anonymous

    i am trying to get a value from (whole number & Currency Filed) and to set that value in text Field I have tried (i) parseInt , parseFloat ,.
    But itz not working.. Can any one plz tel how to do this ….

  • http://www.daveenjoys.com/ Dave Mackey

    Can you show how one would call upon the creation of a new activity from the underlying contact record an autopopulation of a field – say a phone number?

  • Pingback: Populating Field Values in a Form Using a Query String in CRM 2011 - PowerObjects- Bringing Focus to Dynamics CRM - Microsoft Dynamics CRM - Microsoft Dynamics Community

Return to Top ▲Return to Top ▲