Duane Napier's Blog

Microsoft Dynamics CRM

  • Follow Duane Napier's Blog on WordPress.com

Auto-Populating the Name attribute – CRM 2013

Posted by duanenapier on April 25, 2014

This is an update the original post on auto-populating the Name attribute in CRM 4.0 found here. Microsoft Dynamics CRM 2013 has yet another new object model for JavaScript so I wanted to create a new post to step you through the process to auto-populate the name attribute in the Microsoft Dynamics CRM 2013 environment.

Background: When creating a new entity in Microsoft Dynamics CRM 2013 the Primary Attribute defaults to “Name”. This name attributes is used in all the default views and when you open the record, this attribute is what the user sees in the top of the form as well. In this scenario I have a new entity that tracks my employees weekly utilization. The form captures the employee name, the week of the work and the overall percent of utilization they are tied up. Capturing this high level data allows me to quickly see who is working on what and when new work items come in for that week, I know at a glance who I can assign work to without any calls\email\spreadsheets, etc., lowering my administration time to delegate work.

Issue: So you have created a new entity and the default Name attribute needs to be populated. The problem is, the Name attribute really shouldn’t be populated by the user, it really needs to be automatically populated with several attributes of the record such as Week, employee name and percent utilized.

Provided you are not importing records into this entity and there is no integration considerations (meaning you are not creating records of this type through code), auto-populating the name can safely be done via JavaScript.

Resolution: Form level JavaScript, not Business Rules. Why not Business Rules?…they are simply not powerful enough to capture & concatenate several fields and then populate them into a single field.

You will need to review the SDK to understand some of the code in this article. The OnLoad code can be used as-is. The OnSave code is geared towards my entity with the following attributes:
1) A lookup to the user entity with the attribute of new_cincomrep
2) A string field attribute of new_utilizedpercent
3) A Date field attribute of new_sundaystartweek

Form OnLoad:

function CheckName()
{
var UtilName = Xrm.Page.getAttribute(“new_name”).getValue();
if (UtilName == null)
   {
// We will temporarily set the required Name field to a dummy value of “-” so the form will save.
     Xrm.Page.getAttribute(“new_name”).setValue(“-“);
}
}

function SetName()
{
var newName;
var CincomRep;
var WeeklyUtil = Xrm.Page.getAttribute(“new_utilizedpercent”).getValue();
var StartDate = new Date(Xrm.Page.getAttribute(“new_sundaystartweek”).getValue());
var lookupEmployee = Xrm.Page.getAttribute(“new_cincomrep”);

// The Employee should never be null if it is a required field.
if (lookupEmployee != null)
{
var lookupEmployeeValue = lookupEmployee.getValue();
if ((lookupEmployeeValue != null))
  {
     CincomRep = lookupEmployeeValue[0].name;
   }
}

newName = StartDate.format(“MM/dd/yyyy”)
newName += ” – “;
newName += CincomRep;
newName += ” – “;
newName += WeeklyUtil;
newName += “%”;

Xrm.Page.getAttribute(“new_name”).setValue(newName);
// The return will look like: 04/13/14 – Duane Napier – 90%
}

Adding these 2 JavaScript functions to the OnLoad and OnSave events will auto-populate the name attribute for you, allowing you to keep the default new_name field as it is without the additional work of modifying views, etc.

Leave a comment