Duane Napier's Blog

Microsoft Dynamics CRM

  • Follow Duane Napier's Blog on WordPress.com

Posts Tagged ‘Microsoft Dynamics 2013’

Getting values from form fields in JavaScript – CRM 2013

Posted by duanenapier on April 25, 2014

There are some subtle differences in CRM 2011 JavaScript vs. CRM 2013 JavaScript so this post will focus on the new Xrm schema and cover getting the values for text fields, picklist fields and lookup fields in Microsoft Dynamics CRM 2013. Refer to the Web Resources post for specifics how to create and use JavaScript files.

Get the value from a Lookup Field:

//1st Thing – You need to create an Array variable
var UserlookupItem = new Array;

//2nd Thing – You need to get the Value. The getValue function will return the respective object’s return type. In this case it will return an Array because the Coverage Member I have created is a lookup to the user table.
UserlookupItem = Xrm.Page.getAttribute(“new_coveragemember”).getValue();

//3rd Thing – Get the first item in the array, which is the name.
var MyNewName;
MyNewName = UserlookupItem[0].name;

Get the value from a OptionSet (Picklist) Field:
//You may want the ordinal value or the text value so we will grab both
//1st Thing – Get the ordinal, or integer value of the selected option using getValue()
var intListItem = Xrm.Page.getAttribute(“CompanyType”).getValue();

//2nd Thing – Get the text value of the selected option using getText()
var txtListItem = Xrm.Page.getAttribute(“CompanyType”).getText();

Get the value from a Text Field:
//1st Thing – 1 simple function call of getValue() can grab the value
var RecordName = Xrm.Page.getAttribute(“new_name”).getValue();

Posted in JavaScript, Microsoft Dynamics CRM 2013 | Tagged: , , , , , , , | Leave a Comment »

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.

Posted in JavaScript, Microsoft Dynamics CRM 2013 | Tagged: , , , , , , | Leave a Comment »