JavaScript Basics

Through the years that I’ve been working with MS Dynamics CRM JavaScript, I started to take notes of the basic commands that were usefull to me at some point.  Today, I would like to share these notes so hopefully they can help you. I’ll be updating this post frecuently each time I find something interesting or helpfull. Hope it helps!

Code

//-----------------------
// Printing info
//----------------------- 

// Print in console (As recommended in the SDK)
console.log("HELLO WORLD");

// Popup that will show the string send as parameter
alert("HELLO WORLD");

//-----------------------
// Notifications
//----------------------- 

/* Parameters:
* First parameter is the text you want to display
* Second parameter is to set the Level, these could be: INFO, ERROR, WARNING
* Third parameter is just an ID for the message
*/
 Xrm.Page.ui.setFormNotification("This is a notification!", "INFO", "1");

//-----------------------
// Ribbon
//-----------------------

// Refresh ribbon
Xrm.Page.ui.refreshRibbon();

//-----------------------
// Get / Set Fields
//-----------------------

// Get: Field
var Field = Xrm.Page.data.entity.attributes.get("FIELD_NAME");

// Get: Field value
var Field_Value = Xrm.Page.data.entity.attributes.get("FIELD_NAME").getValue();

// Set: Field Value (String)
var Field_Value = Xrm.Page.data.entity.attributes.get("FIELD_NAME").setValue("HELLO WORLD");

// Set: Field Label
Xrm.Page.ui.controls.get("FIELD_NAME").setLabel("EXAMPLE");

//-----------------------
// Set: Focus
//-----------------------
Xrm.Page.ui.controls.get("FIELD_NAME").setFocus();

//-----------------------
// Fire on change event for a field
//-----------------------
Xrm.Page.getAttribute("FIELD_NAME").fireOnChange();
//-----------------------
// Show / Hide Field
//-----------------------

// Show Field
Xrm.Page.ui.controls.get("FIELD_NAME").setVisible(true);

// Hide Field
Xrm.Page.ui.controls.get("FIELD_NAME").setVisible(false);

//-----------------------
// Enable / Disable fields
//-----------------------

// Enable Field
Xrm.Page.ui.controls.get("FIELD_NAME").setDisabled(false);

// Disable Field
Xrm.Page.ui.controls.get("FIELD_NAME").setDisabled(true);

//-----------------------
// Set: Field Requirement Level
//-----------------------

//  Not required
Xrm.Page.getAttribute("FIELD_NAME").setRequiredLevel("none");

// Business recommended
Xrm.Page.getAttribute("FIELD_NAME").setRequiredLevel("recommended"); 

// Required
Xrm.Page.getAttribute("FIELD_NAME").setRequiredLevel("required");

//-----------------------
// Option Set features
//-----------------------

// Get: Option Set
var Option_Set = Xrm.Page.data.entity.attributes.get("FIELD_NAME");

// Get: Option Set / Selected Option
var OptionSet_Selcted_Option = Xrm.Page.data.entity.attributes.get("FIELD_NAME").getValue();

// Get: Option Set / Options
var OptionSet_Options = Xrm.Page.data.entity.attributes.get("FIELD_NAME").getOptions();

// Get: Option Set / Label text
var OptionSet_Label_Text = Xrm.Page.data.entity.attributes.get("FIELD_NAME").getText();

//-----------------------
// Lookup features
//-----------------------

// Get: Lookup
var Lookup_Field = Xrm.Page.data.entity.attributes.get("FIELD_NAME");

// Get: Lookup / ID
// To avoid possible errors, verfy that the lookup has value with:
//   If(Lookup_Field != null)
var Lookup_ID = Xrm.Page.data.entity.attributes.get("FIELD_NAME").getValue()[0].id;

// Get: Lookup / Name
// To avoid possible errors, verfy that the lookup has value with:
//   If(Lookup_Field != null)
var Lookup_Name = Xrm.Page.data.entity.attributes.get("FIELD_NAME").getValue()[0].name;

// Set: Lookup / Value
var Lookup_Value = new Array();
Lookup_Value[0] = new Object();
Lookup_Value[0].entityType = "ENTITY_NAME";
Lookup_Value[0].id = YOUR_ID; // YOUR_ID = ID Of the record
Lookup_Value[0].name = TEXT; // TEXT =  Text to be set in Lookup
Xrm.Page.getAttribute("FIELD_NAME").setValue(Lookup_Value);

// Add Presearch filter to a lookup
Xrm.Page.getControl("FIELD_NAME").addPreSearch(function () {
// This method should build a Fetch XML that will point to the same
// Entity to which the lookup is pointing to. Once the FetchXML is
// done, add it as prefilter with:
// Xrm.Page.getControl("FIELD_NAME").addCustomFilter(Fetch_XML);
Filtering_Method();
});

//-----------------------
// Tabs
//-----------------------

// Get: Tab
var Tab = Xrm.Page.ui.tabs.get("TAB_NAME");

// Show
Tab.setVisible(true);

// Hide
Tab.setVisible(false);

// Collapse
Tab.setDisplayState("collapsed");

// Expand
Tab.setDisplayState("expanded"); 

/*
* Sections
*/

// Get: Tab / Section
var Section = Tab.sections.get("SECTION_NAME");

// Show Section
Section.setVisible(true);

// Hide Section
Section.setVisible(false)

//-----------------------
// Record Properties
//-----------------------

// Get: Record / Entity Name
var Entity_Name = Xrm.Page.data.entity.getEntityName();

// Get: Record / ID
var Record_ID = Xrm.Page.data.entity.getId();

//-----------------------
// User Properties
//-----------------------

// Get: User / ID
var User_ID = Xrm.Page.context.getUserId();

// Get: User / User Roles
var User_Roles = Xrm.Page.context.getUserRoles();

//-----------------------
// Form Properties & Utilities
//-----------------------

// Save changes made in record
Xrm.Page.data.entity.save();

// Save and Close Form
Xrm.Page.data.entity.save("saveandclose");

// Close window
Xrm.Page.ui.close();

// Check if Form is Dirty. If so, true. Otherwise, false.
var Is_Form_Dirty = Xrm.Page.data.entity.getIsDirty();

// Get: Form / Type
// Form Types:
//             1 - Create
//             2 - Update
//             3 - Read Only
//             4 - Disabled
//             6 - Bulk Edit
var Form_Type = Xrm.Page.ui.getFormType();

//-----------------------
// URL
//-----------------------      

// Get: Server URL
// Only for CRM 13
var Server_URL = Xrm.Page.context.getServerUrl();

// Get: Server URL
// Only for CRM Online
var Server_URL = XXrm.Page.context.getClientUrl();

// Get: Organization / Name
var Organization_Name = Xrm.Page.context.getOrgUniqueName();

//-----------------------
// Multiple utilities
//-----------------------

/*
* Set Form based on its Name
*@Form_Name - Name of the form to be set
*/
function Set_Form(Form_Name)
{
    // Check if current Form has value
    if (Xrm.Page.ui.formSelector.getCurrentItem() != null)
    {
        // Get: current Form / Name
        var Current_Form = Xrm.Page.ui.formSelector.getCurrentItem().getLabel();

        // If current Form is not the form we're looking for, then...
        if (Current_Form != Form_Name)
        {
            // Get: all Entity Forms
            var EntityForms = Xrm.Page.ui.formSelector.items.get();

            // Iterate on all Forms of the Entity
            for (var i in EntityForms)
            {
                // Get Form Item
                var FormItem = EntityForms[i];

                // If Form Item is not null, then...
                if (FormItem != null) {

                    // Get Form ID and Name
                    var Form_Id = FormItem.getId();
                    var FormItem_Name = FormItem.getLabel();

                    // If Form Item equals Target Form, then...
                    if (FormItem_Name == Form_Name)
                    {
                        // Navigate to the Form
                        FormItem.navigate();
                    }
                }
            }
        }
    }
}

/*
* Add OnRefresh Event to subgrid in CRM 13
*/
function AddEventToSubgrid() {
    console.log("Getting subgrid...");
    var Subgrid = document.getElementById('SUBGRID_NAME');

    if (Subgrid == null || Subgrid == "undefined") {
        console.log("Subgrid not found, trying again in 1 sec...");
        setTimeout(AddEventToSubgrid, 1000);
        return;
    }

    console.log("Grid Found. Adding on refresh method...");
    Subgrid.control.add_onRefresh(YOUR_FUNCTION_NAME);

    console.log("OnRefresh added");
}

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

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

w

Connecting to %s