CRM 2011 – Using Javascript to identifying if a form is in Create or Update Contect

Here is a quick tip to tell if a form is being created or updated in Javascript, this can be very useful for form load events and creating code which needs to be run when a form is being created, like setting the values of variables when a form is created.

var  formType= Xrm.Page.ui.getFormType();
getFormType() function returns a number
If the return value is 1 –  Form context is Create
If the return value is 2 –  Form context is Update

Disable Field In Microsoft CRM 2011

Przykład funkcji Disabled Field
1
2
3
4
5
6
7
8
9
function DisabledField(picklistName, fieldName) {
    if (Xrm.Page.getAttribute(picklistName).getSelectedOption() != null) {
        var selectedGenderValue = Xrm.Page.getAttribute(picklistName).getSelectedOption().value;
        if (selectedGenderValue == 1)
            Xrm.Page.ui.controls.get(fieldName).setDisabled(false);
        else
            Xrm.Page.ui.controls.get(fieldName).setDisabled(true);
    }

 

CRM 2011 – Javascript and Subgrids code example

 

Kilka fajnych przykładów związanych z Subgrids

You can inspect the subgrid values on save by doing the following:

var gridControl = document.getElementById('subgrid_id').control; 
var ids = gridControl.get_allRecordIds(); 
 for(i = 0; i < ids.length; i++) 

 var cellValue = gridControl.getCellValue('column_name', ids[i]); 
 // logic 
 } 

Doing this on load is a bit more challenging since subgrids are loaded asynchronously and aren’t likely to be done loading when the form onload event fires. You can check the grid periodically though to see when it’s done loading by calling a function like the following in your form onload:

function subGridOnload() 

var grid = document.getElementById('grid_identifications'); 
 if (grid.readyState!="complete") 

 // delay one second and try again. 
 setTimeout(subGridOnload, 1000); 
 return; 

// logic 
}

I then also found a forum post on attaching events to a subgrid, which you can read here

For CRM 2011, your code should look like this:

if (Xrm.Page.ui.getFormType() != 1) {

  Xrm.Page.ui.controls.get("IFRAME_OpportunityLines").setSrc(GetFrameSource("fj_opportunity_new_opportunitymanagementmodu"));;
  Xrm.Page.ui.controls.get("IFRAME_OpportunityLines").onreadystatechange = function oppLineTotals() {
    if (Xrm.Page.ui.controls.get("IFRAME_OpportunityLines").readyState == 'complete') {
      var iFrame = frames[window.event.srcElement.id];
      iFrame.document.all.crmGrid.attachEvent("onrefresh", GridRefresh);
    }
  }
}

Funny thing, the sdk recomended function did not work for me (i.e.Xrm.Page.ui.controls.get(“GridName”) ), and yet the following works (tested!)

var grid = document.getElementById("GridName");
grid.attachEvent("onrefresh", EventHandlerFunction);

because the grid.htc (Html Component) actually includes a public event “onrefresh”… And this event can be handled with any function. We dont need to dive into the eventManager and scriptEvents.

Microsoft Dynamics CRM 2011 – JavaScript Development

Xrm.Page.context Methods

  • getAuthenticationHeader: Returns the encoded SOAP header for MSCRM 4.0 style Web service calls
  • getCurrentTheme: Returns the current user’s Outlook theme
  • getOrgLcid: Returns the LCID value for the base language of the organization
  • getOrgUniqueName: Returns the unique organizations name
  • getQueryStringParameters: Returns an array of key-value pairs representing the query strings
  • getServerUrl: Returns the base server URL
  • getUserId: Returns the current user’s SystemUser id
  • getUserLcid: Returns the LCID value representing the user’s preferred language
  • getUserRoles: Returns an array of GUID values of the security roles the user is associated with
  • isOutlookClient: Returns a Boolean value indicating if the user is using Outlook client
  • isOutlookOnline: Returns a Boolean value indicating if the user is connected to the server while using MSCRM for Outlook with Offline Access
  • prependOrgName: Prepends the organization name to the specified path

Xrm.Page.data.entity attribute Methods 

  • addOnChange: (Applicable: All) Sets a function to be called when the value is changed
  • fireOnChange: (Applicable: All) Causes the OnChange event to occur on the attribute
  • getAttributeType: (Applicable: All) Returns the type of attribute (string)
  • getFormat: (Applicable: All) Returns formatting options for the attribute (string)
  • getInitialValue: (Applicable: boolean, optionset) Returns the initial value for Boolean or optionset attributes
  • getIsDirty: (Applicable: All) Returns a Boolean value indicating if there are unsaved changes to the attribute value.
  • getMax: (Applicable: money, decimal, integer, double) Returns the maximum allowed value for an attribute (number)
  • getMaxLength: (Applicable: string, memo) Returns the maximum length of an attribute (number)
  • getMin: (Applicable: money, decimal, integer, double) Returns the minimum allowed value for an attribute (number)
  • getName: (Applicable: All) Returns the logical name of the attribute
  • getOption: (Applicable: optionset) Returns an option object by matching its name
  • getOptions: (Applicable: optionset) Returns an array of options for an optionset attribute
  • getParent: (Applicable: All) Returns the parent object to the attribute
  • getPrecision: (Applicable: money, decimal, double, integer) Returns the number of digits allowed after the decimal point
  • getRequiredLevel: (Applicable: All) Returns a string indicating whether the attribute is required or recommended
  • getSelectedOption: (Applicable: optionset) Returns the option selected in an optionset attribute.
  • getSubmitMode: (Applicable: All) Returns a string indicating if the attribute will be submitted when the record is saved
  • getText: (Applicable: optionset) Returns the selected option for an optionset attribute
  • getUserPrivilege: (Applicable: All) Returns an array of Boolean values indicating if the user can create/read/update an attribute’s values
  • getValue: (Applicable: All) Retrieves the data value for an attribute
  • removeOnChange: (Applicable: All) Removes a function from the OnChange event handler
  • setRequiredLevel: (Applicable: All) Sets whether the attribute is required or recommended
  • setSubmitMode: (Applicable: All) Sets whether the attribute’s data will be submitted
  • setValue: (Applicable: All) Sets the data value for an attribute.

Form Event Handler Execution Context Reference 

  • getContext: Returns the Xrm.Page.context object
  • getDepth: Returns a value indicating the order in which this handler is executed
  • getEventArgs: Returns an object with methods to manage the Save event
  • getEventSource: Returns a reference to the object that the event occurred on
  • getSharedVariable: Retrieves a variable set using setSharedVariable
  • setSharedVariable: Sets the value of a variable to be used by a hander after the current handler completes

Xrm.Page.ui control methods

  • addCustomView: (Lookup) Adds a new view for the lookup dialog
  • addOption: (Option Set) Adds an option to an Option set control
  • clearOptions: (Option Set) Clears all options for an Option Set control
  • getAttribute: (Standard, Lookup, Option Set) Returns the attribute that the control is bound to
  • getControlType: (All) Returns a value that categorizes controls
  • getData: (Silverlight Web resources) Returns the data query string parameter passed to a Silverlight Web resource
  • getDefaultView: (Lookup) Returns the ID value of the default lookup dialog view
  • getDisabled: (All) Returns a value indicating whether the control is disabled
  • getLabel: (All) Returns the label for the control
  • getName: (All) Returns the name assigned to the control.
  • getParent: (All) Returns a reference to the section object that contains the control.
  • getSrc: (IFrame, Web Resource) Returns the current URL being displayed in an IFRAME.
  • getInitialUrl: (IFrame) Returns the default URL of an IFrame control
  • getObject: (IFrame, Web resource) Returns the object in the form representing an IFrame or Web resource
  • getVisible: (All) Returns a value indicating if the control is visible
  • refresh: (SubGrid) Refreshes the data displayed in a Sub-Grid
  • removeOption: (Option Sets) Removes an option from an Option Set control
  • setData: (Silverlight Web resources) Sets the data query string parameter passed to a Silverlight Web resource.
  • setDefaultView: (Lookup) Sets the default view for the lookup dialog
  • setDisabled: (All except Web Resources) Sets a value indicating whether the control is disabled.
  • setFocus: (All) Sets the focus on the control.
  • setLabel: (All) Sets the label for the control.
  • setSrc: (IFrame and Web Resource) Sets the URL to be displayed in an IFrame.
  • setVisible: (All) Sets a value indicating if the control is visible

Xrm.Page.data.entity Methods 

  • addOnSave: Sets a function to be called when the record is saved
  • getDataXml: Returns the xml string to be sent to the server when the record is saved
  • getEntityName: Returns the logical name of the entity for the record
  • getId: Returns GUID id value for the record
  • getIsDirty: Returns a Boolean value indicating if any fields in the form have been modified
  • removeOnSave: Removes a function from the OnSave event hander
  • save: Saves the record

Xrm.Page.ui Methods

  • close: Closes the form.
  • getCurrentControl: Returns the control that currently has focus
  • getFormType: Indicates the form context for the record
  • getViewPortHeight: Returns the height of the viewport in pixels
  • getViewPortWidth: Returns the width of the viewport in pixels
  • refreshRibbon: Causes the ribbon to refresh

Xrm.Page.ui.controls Collection Methods

  • forEach: Applies the action contained within a delegate function
  • get: Returns one or more controls
  • getLength: Returns the number of controls in the collection.

Xrm.Page.ui.navigation.items Collection Methods

  • forEach: Applies the action contained within a delegate function
  • get: Returns one or more navigation items
  • getLength: Returns the number of navigation items in the collection.

Xrm.Page.ui.formSelector members

  • items: (Collection) A collection of all accessible form items
  • getCurrentItem: (Method) Returns the form currently being shown.

Xrm.Page.ui.formSelector.items Collection Methods

  • forEach: Applies the action contained within a delegate function.
  • get: Returns one or more roleForms
  • getLength: Returns the number of roleForms in the collection.

Xrm.Page.ui.tabs Collection Methods

  • forEach: Applies the action contained within a delegate function
  • get: Returns one or more tabs
  • getLength: Returns the number of tabs in the collection.

Xrm.Page.data.entity.attributes Collection Methods

  • forEach: Applies the action contained within a delegate function
  • get: Returns one or more attributes
  • getLength: Returns the number of items in the collection.

Shortcut methods

  • Xrm.Page.getAttribute: Xrm.Page.data.entity.attributes.get
  • Xrm.Page.getControl: Xrm.Page.ui.controls.get
  • GetGlobalContext (Web Resources): Xrm.Page.context (within forms)