CRM 2011 Useful JavaScript tidbits

One of the noticeable changes between CRM 4 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 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);
            }
        }
    }
}
This entry was posted in CRM 2011 and tagged , , . Bookmark the permalink.

22 Responses to CRM 2011 Useful JavaScript tidbits

  1. Alper says:

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

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

  2. Dev says:

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

  3. hosk says:

    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?

  4. Mike Karls says:

    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?

    • alexf says:

      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 says:

      you can hide the section in one line through this

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

  5. Rob says:

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

  6. Rob says:

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

  7. Tom says:

    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

  8. Ajmal says:

    var originalValue = crmForm.all.contosa_name.defaultValue;

    what is the equivalent value in CRM 2011

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

  10. jz says:

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

  11. Tim says:

    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.

  12. Daniel says:

    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?

  13. Yao says:

    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?

  14. manoj says:

    how to hide attribute on particular date in crm 2011

  15. Atul says:

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

  16. Surya says:

    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

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

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

  19. Fahmeeda Yaseen says:

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

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>