Export (0) Print
Expand All

Create Activity Record Templates

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Create buttons that will open new activity forms that are populated with default data. By default, most entities support including values in the query string that will set default values for a new form opened by using the URL address. However, activity entities currently do not support this technique. This example demonstrates a workaround.

The URL defined for a button in ISV.Config may contain additional query string parameters that can be read from the DHTML document.location.search object to start specific conditional logic in the form onload event to set default values.

This sample creates three buttons on the ccount form. Each button will open a new Task form that contains different default data. There are three buttons: Low, Normal, and High to correspond to the priority of the task. Logic in the OnLoad event for the Task form will set default data for four fields as shown in the following table.

Button LabelPrioritySubjectDueRegarding
LowLowLow Priority:3 days from todayThe Account record from which the Task was created
NormalNormalNormal Priority:2 days from today 
HighHighHigh Priority:1 day from today 

Completing this task involves two steps:

  1. Configure the Buttons using ISV.Config.
  2. Configure a script in the OnLoad event for the Task form.

Example ISV.Config

The ISV.Config must be modified to create the buttons in the toolbar for the Account entity. The toolbar must only appear for Accounts that have been saved. The URL attribute of the button elements uses the URL to the Task activity form and includes some additional query string values.

See also:

Account Toolbar element from ISV.Config:

<!-- The Account Tool Bar -->
<ToolBar ValidForCreate="0"  ValidForUpdate="1">
 <Button Icon="/_imgs/ico_18_4212.gif" Url="/activities/task/edit.aspx?prioritycode=0&amp;subject=Low Priority:">
  <Titles>
   <Title LCID="1033" Text="Low" />
  </Titles>
  <ToolTips>
   <ToolTip LCID="1033" Text="Low Priority Task" />
  </ToolTips>
 </Button>
 <Button Icon="/_imgs/ico_18_4212.gif" Url="/activities/task/edit.aspx?prioritycode=1&amp;subject=Normal Priority:">
  <Titles>
   <Title LCID="1033" Text="Normal" />
  </Titles>
  <ToolTips>
   <ToolTip LCID="1033" Text="Normal Priority Task" />
  </ToolTips>
 </Button>
 <Button Icon="/_imgs/ico_18_4212.gif" Url="/activities/task/edit.aspx?prioritycode=2&amp;subject=High Priority:">
  <Titles>
   <Title LCID="1033" Text="High" />
  </Titles>
  <ToolTips>
   <ToolTip LCID="1033" Text="High Priority Task" />
  </ToolTips>
 </Button>
</ToolBar>

Toolbar Configuration

Note the following with regard to how the button elements in the toolbar have been configured:

  • The ampersand symbol "&" used to separate query string values must be escaped using "&amp;" within the XML used in ISV.Config.
  • All query string values must correspond to attribute names for the entity. Arbitrary query string values are not supported.
  • The PassParameters attribute is not used. Use of this attribute to pass values related to the Account record will conflict with the parameters that are passed in the URL to set values in the Task record.

Example Task OnLoad Event Code

The Task OnLoad event code reads the information passed from the querystrings through the document.location.search object. Based on the values that are passed, specific default values are set in the new Task form. The user may alter these default values before the Task is saved.

Task OnLoad Event Code

var CRM_FORM_TYPE_CREATE = 1;

// Only for new Tasks created with querystring parameters added.
if ((crmForm.FormType == CRM_FORM_TYPE_CREATE) && (location.search))
{
// Create an array to store the querystring parameters.
 var request= new Array(); 
 var vals=location.search.substr(1).split("&");
 for (var i in vals) 
 { 
  vals[i] = vals[i].replace(/\+/g, " ").split("="); 
  request[unescape(vals[i][0])] = unescape(vals[i][1]); 
 } 
 // Get the priority and subject fields from the querystring.
 var prioritycode = parseInt(request["prioritycode"],10);
 var subject = request["subject"];
 
 // Set the due date based on the priority.
 var oToday = new Date();
 var oDate = oToday;
 var iDate = oToday.getDate();
 switch (prioritycode)
 {
  case 0: //Low Priority
  iDate = iDate+3
  break;
  case 1: //Normal Priority
  iDate = iDate+2
  break;
  case 2: //High Priority
  iDate = iDate+1
  break;
 }
 oDate.setDate(iDate);
 
 // Set the values for the priority, subject, and due date.
 crmForm.all.prioritycode.DataValue = prioritycode;
 crmForm.all.subject.DataValue = subject;
 crmForm.all.scheduledend.DataValue = oDate;
 
 // Get information from the parent account.
 if (window.opener)
 {
  var oParentCrmForm = window.opener.document.all.crmForm;
  
  if (oParentCrmForm)
  {
    var gRecordId = oParentCrmForm.ObjectId;
    var sRecordName = oParentCrmForm.all.name.DataValue;
    var sFormTypeName = oParentCrmForm.ObjectTypeName;
    
    var lookupData = new Array();
    
    var lookupItem = new Object();
    lookupItem.id = gRecordId;
    lookupItem.typename = sFormTypeName;
    lookupItem.name = sRecordName;
    
    lookupData[0] = lookupItem;
    // Set the value of the Resgarding lookup field
    crmForm.all.regardingobjectid.DataValue = lookupData;
    
  }
 }
}

Task OnLoad Event Code Logic

Note the following logic applied in the Task OnLoad event code:

  • The crmForm.FormType property value is used as a condition so that the logic is only applied before a new record is created.
  • The DHTML location.search object must exist for the code to run.
  • This sample uses logic to parse the location.search object and make the values available in an array called 'request' that resembles the way query string values are made available in ASP.NET server-side code. This particular technique is not required, but the location.search object represents a string with name/value pairs separated by the ampersand symbol that must be parsed before it can be used.
  • For other entities, setting the value of an appropriately named query string parameter is enough to set the value for text fields. For example, setting the name value for a new account can be done by using this URL attribute for a button element in the ISV.Config:
    Url="/sfa/accts/edit.aspx?name=test"
    

    However, the Task entity form does not behave the same way. Although the value for the subject text field is available in the location.search object, it must be explicitly set.

  • The prioritycode value that is passed must be converted to an integer value by using the parseInt function.
  • To set the Due field, a DateTime value must be set. In this case, the date portion is adjusted based on the priority.
  • Information about the parent record is usually passed from a button using the PassParameters attribute. However, this method is incompatible with manually setting query string values. Instead of passing information about the account using the querystring, information about the originating account record is obtained using window.opener. This information is used to set the Regarding lookup field value.

© 2010 Microsoft Corporation. All rights reserved.


Show:
© 2014 Microsoft