Export (0) Print
Expand All

Setting Default Values

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Data entry can be accelerated when default values are set on the form for a new record. There are several strategies that can be employed to do this.

Edit attribute configuration

Boolean (bit) and Picklist attributes can be configured in the application to set a default value. No other attribute types support setting a default value.

Mapping Entity Attributes

Some values are set automatically when a new record is created that has a relationship to an existing record. When a new record is created from an Associated View, the Mapping configured for that relationship is applied to take data from the parent record and store it in the fields specified in the Mapping. For more information about this technique, see the Help topic "Mapping Entity Attributes." This topic is also available online at rc.crm.dynamics.com/rc/regcont/en_us/op/help/ug_cust_entity_mapping.htm.

URL Addressable Form QueryString parameters

When a new record is created by using a URL addressable form, you can pass querystring parameters to the form using the attribute name. When the form loads, the values are set. This can be used in a button or menu item configured by using ISV.Config or from another Web site. For more information, see Passing parameters to set values to new records in URL Addressable Forms and Views.

Setting Values based on Form or Field events

For default values that are always the same use the onload event to set field values. Make sure that you include code that checks the crmForm.FormType property so that the default values are only set when a new record is created.

When default values may vary based on a particular sub-category of record, the OnChange event of a picklist field listing those sub-categories may be used to set values that depend on the value of the picklist.

Setting default values depends on the type of field. An example that shows how to set the value of a duration field in an appointment can be found at rc.crm.dynamics.com/rc/regcont/en_us/op/articles/defaultvalues.aspx. 

The following sample presents examples based on several types of fields found in the default Opportunity Entity Form.

Example

var CRM_FORM_TYPE_CREATE = "1";
if (crmForm.FormType==CRM_FORM_TYPE_CREATE)
 {
  // Set the value of the topic field.
  var topic = crmForm.all.name;
  topic.DataValue = "New Opportunity: ";
   
  // Price List - a lookup field.
  var priceList = crmForm.all.pricelevelid;
   
  // Do Not overwrite an existing value that may be mapped.
  if (priceList.DataValue == null)
  {
   // Create an array to set as the DataValue for the lookup control.
   var lookupData = new Array();
   // Create an object to add to the array.
   var lookupItem= new Object();
   // Set the id, typename, and name properties to the object.
   lookupItem.id = '{1726C802-22CA-DC11-BE78-0003FF9EE217}';
   lookupItem.typename = 'pricelevel';
   lookupItem.name = 'Retail';
   // Add the object to the array.
   lookupData[0] = lookupItem;
   // Set the value of the lookup field to the value of the array.
   priceList.DataValue = lookupData;
  }
   
  // Set the currency before the revenue amount can be set.
  var currency = crmForm.all.transactioncurrencyid;
   
  // Do Not overwrite an existing value that may be mapped.
  if (currency.DataValue == null)
  {
   // Create an array to set as the DataValue for the lookup control.
   var lookupData = new Array();
   // Create an Object add to the array.
   var lookupItem= new Object();
   // Set the id, typename, and name properties to the object.
   lookupItem.id = '{F84F576A-3EC9-DC11-A8D2-0003FF9EE217}';
   lookupItem.typename = 'transactioncurrency';
   lookupItem.name = 'US Dollar';
   // Add the object to the array.
   lookupData[0] = lookupItem;
   // Set the value of the lookup field to the value of the array.
   currency.DataValue = lookupData;
  }
   
  // Set the Estimated Revenue.
  var estRev = crmForm.all.estimatedvalue;
  // Only set the default if the currency is US Dollar.
  if (currency.DataValue[0].name == 'US Dollar')
  {
   estRev.DataValue = 1000;
  }
   
  // Set the Est Close Date field.
  var estCloseDate = crmForm.all.estimatedclosedate;
  var oToday = new Date();
  var todaysDate = oToday.getDate();
  // Set the date 5 days into the future.
  var DueDate = todaysDate + 5;
  var oDate = oToday;
  oDate = oDate.setDate(DueDate);
  estCloseDate.DataValue = oDate;

  // Set the Description Field.
  var description = crmForm.all.description;
  var message = "Don't forget to include a short ";
  message += "description of the opportunity here.";
  description.DataValue = message;
  
 }

© 2010 Microsoft Corporation. All rights reserved.


Show:
© 2014 Microsoft