Sample: SDK.EntitySamples.js

[Applies to: Microsoft Dynamics CRM 2011]

This sample code is for Microsoft Dynamics CRM 2011, and can be found in the following location in the SDK download: SDK\SampleCode\JS\FormScripts\SDK.EntitySamples.js

Requirements

This sample code represents a JScript library that can be added as a Web resource. Before a function in a JScript library can be used as a form event handler, the JScript library must be added to the form libraries available for that form. The full name of the function must be used in the form event handler. For example, the showRecordProperties function must be called using SDK.EntitySamples.showRecordProperties().

Demonstrates

This library contains examples of the following Xrm.Page.data.entity methods:

Example

//If the SDK namespace object is not defined, create it.
if (typeof (SDK) == "undefined")
{ SDK = {}; }
// Create Namespace container for functions in this library;
SDK.EntitySamples = {};

 // Xrm.Page.data.entity.getEntityName() example
 // Xrm.Page.data.entity.getId() example
 // Example: The showRecordProperties function displays the entity name and id to the user.
SDK.EntitySamples.showRecordProperties = function showRecordProperties() {
 var entityName = Xrm.Page.data.entity.getEntityName();
 var entityId = Xrm.Page.data.entity.getId();

 var message = "This record's entity name is '" + entityName + "'.\n";

 if (entityId == null) {
  message += "This record must be saved before it has an id.";
 }
 else {
  message += "This record's id is '" + entityId + "'.";
 }

 alert(message);
};

 // Xrm.Page.data.entity.getDataXml() example
 // Xrm.Page.data.entity.getIsDirty() example
 // Xrm.Page.data.entity.save() example
// Example: The SDK.EntitySamples.saveDirtyRecordDemo function will check if the form is dirty, and if so will present the user with a 
 // list of fields that will be sent to the server. 
SDK.EntitySamples.saveDirtyRecordDemo = function saveDirtyRecordDemo() {
 var isDirty = Xrm.Page.data.entity.getIsDirty();

 if (isDirty) {
  var dirtyFieldsPassedToServer = [];
  var cleanFieldsPassedToServer = [];

  // Create an XML Object to parse the entity's DataXml
  var xmlDoc;
  if (window.DOMParser) {
   xmlDoc = new DOMParser().parseFromString(Xrm.Page.data.entity.getDataXml(), "text/xml");
  }
  else {
   xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
   xmlDoc.async = false;
   xmlDoc.resolveExternals = false;
   xmlDoc.loadXML(Xrm.Page.data.entity.getDataXml());
  }

  // Assume every attribute being passed to the server is clean.
  // When we loop through the dirty attributes on the form,
  // we will move them from the clean array to the dirty array.
  for (var i = 0; i < xmlDoc.documentElement.childNodes.length; i++) {
   var childNode = xmlDoc.documentElement.childNodes[i];
   cleanFieldsPassedToServer.push(childNode.tagName);
  }

  Xrm.Page.data.entity.attributes.forEach(function (attribute, index) {
   if (attribute.getIsDirty()) {
    var attributeName = attribute.getName();

    // Remove the attribute from the clean fields array. 
    SDK.EntitySamples.removeElementFromArray(cleanFieldsPassedToServer, attributeName);

    // Add it to the dirty fields array.
    dirtyFieldsPassedToServer.push(attributeName);
   }
  });

  if (dirtyFieldsPassedToServer.length > 0) {
   var message = "The following fields are dirty and will be now be saved:\n- ";
   message += dirtyFieldsPassedToServer.join("\n- ");
  }

  if (cleanFieldsPassedToServer.length > 0) {
   message += "\n\nThe following fields are not dirty, but will still be sent to the server:\n- ";
   message += cleanFieldsPassedToServer.join("\n- ");
  }

  alert(message);
  Xrm.Page.data.entity.save();
 }
 else {
  alert("The current record is not dirty.");
 }
};

SDK.EntitySamples.removeElementFromArray = function (array, element) {
 for (var i = 0; i < array.length; i++) {
  if (array[i] == element) {
   array.splice(i, 1);
   break;
  }
 }
};
 //End of Entity Samples Functions

See Also

Reference

Xrm.Page.data.entity

Concepts

Write Code for Microsoft Dynamics CRM Forms
Use JavaScript with Microsoft Dynamics CRM

Other Resources

Xrm.Page Sample Libraries

Microsoft Dynamics CRM 2011
Send comments about this topic to Microsoft.
© 2013 Microsoft Corporation. All rights reserved.