Duane Napier's Blog

Microsoft Dynamics CRM

  • Follow Duane Napier's Blog on WordPress.com

Auto-Populating the Name attribute – CRM4.0

Posted by duanenapier on May 5, 2010

Background: When creating a new entity in Microsoft Dynamics CRM 4.0 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.

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.

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

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 project entity with the attribute of new_projectid.
2) A lookup to the partner entity with the attribute of new_partnerid
3) A picklist to the Stage via the attribute of new_stage.
4) A currency field attribute of new_investment.

I used different types of fields in this example so that you have a good idea of how to extract the value of different field types.

Form OnLoad:
if (crmForm.all.new_name.DataValue == null)
{
//This is done because the name attribute is required and the OnSave event will not fire if it is blank.
crmForm.all.new_name.DataValue = “-“;
}
crmForm.all.new_name.readOnly = true;

Form OnSave:
var ProjectInvestmentName;
var ProjectlookupItem = new Array;
var PartnerlookupItem = new Array;
var StageName;
var InvestmentAmount;

// This gets the lookup for the attribute new_projectid on the Project Investment form.
ProjectlookupItem = crmForm.all.new_projectid.DataValue;
PartnerlookupItem = crmForm.all.new_partnerid.DataValue;
//Note that this step wouldn’t be required if the Project attribute is set to Business Required.
if (ProjectlookupItem != null)
{
//I have elected to set the name with the Project name first
ProjectInvestmentName=ProjectlookupItem[0].name;
}

//Note that this step wouldn’t be required if the Partner attribute is set to Business Required.
if (PartnerlookupItem != null)
{
ProjectInvestmentName+= “-” + PartnerlookupItem[0].name;
}

//Note that this step doesn’t need to verify if a selection exists because the Stage attribute has been set to Business Required.
StageName=crmForm.all.new_stage.SelectedText;

//ProjectName-Partner-Stage
ProjectInvestmentName += “-” + StageName;

InvestmentAmount = crmForm.all.new_investment.DataValue;

//I only use this next line because I want to format the currency value.
InvestmentAmount=formatCurrency(InvestmentAmount);
ProjectInvestmentName += “-” + InvestmentAmount;

//We now set the name.
crmForm.all.new_name.readOnly = false;
crmForm.all.new_name.DataValue = ProjectInvestmentName;
crmForm.all.new_name.readOnly = true;

//This is a cool format function that properly formats US currency.
function formatCurrency(strValue)
{
strValue = strValue.toString().replace(/\$|\,/g,”);
dblValue = parseFloat(strValue);

blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
dblValue = Math.floor(dblValue*100+0.50000000001);
intCents = dblValue%100;
strCents = intCents.toString();
dblValue = Math.floor(dblValue/100).toString();
if(intCents<10)
strCents = “0” + strCents;
for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+’,’+
dblValue.substring(dblValue.length-(4*i+3));
return (((blnSign)?”:’-‘) + ‘$’ + dblValue + ‘.’ + strCents);
}

One Response to “Auto-Populating the Name attribute – CRM4.0”

  1. […] 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 […]

Leave a comment