Looking for PowerObjects? Don’t worry, you’re in the right place! We’ve been part of HCL for several years, and we’ve now taken the final step in our acquisition journey: moving our website to the HCL domain. Nothing else is changing – we are still fanatically focused on Microsoft Business Applications!

PowerObjects Blog 

for Microsoft Business Applications


Useful CRM 2011 JavaScript Tidbits

Post Author: Joe D365 |

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 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);
            }
        }
    }
}
Joe CRM
By Joe D365
Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.

39 comments on “Useful CRM 2011 JavaScript Tidbits”

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

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

  2. // 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. 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. 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?

    1. 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);
      }
      }
      }
      }

    2. you can hide the section in one line through this

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

  5. 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. 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. 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. var originalValue = crmForm.all.contosa_name.defaultValue;

    what is the equivalent value in CRM 2011

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

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

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

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

  13. Hi,
    How to freeze "lookup for" picklist of lookup record window. I don't want to show other entities in that picklist.

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

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

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

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

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

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

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

  19. How to get the id of a primary entity in related entity. For eg how to get order ID in order Product ID.

  20. Trying to hide a tab and show another if the value of OptionSelect is Vendor and then do the same if Distributor problem is it hides one and shows both tabs as if it is ignoring the if statements am i missing something obvious here?

    function Information() {
    var RelationshipType = Xrm.Page.getAttribute("customertypecode").getSelectedOption().text;
    if (RelationshipType == "Vendor"); {
    Xrm.Page.ui.tabs.get("CompanyInformation").setVisible(false);
    Xrm.Page.ui.tabs.get("VendorInformation").setVisible(true);
    }
    if (RelationshipType == "Distributor"); {
    Xrm.Page.ui.tabs.get("CompanyInformation").setVisible(false);
    Xrm.Page.ui.tabs.get("DistributorInformation").setVisible(true);
    }
    }

    1. Hi Chris - to help quickly debug, add an alert ( RelationshipType ); Right after you get it. this will show if your names are correct.

      1. thanks Joe through some tinkering i found that the issue was the semi-colons after "Vendor") and "Distributor") removing them and it works as required.

  21. Alright, but how to retrieve an option set (all items) via JavaScript in a form's OnLoad, that aren't related to the entity on the form?

    1. Hi - If you want to retrieve all options sets from an unreleated option set.....something NOT on the form....then you will need to use the sdk and do a 'soap call'. However, if you need this, why not just add the option set to the form and keep it hidden?

  22. Hi,
    I have a problem about opening new form with default value of some fields in MS CRM 2011. I use this JavaScript and also i define that field in parameter section of second form, but that form opens with error message:
    please help me about this problem.
    Thanks,
    Rey

    function paymentrequest()
    {
    //get formno
    var formno= Xrm.Page.data.entity.attributes.get("new_formno").getValue();
    //define default values for new Incident record
    var parameters = {};
    parameters["new_purchasrequestno"] = "formno";
    //pop paymentrequest form with default values
    Xrm.Utility.openEntityForm("new_paymentrequest",null,parameters);
    }

    1. HI Rey - IN this line parameters["new_purchasrequestno"] = "formno"; you are setting your parameter to the text "formno". I'm assuming you want to set this to the actual value of your variable right? If so, then no need for the quotes. I would recommend starting with a simple js were you hard code the parameter. Then once that is working ok, get the parameter value from your field.

  23. i want to Retrieve Solution (field) and offering (field) on the order item form when i select product (field) where the 3 fields are lookup fields

  24. Hi,

    I have a wizard with 2 steps and I want to make a field mandatory at first step so that I can't be able to pass to the second step until I have this field complete.
    I tryied with an alert message but it's not enought. I click on the OK button from the message box and then I can pass to teh second step without filling in the mandatory field.
    Thanks,
    Anca

  25. Thanks! Very useful page!
    But I have an error while executing this --> Xrm.Page.ui.tabs.get(5).SetVisible(false);
    Error message says that SetVisible is not a correct function.
    The reason is function name is case sensitive and it needs not "SetVisible" but "setVisible".

PowerObjects Recommends