Xrm.Page.data.entity Attribute Methods
The attribute object provides methods to retrieve information and perform actions on attributes. It also contains a Controls Collection.
Examples shown here are in the Sample: SDK.AttributeSamples.js library.
The following table lists the methods of the attribute object.
| Method | Description |
|---|---|
|
Sets a function to be called when the attribute value is changed. See also removeOnChange Attribute Types: All |
|
|
Causes the OnChange event to occur on the attribute so that any script associated to that event can execute. Attribute Types: All |
|
|
Returns a string value that represents the type of attribute. Attribute Types: All |
|
|
Returns a string value that represents formatting options for the attribute. Attribute Types: All |
|
|
Returns the initial value for Boolean or optionset attributes. Attribute Types: Boolean, optionset |
|
|
Returns a Boolean value indicating if there are unsaved changes to the attribute value. Attribute Types: All |
|
|
Returns a number indicating the maximum allowed value for an attribute. Attribute Types: money, decimal, integer, double |
|
|
Returns a number indicating the maximum length of a string or memo attribute. Attribute Types: string, memo |
|
|
Returns a number indicating the minimum allowed value for an attribute. Attribute Types: money, decimal, integer, double |
|
|
Returns a string representing the logical name of the attribute. Attribute Types: All |
|
|
Returns an option object with the name matching the argument passed to the method. Attribute Types: optionset |
|
|
Returns an array of option objects representing the valid options for an optionset attribute. Attribute Types: optionset |
|
|
Returns the entity object that is the parent to the attribute. Attribute Types: All |
|
|
Returns the number of digits allowed to the right of the decimal point. Attribute Types: money, decimal, double, and integer |
|
|
Returns a string value indicating whether a value for the attribute is required or recommended. See also setRequiredLevel Attribute Types: All |
|
|
Returns the option object that is selected in an optionset attribute. Attribute Types: optionset |
|
|
Returns a string indicating when data from the attribute will be submitted when the record is saved. See also setSubmitMode Attribute Types: All |
|
|
Returns a string value of the text for the currently selected option for an optionset attribute. Attribute Types: optionset |
|
|
Returns an array of privileges that contain Boolean values indicating if the user can create, read or update data values for an attribute. Attribute Types: All |
|
|
Retrieves the data value for an attribute. See also setValue. Attribute Types: All |
|
|
Removes a function from the OnChange event hander for an attribute. See also addOnChange Attribute Types: All
|
|
|
Sets whether data is required or recommended for the attribute before the record can be saved. See also getRequiredLevel. Attribute Types: All |
|
|
Sets whether data from the attribute will be submitted when the record is saved. See also getSubmitMode. Attribute Types: All |
|
|
Sets the data value for an attribute. See also getValue. Attribute Types: All |
addOnChange
Sets a function to be called when the attribute value is changed.
attributeObj.addOnChange([function reference])
- Parameter
-
Type: function pointerRemarks: The function will be added to the bottom of the event handler pipeline.Example:In this example, the JScript library contains two functions. Adding the addMessageToOnChange function to the form OnLoad event will add the displayMessage function as a handler for the OnChange event for the first attribute in the form.
function addMessageToOnChange() { Xrm.Page.data.entity.attributes.get(0).addOnChange(displayMessage); } function displayMessage() { alert("message"); }
fireOnChange
Causes the OnChange event to occur on the attribute so that any script associated to that event can execute.
Attribute Types: All
attributeObj.fireOnChange()
- Return Value
-
Type: NoneExample: The SDK.AttributeSamples.fireOnChangeDemo function will cause the OnChange event to occur for every lookup attribute on the form.
SDK.AttributeSamples.fireOnChangeDemo = function () { var attributes = Xrm.Page.data.entity.attributes.get(SDK.AttributeSamples.isLookup); for (var i in attributes) { var attribute = attributes[i]; var tempFunc = function () { SDK.AttributeSamples.attributeChanged(attribute); }; attribute.addOnChange(tempFunc); attribute.fireOnChange(); attribute.removeOnChange(tempFunc); } }; SDK.AttributeSamples.isLookup = function (attribute, index) { return attribute.getAttributeType() == "lookup"; }; SDK.AttributeSamples.attributeChanged = function (attribute) { alert("Change event occured on " + attribute.getName()); };
getAttributeType
Returns a string value that represents the type of attribute.
Attribute Types: All
attributeObj.getAttributeType()
- Return Value
-
Type: String
This method will return one of the following string values:
-
boolean
-
datetime
-
decimal
-
double
-
integer
-
lookup
-
memo
-
money
-
optionset
-
string
SDK.AttributeSamples.showAttributeTypes = function () { var html = "<!DOCTYPE html ><html lang='en-US' ><head><title>Show Attribute Types</title>"; html += "<style type=\"text/css\">body { font-family:Calibri;}"; html += "table {border:1px solid gray; border-collapse:collapse;}"; html += "th {text-align:left; border:1px solid gray;}"; html += "td {border:1px solid gray;}</style>"; html += "</head><body>"; html += SDK.AttributeSamples.getAttributeTypes(); html += "</body></html>"; var myWindow = window.open("", "_blank", "height=400,width=350,scrollbars=1,resizable=1", false); myWindow.document.open(); myWindow.document.write(html); myWindow.document.close(); }; SDK.AttributeSamples.getAttributeTypes = function () { var html = "<table summary='This table displays the type of each attribute.'><thead><tr><th scope='col'>Attribute Name</th><th scope='col'>Attribute Type</th></tr></thead><tbody>"; var attributes = Xrm.Page.data.entity.attributes.get(); for (var i in attributes) { var attribute = attributes[i]; html += "<tr><td>" + attribute.getName() + "</td><td>" + attribute.getAttributeType() + "</td></tr>"; } html += "</tbody></table>"; return html; };
-
boolean
getFormat
Returns a string value that represents formatting options for the attribute.
Attribute Types: All
attributeObj.getFormat()
- Return Value
-
Type: String
This method will return one of the following string values or null:
-
date
-
datetime
-
duration
-
email
-
language
-
none
-
text
-
tickersymbol
-
timezone
-
url
SDK.AttributeSamples.showFormatValues = function () { var html = "<!DOCTYPE html ><html lang='en-US' ><head><title>Show Format</title>"; html += "<style type=\"text/css\">body { font-family:Calibri;}"; html += "table {border:1px solid gray; border-collapse:collapse;}"; html += "th {text-align:left; border:1px solid gray;}"; html += "td {border:1px solid gray;}</style>"; html += "</head><body>"; html += SDK.AttributeSamples.getFormatValues(); html += "</body></html>"; var myWindow = window.open("", "_blank", "height=400,width=400,scrollbars=1,resizable=1", false); myWindow.document.open(); myWindow.document.write(html); myWindow.document.close(); }; SDK.AttributeSamples.getFormatValues = function () { var html = "<table summary='This table displays the format values for attributes on the page.'><thead><tr><th scope='col'>Attribute Name</th><th scope='col'>Attribute Type</th><th scope='col'>Format</th></tr></thead><tbody>"; var attributes = Xrm.Page.data.entity.attributes.get(); for (var i in attributes) { var attribute = attributes[i]; html += "<tr><td>" + attribute.getName() + "</td><td>" + attribute.getAttributeType() + "</td><td>" + attribute.getFormat() + "</td></tr>"; } html += "</tbody></table>"; return html; };
-
date
Note |
|---|
| This format information generally represents the format options of the application field. Format options for Boolean fields are not provided. The Text Area formatting option for Single Line of Text fields is not shown as a format. However it is shown as the attribute type of ‘memo’ using getAttributeType. The following table lists the format string values to expect for each type of attribute schema type and format option. |
|
Application Field Type |
Format Option |
Attribute Type |
Format Value |
|
Date and Time |
Date Only |
datetime |
date |
|
Date and Time |
Date and Time |
datetime |
datetime |
|
Whole Number |
Duration |
integer |
duration |
|
Single Line of Text |
|
string |
|
|
Whole Number |
Language |
optionset |
language |
|
Whole Number |
None |
integer |
none |
|
Single Line of Text |
Text Area |
string |
text |
|
Single Line of Text |
Text |
string |
text |
|
Single Line of Text |
Ticker Symbol |
string |
tickersymbol |
|
Whole Number |
Time Zone |
optionset |
timezone |
|
Single Line of Text |
Url |
string |
url |
getInitialValue
Returns a value that represents the value set for an optionset or Boolean attribute when the form opened.
Attribute Types: Optionset
attributeObj.getInitialValue()
- Return Value
-
Type: NumberExample: The SDK.AttributeSamples.showInitialValue function will display a page in a new window showing the initial values for boolean or optionset attributes when the form is opened.
SDK.AttributeSamples.showInitialValue = function () { var html = "<!DOCTYPE html ><html lang='en-US' ><head><title>OptionSet and Boolean Default Values</title>"; html += "<style type=\"text/css\">body { font-family:Calibri;}"; html += "table {border:1px solid gray; border-collapse:collapse;}"; html += "th {text-align:left; border:1px solid gray;}"; html += "td {border:1px solid gray;}</style>"; html += "</head><body>"; html += SDK.AttributeSamples.getBooleanAndOptionSetAttributes(); html += "</body></html>"; var myWindow = window.open("", "_blank", "height=400,width=350,scrollbars=1,resizable=1", false); myWindow.document.open(); myWindow.document.write(html); myWindow.document.close(); }; SDK.AttributeSamples.getBooleanAndOptionSetAttributes = function () { var fields = "<table summary='This table displays the initial values for boolean and optionset attributes.'><thead><tr><th scope='col'>Name</th><th scope='col'>Initial Value</th></tr></thead><tbody>"; var attributes = Xrm.Page.data.entity.attributes.get(SDK.AttributeSamples.isBooleanOrOptionSet); for (var i in attributes) { var attribute = attributes[i]; fields += "<tr><td>" + attribute.getName() + "</td><td>" + attribute.getInitialValue() + "</td></tr>"; } fields += "</tbody></table>"; return fields; }; SDK.AttributeSamples.isBooleanOrOptionSet = function (attribute, index) { var result = false; var type = attribute.getAttributeType(); if (type == "boolean" || type == "optionset") { result = true; } return result; };
getIsDirty
Returns a Boolean value indicating if there are unsaved changes to the attribute value.
Attribute Types: All
attributeObj.getIsDirty()
- Return Value
-
Type: BooleanExample: The SDK.AttributeSamples.showIsDirty function will display a page in a new window showing the IsDirty values for attributes on the page.
SDK.AttributeSamples.showIsDirty = function () { var html = "<!DOCTYPE html ><html lang='en-US' ><head><title>Show IsDirty</title>"; html += "<style type=\"text/css\">body { font-family:Calibri;}"; html += "table {border:1px solid gray; border-collapse:collapse;}"; html += "th {text-align:left; border:1px solid gray;}"; html += "td {border:1px solid gray;}</style>"; html += "</head><body>"; html += SDK.AttributeSamples.getIsDirty(); html += "</body></html>"; var myWindow = window.open("", "_blank", "height=400,width=350,scrollbars=1,resizable=1", false); myWindow.document.open(); myWindow.document.write(html); myWindow.document.close(); }; SDK.AttributeSamples.getIsDirty = function () { var html = "<table summary='This table displays the isDirty values for attributes on the page.'><thead><tr><th scope='col'>Attribute Name</th><th scope='col'>IsDirty</th></tr></thead><tbody>"; var attributes = Xrm.Page.data.entity.attributes.get() for (var i in attributes) { var attribute = attributes[i]; html += "<tr><td>" + attribute.getName() + "</td><td>" + attribute.getIsDirty() + "</td></tr>"; } html += "</tbody></table>"; return html; };
getMax
Returns a number indicating the maximum allowed value for an attribute.
Attribute Types: money, decimal, integer, double
attributeObj.getMax()
- Return Value
-
Type: NumberExample: The SDK.AttributeSamples.showNumberFieldValueRanges function will display a page in a new window showing the maximum and minimum values for number attributes on the page.
SDK.AttributeSamples.showNumberFieldValueRanges = function () { var html = "<!DOCTYPE html ><html lang='en-US' ><head><title>Maximum and Minimum Values</title>"; html += "<style type=\"text/css\">body { font-family:Calibri;}"; html += "table {border:1px solid gray; border-collapse:collapse;}"; html += "th {text-align:left; border:1px solid gray;}"; html += "td {border:1px solid gray;}</style>"; html += "</head><body>"; html += SDK.AttributeSamples.getNumberFieldValueRanges(); html += "</body></html>"; var myWindow = window.open("", "_blank", "height=400,width=350,scrollbars=1,resizable=1", false); myWindow.document.open(); myWindow.document.write(html); myWindow.document.close(); }; SDK.AttributeSamples.getNumberFieldValueRanges = function () { var fields = "<table summary='This table displays the maximum and minimum values for number attributes on the page.'><thead><tr><th scope='col'>Name</th><th scope='col'>Max Value</th><th scope='col'>Min Value</th></tr></thead><tbody>"; var attributes = Xrm.Page.data.entity.attributes.get(SDK.AttributeSamples.isNumberField); for (var i in attributes) { var attribute = attributes[i]; fields += "<tr><td>" + attribute.getName() + "</td><td>" + attribute.getMax() + "</td><td>" + attribute.getMin() + "</td></tr>"; } fields += "</tbody></table>"; return fields; }; SDK.AttributeSamples.isNumberField = function (o, i) { return o.getAttributeType() == "money" || o.getAttributeType() == "decimal" || o.getAttributeType() == "integer" || o.getAttributeType() == "double"; };
getMaxLength
Returns a number indicating the maximum length of a string or memo attribute.
Attribute Types: string, memo
attributeObj.getMaxLength()
- Return Value
-
Type: NumberExample: The SDK.AttributeSamples.showMaxLengths function will display a page in a new window showing the maximum length values for string or memo attributes on the page.
Note The email form description attribute is a memo attribute, but it does not have a getMaxLength method. SDK.AttributeSamples.showMaxLengths = function () { var html = "<!DOCTYPE html ><html lang='en-US' ><head><title>Show Maximum Length Values</title>"; html += "<style type=\"text/css\">body { font-family:Calibri;}"; html += "table {border:1px solid gray; border-collapse:collapse;}"; html += "th {text-align:left; border:1px solid gray;}"; html += "td {border:1px solid gray;}</style>"; html += "</head><body>"; html += SDK.AttributeSamples.getMaximumLength(); html += "</body></html>"; var myWindow = window.open("", "_blank", "height=400,width=400,scrollbars=1,resizable=1", false); myWindow.document.open(); myWindow.document.write(html); myWindow.document.close(); }; SDK.AttributeSamples.getMaximumLength = function () { var fields = "<table summary='This table displays the maximumn length values for string or memo attributes on the page.'><thead><tr><th scope='col'>Name</th><th scope='col'>Max Length</th></tr></thead><tbody>"; var attributes = Xrm.Page.data.entity.attributes.get(SDK.AttributeSamples.isStringField); for (var i in attributes) { var attribute = attributes[i]; fields += "<tr><td>" + attribute.getName() + "</td><td>" + attribute.getMaxLength() + "</td></tr>"; } fields += "</tbody></table>"; return fields; }; SDK.AttributeSamples.isStringField = function (attribute, index) { return attribute.getAttributeType() == "string" || attribute.getAttributeType() == "memo"; };
getMin
Returns a number indicating the minimum allowed value for an attribute.
Attribute Types: money, decimal, integer, double
attributeObj.getMin()
- Return Value
-
Type: NumberExample: The SDK.AttributeSamples.showNumberFieldValueRanges function will display a page in a new window showing the maximum and minimum values for number attributes on the page.
SDK.AttributeSamples.showNumberFieldValueRanges = function () { var html = "<!DOCTYPE html ><html lang='en-US' ><head><title>Maximum and Minimum Values</title>"; html += "<style type=\"text/css\">body { font-family:Calibri;}"; html += "table {border:1px solid gray; border-collapse:collapse;}"; html += "th {text-align:left; border:1px solid gray;}"; html += "td {border:1px solid gray;}</style>"; html += "</head><body>"; html += SDK.AttributeSamples.getNumberFieldValueRanges(); html += "</body></html>"; var myWindow = window.open("", "_blank", "height=400,width=350,scrollbars=1,resizable=1", false); myWindow.document.open(); myWindow.document.write(html); myWindow.document.close(); }; SDK.AttributeSamples.getNumberFieldValueRanges = function () { var fields = "<table summary='This table displays the maximum and minimum values for number attributes on the page.'><thead><tr><th scope='col'>Name</th><th scope='col'>Max Value</th><th scope='col'>Min Value</th></tr></thead><tbody>"; var attributes = Xrm.Page.data.entity.attributes.get(SDK.AttributeSamples.isNumberField); for (var i in attributes) { var attribute = attributes[i]; fields += "<tr><td>" + attribute.getName() + "</td><td>" + attribute.getMax() + "</td><td>" + attribute.getMin() + "</td></tr>"; } fields += "</tbody></table>"; return fields; }; SDK.AttributeSamples.isNumberField = function (o, i) { return o.getAttributeType() == "money" || o.getAttributeType() == "decimal" || o.getAttributeType() == "integer" || o.getAttributeType() == "double"; };
getName
Returns a string representing the logical name of the attribute.
Attribute Types: All
attributeObj.getName()
- Return Value
-
Type: StringExample: The SDK.AttributeSamples.showNames function will display a page in a new window showing the names of attributes on the page.
SDK.AttributeSamples.showNames = function () { var html = "<!DOCTYPE html ><html lang='en-US' ><head><title>Show Attribute Name</title>"; html += "<style type=\"text/css\">body { font-family:Calibri;}"; html += "table {border:1px solid gray; border-collapse:collapse;}"; html += "th {text-align:left; border:1px solid gray;}"; html += "td {border:1px solid gray;}</style>"; html += "</head><body>"; html += SDK.AttributeSamples.getNames(); html += "</body></html>"; var myWindow = window.open("", "_blank", "height=400,width=350,scrollbars=1,resizable=1", false); myWindow.document.open(); myWindow.document.write(html); myWindow.document.close(); }; SDK.AttributeSamples.getNames = function () { var fields = "<table summary='This table displays the names of attributes on the page.'><thead><tr><th scope='col'>Name</th></tr></thead><tbody>"; var attributes = Xrm.Page.data.entity.attributes.get(); for (var i in attributes) { fields += "<tr><td>" + attributes[i].getName() + "</td></tr>"; } fields += "</tbody></table>"; return fields; };
getOption
Returns an option object with the value matching the argument passed to the method.
Attribute Types: optionset
attributeObj.getOption(value)
- Arguments
- String or Number value
- Return Value
-
Type: ObjectExample: The SDK.AttributeSamples.showAttributeSelectedOptionfunction will display a page in a new window showing the details of the selected option. The options are displayed using JSON style syntax.
SDK.AttributeSamples.showAttributeSelectedOption = function () { var html = "<!DOCTYPE html ><html lang='en-US' ><head><title>getOption</title>"; html += "<style type=\"text/css\">body { font-family:Calibri;}"; html += "table {border:1px solid gray; border-collapse:collapse;}"; html += "th {text-align:left; border:1px solid gray;}"; html += "td {border:1px solid gray;}</style>"; html += "</head><body>"; html += SDK.AttributeSamples.getSelectedOption(); html += "</body></html>"; var myWindow = window.open("", "_blank", "height=400,width=350,scrollbars=1,resizable=1", false); ; myWindow.document.open(); myWindow.document.write(html); myWindow.document.close(); }; SDK.AttributeSamples.getSelectedOption = function () { var fields = "<table summary='This table displays the selected options for optionset values on the page. Options are shown in JSON format.'><thead><tr><th scope='col'>Name</th><th scope='col'>Selected JSON option</th></tr></thead><tbody>"; var attributes = Xrm.Page.data.entity.attributes.get(SDK.AttributeSamples.isOptionSet); for (var i in attributes) { var attribute = attributes[i]; var attributeValue = attribute.getValue(); fields += "<tr><td>" + attribute.getName() + "</td><td>"; // SDK.AttributeSamples.showJSONOption(attribute) if (attributeValue != null) { var option = attribute.getOption(attributeValue); fields += " {"; fields += " text:\"" + option.text + "\" , value:\"" + option.value + "\""; fields += " }"; } else { fields += "No selected value"; } fields += "</td></tr>"; } fields += "</tbody></table>"; return fields; }; SDK.AttributeSamples.isOptionSet = function (attribute, index) { return attribute.getAttributeType() == "optionset"; };
getOptions
Returns an array of option objects representing the valid options for an optionset attribute.
Attribute Types: optionset
attributeObj.getOptions()
- Return Value
-
Type: ArrayExample: The SDK.AttributeSamples.showOptions function will display a page in a new window showing the available options for optionset attributes on the page. The option arrays are displayed using JSON style syntax.
SDK.AttributeSamples.showOptions = function () { var html = "<!DOCTYPE html ><html lang='en-US' ><head><title>OptionSet Options</title>"; html += "<style type=\"text/css\">body { font-family:Calibri;}"; html += "table {border:1px solid gray; border-collapse:collapse;}"; html += "th {text-align:left; border:1px solid gray;}"; html += "td {border:1px solid gray;}</style>"; html += "</head><body>"; html += SDK.AttributeSamples.getOptionSets(); html += "</body></html>"; var myWindow = window.open("", "_blank", "height=400,width=400,scrollbars=1,resizable=1", false); myWindow.document.open(); myWindow.document.write(html); myWindow.document.close(); }; SDK.AttributeSamples.getOptionSets = function () { var fields = "<table summary='This table displays the available options for optionset attributes on the page.'><thead><tr><th scope='col'>Name</th><th scope='col'>JSON options</th></tr></thead><tbody>"; var attributes = Xrm.Page.data.entity.attributes.get(SDK.AttributeSamples.isOptionSet) for (var i in attributes) { var attribute = attributes[i]; fields += "<tr><td>" + attribute.getName() + "</td><td>" + SDK.AttributeSamples.showJSONOptions(attribute.getOptions()) + "</td></tr>"; } fields += "</tbody></table>"; return fields; }; SDK.AttributeSamples.isOptionSet = function (attribute, index) { return attribute.getAttributeType() == "optionset"; }; SDK.AttributeSamples.showJSONOptions = function (options) { var html = "[<br />"; for (var i in options) { html += " {"; html += " text:\"" + options[i].text + "\" , value:\"" + options[i].value + "\""; html += " }"; html += (i < options.length - 1) ? ",<br />" : "<br />"; } html += "]"; return html; };
getParent
Returns the entity object that is the parent to the attribute.
Attribute Types: All
attributeObj.getParent()
- Return Value
-
Type: entityExample: The SDK.AttributeSamples.getParentDemo function demonstrates how
attribute.getParent()returns theXrm.Page.data.entityobject.SDK.AttributeSamples.getParentDemo = function () { var firstAttributesParent = Xrm.Page.data.entity.attributes.get(0).getParent(); var entity = Xrm.Page.data.entity; if (firstAttributesParent == entity) { alert("The first attribute's parent is the same as the entity object."); } else { alert("The first attribute's parent is not the same as the entity object."); } };
getPrecision
Returns the number of digits allowed to the right of the decimal point.
Attribute Types: money, decimal, double, and integer
attributeObj.getPrecision()
- Return Value
-
Type: NumberExample: The SDK.AttributeSamples.showPrecisionValues function will display a page in a new window showing the precision values for number attributes on the page.
SDK.AttributeSamples.showPrecisionValues = function () { var html = "<!DOCTYPE html ><html lang='en-US' ><head><title>Precision Values</title>"; html += "<style type=\"text/css\">body { font-family:Calibri;}"; html += "table {border:1px solid gray; border-collapse:collapse;}"; html += "th {text-align:left; border:1px solid gray;}"; html += "td {border:1px solid gray;}</style>"; html += "</head><body>"; html += SDK.AttributeSamples.getNumberFieldPrecision(); html += "</body></html>"; var myWindow = window.open("", "_blank", "height=400,width=350,scrollbars=1,resizable=1", false); myWindow.document.open(); myWindow.document.write(html); myWindow.document.close(); }; SDK.AttributeSamples.getNumberFieldPrecision = function () { var fields = "<table summary='This table displays the precision values for number attributes on the page.'><thead><tr><th scope='col'>Name</th><th scope='col'>Precision</th></tr></thead><tbody>"; var attributes = Xrm.Page.data.entity.attributes.get(SDK.AttributeSamples.isNumberField) for (var i in attributes) { var attribute = attributes[i]; fields += "<tr><td>" + attribute.getName() + "</td><td>" + attribute.getPrecision() + "</td></tr>"; } fields += "</tbody></table>"; return fields; }; SDK.AttributeSamples.isNumberField = function (attribute, index) { return attribute.getAttributeType() == "money" || attribute.getAttributeType() == "decimal" || attribute.getAttributeType() == "integer" || attribute.getAttributeType() == "double"; };
getRequiredLevel
Returns a string value indicating whether a value for the attribute is required or recommended.
Attribute Types: All
attributeObj.getRequiredLevel()
- Return Value
-
Type: String
Returns one of the three possible values:
-
none
-
required
-
recommended
SDK.AttributeSamples.showRequiredLevel = function () { var html = "<!DOCTYPE html ><html lang='en-US' ><head><title>Required Levels </title>"; html += "<style type=\"text/css\">body { font-family:Calibri;}"; html += "table {border:1px solid gray; border-collapse:collapse;}"; html += "th {text-align:left; border:1px solid gray;}"; html += "td {border:1px solid gray;}</style>"; html += "</head><body>"; html += SDK.AttributeSamples.getRequiredLevel(); html += "</body></html>"; var myWindow = window.open("", "_blank", "height=400,width=350,scrollbars=1,resizable=1", false); myWindow.document.open(); myWindow.document.write(html); myWindow.document.close(); }; SDK.AttributeSamples.getRequiredLevel = function () { var fields = "<table summary='This table displays the requirement level value for attributes on the page.'><thead><tr><th scope='col'>Name</th><th scope='col'>Precision</th></tr></thead><tbody>"; var attributes = Xrm.Page.data.entity.attributes.get() for (var i in attributes) { var attribute = attributes[i]; var requiredLevel = attribute.getRequiredLevel(); if (requiredLevel != "none") { fields += "<tr><td>" + attribute.getName() + "</td><td>" + requiredLevel + "</td></tr>"; } } fields += "</tbody></table>"; return fields; };
-
none
getSelectedOption
Returns the option object that is selected in an optionset attribute.
Attribute Types: optionset
attributeObj.getSelectedOption()
- Return Value
-
Type: OptionExample: The SDK.AttributeSamples.showSelectedOption function will display a page in a new window showing the selected option values for optionset attributes on the page.
SDK.AttributeSamples.showSelectedOption = function () { var html = "<!DOCTYPE html ><html lang='en-US' ><head><title>Selected Options</title>"; html += "<style type=\"text/css\">body { font-family:Calibri;}"; html += "table {border:1px solid gray; border-collapse:collapse;}"; html += "th {text-align:left; border:1px solid gray;}"; html += "td {border:1px solid gray;}</style>"; html += "</head><body>"; html += SDK.AttributeSamples.getSelectedOptions(); html += "</body></html>"; var myWindow = window.open("", "_blank", "height=400,width=350,scrollbars=1,resizable=1", false); myWindow.document.open(); myWindow.document.write(html); myWindow.document.close(); }; SDK.AttributeSamples.getSelectedOptions = function () { var fields = "<table summary='This table displays the selected option for optionset attributes on the page.'><thead><tr><th scope='col'>Name</th><th scope='col'>Selected JSON option</th></tr></thead><tbody>"; var attributes = Xrm.Page.data.entity.attributes.get(SDK.AttributeSamples.isOptionSet); for (var i in attributes) { var attribute = attributes[i]; var option = attribute.getSelectedOption(); fields += "<tr><td>" + attribute.getName() + "</td><td>"; if (option != null) { fields += " {"; fields += " text:\"" + option.text + "\" , value:\"" + option.value + "\""; fields += " }"; } else { fields += "This optionset attribute doesn't have a selected value."; } fields += "</td></tr>"; } fields += "</tbody></table>"; return fields; }; SDK.AttributeSamples.isOptionSet = function (attribute, index) { return attribute.getAttributeType() == "optionset"; };
getSubmitMode
Returns a string indicating when data from the attribute will be submitted when the record is saved.
If Xrm.Page.ui.getFormType() == 1 (Create), a ‘dirty’ submit mode will submit the value only if it is not null. If the record is being updated the ‘dirty’ submit mode will submit the value if it has changed.
Attribute Types: All
attributeObj.getSubmitMode()
- Return Value
-
Type: String
Returns one of the three possible values:
-
always
-
never
-
dirty
SDK.AttributeSamples.showSubmitMode = function () { var html = "<!DOCTYPE html ><html lang='en-US' ><head><title>Submit Mode </title>"; html += "<style type=\"text/css\">body { font-family:Calibri;}"; html += "table {border:1px solid gray; border-collapse:collapse;}"; html += "th {text-align:left; border:1px solid gray;}"; html += "td {border:1px solid gray;}</style>"; html += "</head><body>"; html += SDK.AttributeSamples.getSubmitModeValues(); html += "</body></html>"; var myWindow = window.open("", "_blank", "height=400,width=350,scrollbars=1,resizable=1", false); myWindow.document.open(); myWindow.document.write(html); myWindow.document.close(); }; SDK.AttributeSamples.getSubmitModeValues = function () { var fields = "<table summary='This table displays the submit mode values for attributes on the page.'><thead><tr><th scope='col'>Name</th><th scope='col'>Submit Mode</th></tr></thead><tbody>"; var attributes = Xrm.Page.data.entity.attributes.get() for (var i in attributes) { var attribute = attributes[i]; fields += "<tr><td>" + attribute.getName() + "</td><td>" + attribute.getSubmitMode(); + "</td></tr>"; } fields += "</tbody></table>"; return fields; };
-
always
getText
Returns a string value of the text for the currently selected option for an optionset attribute.
Attribute Types: optionset
attributeObj.getText()
- Return Value
-
Type: StringExample: The SDK.AttributeSamples.showSelectedOptionText function will display a page in a new window showing the text for selected option values for optionset attributes on the page.
Note When no option is selected, getText will return an empty string value. SDK.AttributeSamples.showSelectedOptionText = function () { var html = "<!DOCTYPE html ><html lang='en-US' ><head><title>Selected Option Text</title>"; html += "<style type=\"text/css\">body { font-family:Calibri;}"; html += "table {border:1px solid gray; border-collapse:collapse;}"; html += "th {text-align:left; border:1px solid gray;}"; html += "td {border:1px solid gray;}</style>"; html += "</head><body>"; html += SDK.AttributeSamples.getSelectedOptionsText(); html += "</body></html>"; var myWindow = window.open("", "_blank", "height=400,width=350,scrollbars=1,resizable=1", false); myWindow.document.open(); myWindow.document.write(html); myWindow.document.close(); }; SDK.AttributeSamples.getSelectedOptionsText = function () { var fields = "<table summary='This table displays the text of the selected option in optionset attributes on the page.'><thead><tr><th scope='col'>Name</th><th scope='col'>Selected Option Text</th></tr></thead><tbody>"; var attributes = Xrm.Page.data.entity.attributes.get(SDK.AttributeSamples.isOptionSet) for (var i in attributes) { var attribute = attributes[i]; fields += "<tr><td>" + attribute.getName() + "</td><td>" fields += attribute.getText(); fields += "</td></tr>"; } fields += "</tbody></table>"; return fields; }; SDK.AttributeSamples.isOptionSet = function (attribute, index) { return attribute.getAttributeType() == "optionset"; };
getUserPrivilege
Returns an object with three Boolean properties corresponding to privileges indicating if the user can create, read or update data values for an attribute. This function is intended for use when Field Level Security modifies a user’s privileges for a particular attribute.
Attribute Types: All
attributeObj.getUserPrivilege()
- Return Value
-
Type:Object
The object has three Boolean properties:
-
canRead
-
canUpdate
-
canCreate
SDK.AttributeSamples.showUserPrivileges = function () { var html = "<!DOCTYPE html ><html lang='en-US' ><head><title>User Privilege</title>"; html += "<style type=\"text/css\">body { font-family:Calibri;}"; html += "table {border:1px solid gray; border-collapse:collapse;}"; html += "th {text-align:left; border:1px solid gray;}"; html += "td {border:1px solid gray;}</style>"; html += "</head><body>"; html += SDK.AttributeSamples.getUserPrivileges(); html += "</body></html>"; var myWindow = window.open("", "_blank", "height=400,width=350,scrollbars=1,resizable=1", false); myWindow.document.open(); myWindow.document.write(html); myWindow.document.close(); }; SDK.AttributeSamples.getUserPrivileges = function () { var fields = "<table summary='This table displays the priviletges that the user has for each attribute on the page.'><thead><tr><th scope='col'>Name</th><th scope='col'>User Privileges</th></tr></thead><tbody>"; var attributes = Xrm.Page.data.entity.attributes.get(); for (var i in attributes) { var attribute = attributes[i]; var privileges = attribute.getUserPrivilege(); fields += "<tr><td rowspan=3>" + attribute.getName() + "</td>" fields += "<td> canRead: " + privileges.canRead + "</td></tr>"; fields += "<tr><td> canUpdate: " + privileges.canUpdate + "</td></tr>"; fields += "<tr><td> canCreate: " + privileges.canCreate + "</td></tr>"; fields += "</tr>"; } fields += "</tbody></table>"; return fields; };
-
canRead
getValue
Retrieves the data value for an attribute.
Attribute Types: All
attributeObj.getValue()
- Return Value
-
Type:Depends on type of attribute.
Example: The SDK.AttributeSamples.showValues function will display a page in a new window showing the values for each of the attributes on the page.Attribute Type Return Type boolean
datetime
To get the string version of a date using the Microsoft Dynamics CRM user’s locale preferences, use the format and localeFormat methods. Other methods will format dates using the operating system locale rather than the user’s Microsoft Dynamics CRM locale preferences.
decimal
double
integer
lookup
An array of lookup objects.
Note Certain lookups allow for multiple records to be associated in a lookup, such as the To: field for an e-mail entity record. Therefore, all lookup data values use an array of lookup objects – even when the lookup attribute does not support more than one record reference to be added. Each lookup has the following properties:
- entityType
- String: the name of the entity displayed in the lookup
- id
- String: The string representation of the GUID value for the record displayed in the lookup.
- name
- String: The text representing the record to be displayed in in the lookup.
memo
money
optionset
string
SDK.AttributeSamples.showValues = function () { var html = "<!DOCTYPE html ><html lang='en-US' ><head><title>Attribute Value</title>"; html += "<style type=\"text/css\">body { font-family:Calibri;}"; html += "table {border:1px solid gray; border-collapse:collapse;}"; html += "th {text-align:left; border:1px solid gray;}"; html += "td {border:1px solid gray;}</style>"; html += "</head><body>"; html += SDK.AttributeSamples.getAttributeValue(); html += "</body></html>"; var myWindow = window.open("", "_blank", "height=400,width=450,scrollbars=1,resizable=1", false); myWindow.document.open(); myWindow.document.write(html); myWindow.document.close(); }; SDK.AttributeSamples.getAttributeValue = function () { var fields = "<table summary='This table displays the values for each attribute on the page.'><thead><tr><th scope='col'>Name</th><th scope='col'>Type</th><th scope='col'>Value</th></tr></thead><tbody>"; var attributes = Xrm.Page.data.entity.attributes.get() for (var i in attributes) { var attribute = attributes[i]; var type = attribute.getAttributeType(); var value = attribute.getValue(); fields += "<tr><td>" + attribute.getName() + "</td>" fields += "<td>" + type + "</td>" switch (type) { case "boolean": case "decimal": case "double": case "integer": case "money": case "optionset": fields += "<td>" + value + "</td>"; break; case "memo": case "string": if (value != null) { fields += "<td>\"" + value + "\"</td>"; } else { fields += "<td>" + value + "</td>"; } break; case "datetime": if (value != null) { fields += "<td> toString() =\"" + value.toString() + "\"" + "<br /> toLocaleString() =\"" + value.toLocaleString() + "\"" + "<br /> format() =\"" + value.format() + "\"" + "<br /> localeFormat() =\"" + value.localeFormat() + "\"</td>"; } else { fields += "<td>" + null + "</td>"; } break; case "lookup": var data = "["; if (value != null) { for (var i = 0; i < value.length; i++) { data += "{<br /> entityType: \"" + value[i].entityType + "\", <br />"; data += " id: \"" + value[i].id + "\", <br />"; data += " name: \"" + value[i].name + "\" <br />"; if (i + 1 != value.length) { data += "},<br />"; } else { data += "}"; } } data += "]"; } else { data = value; } fields += "<td>" + data + "</td>"; break; } } fields += "</tbody></table>"; return fields; };
removeOnChange
Removes a function from the OnChange event hander for an attribute.
attributeObj.removeOnSave([function reference])
- Parameter
-
Type: function referenceExample:In this example, the JScript library contains two functions. Adding the removeMessageFromOnChange function to another form event will remove the displayMessage function as a handler for the OnChange event for the first attribute on the form.
function removeMessageFromOnChange() { Xrm.Page.data.entity.attributes.get(0).removeOnChange(displayMessage); } function displayMessage() { alert("message"); }
setRequiredLevel
Sets whether data is required or recommended for the attribute before the record can be saved.
Important |
|---|
| Reducing the required level of an attribute can cause an error when the page is saved. If the attribute is required by the server an error will occur if the there is no value for the attribute. |
Attribute Types: All
attributeObj.setRequiredLevel(requirementLevel)
- Arguments
-
Type: String
One of the following values:
-
none
-
required
-
recommended
-
none
Example: The SDK.AttributeSamples.makeOptionSetsRequired function will make all optionset attributes on the page required.
SDK.AttributeSamples.makeOptionSetsRequired = function () { var attributes = Xrm.Page.data.entity.attributes.get(SDK.AttributeSamples.isOptionSet); for (var i in attributes) { attributes[i].setRequiredLevel("required"); } }; SDK.AttributeSamples.isOptionSet = function (attribute, index) { return attribute.getAttributeType() == "optionset"; };
setSubmitMode
Sets whether data from the attribute will be submitted when the record is saved.
Attribute Types: All
attributeObj.setSubmitMode()
- Arguments
-
Type: String
One of the following values:
-
always
-
never
-
dirty
-
always
Example: The SDK.AttributeSamples.submitAllOptionsetData function will force all optionset attributes to be submitted. This is confirmed by checking getDataXml and displaying the result in an alert.
SDK.AttributeSamples.submitAllOptionsetData = function () { var attributes = Xrm.Page.data.entity.attributes.get(SDK.AttributeSamples.isOptionSet); for (var i in attributes) { attributes[i].setSubmitMode("always"); } alert(Xrm.Page.data.entity.getDataXml()); };
setValue
Sets the data value for an attribute.
Attribute Types: All
attributeObj.setValue()
- Arguments
-
Depends on the type of attribute.
Attribute Type Argument Type boolean
datetime
decimal
double
integer
lookup
An array of lookup objects.
Note Certain lookups, known as ‘partylist’ lookups, allow for multiple records to be associated in a lookup, such as the To: field for an e-mail entity record. Therefore, all lookup data values use an array of lookup objects – even when the lookup attribute does not support more than one record reference to be added. Each lookup value has the following properties:
- entityType
- String: the logical name of the entity represented by the lookup.
- id
-
String: The string representation of the GUID value for the record displayed in the lookup. The value should match the following format:
{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}.
- name
- String: The text representing the record to be displayed in in the lookup. Typically the primary attribute for the entity.
See Set Lookup Attribute Value for an example of a helper function you can create to set simple lookup attribute values.
memo
money
optionset
Note The getOptions method returns option values as strings. You must use parseInt to convert them to numbers before you can use those values to set the value of an optionset attribute. string
Note A String field with the email format requires that the string represents a valid e-mail address.
Example: The SDK.AttributeSamples.setAttributeValues function will attempt to set each attribute value on the form with a valid value.
Note |
|---|
| When setting the value of a lookup attribute, this sample uses the OData service to retrieve data about the first account record retrieved. This code will work for the OnLoad or OnChange events but will not work for the OnSave event. Because this method uses an asynchronous callback, the save event will complete before the callback function returns a value. |
SDK.AttributeSamples.setAttributeValues = function () { var attributes = Xrm.Page.data.entity.attributes.get(); for (var i in attributes) { var attribute = attributes[i]; //Do not update attributes for hidden controls if (!SDK.AttributeSamples.isAttributeControlHidden(attribute)) { var type = attribute.getAttributeType(); var oAttribute = attribute; switch (type) { case "boolean": //Set all Boolean attributes to 'true'; oAttribute.setValue(true); break; case "datetime": //Set all datetime attributes to 5 days in the future var today = new Date(); var futureDate = new Date(today.setDate(today.getDate() + 5)); oAttribute.setValue(futureDate); break; case "decimal": case "money": case "double": case "integer": //Set all number attributes to their maximum precision just less than 10. var preciseNumber; var precision = oAttribute.getPrecision(); switch (precision) { case 0: preciseNumber = 10; break; case 1: preciseNumber = 9.9; break; case 2: preciseNumber = 9.99; break; case 3: preciseNumber = 9.999; break; case 4: preciseNumber = 9.9999; break; case 5: preciseNumber = 9.99999; break; default: preciseNumber = 9.999999; break; } oAttribute.setValue(preciseNumber); break; case "lookup": //For a specific lookup where the entity type is known if (oAttribute.getName() == "parentaccountid") { SDK.AttributeSamples.setToFirstEntityRecord("AccountSet", "account", "Name", "AccountId", oAttribute); // NOTE: This function uses an asynchronous call to the OData service. // It works for the OnChange and Onload events – // but when used with the OnSave event, // It does not set the lookup value before the save action is completed. } break; case "memo": case "string": //Add text to String and memo fields //Email fields require a valid e-mail address value if (oAttribute.getFormat() != "email") { var textToAdd = "Text added by sample"; if (textToAdd.length <= oAttribute.getMaxLength()) { oAttribute.setValue(textToAdd); } } else { oAttribute.setValue("someone@microsoft.com"); } break; case "optionset": //Set options to the first option var firstOptionValue = oAttribute.getOptions()[0].value; if (firstOptionValue == "null") { var secondOptionValue = oAttribute.getOptions()[1].value; oAttribute.setValue(parseInt(secondOptionValue)); } else { oAttribute.setValue(parseInt(firstOptionValue)); } break; } } } }; SDK.AttributeSamples.setToFirstEntityRecord = function (entitySetName, entityName, nameAttribute, idAttribute, lookupAttribute) { try { var oReq = SDK.AttributeSamples.getXMLHttpRequest(); if (oReq != null) { oReq.open("GET", Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/" + entitySetName + "?$top=1&$select=" + nameAttribute + "," + idAttribute, true); oReq.setRequestHeader("Accept", "application/json"); oReq.setRequestHeader("Content-Type", "application/json; charset=utf-8"); oReq.onreadystatechange = function () { SDK.AttributeSamples.setLookupValue(oReq, lookupAttribute, entityName, nameAttribute, idAttribute); }; oReq.send(); } else { alert("AJAX (XMLHTTP) not supported."); } } catch (e) { alert(e.Message); } }; SDK.AttributeSamples.setLookupValue = function (oReq, lookupAttribute, entityName, nameAttribute, idAttribute) { if (oReq.readyState == 4 /* complete */) { if (oReq.status == 200) { var retrievedRecord = JSON.parse(oReq.responseText).d.results[0]; var olookup = {}; olookup.id = "{" + retrievedRecord[idAttribute] + "}"; olookup.entityType = entityName; olookup.name = retrievedRecord[nameAttribute]; var olookupValue = []; olookupValue[0] = olookup; lookupAttribute.setValue(olookupValue); } } }; SDK.AttributeSamples.getXMLHttpRequest = function () { if (XMLHttpRequest) { return new XMLHttpRequest; } else { try { return new ActiveXObject("MSXML2.XMLHTTP.3.0"); } catch (ex) { return new Error("AJAX (XMLHTTP) not supported."); } } }; SDK.AttributeSamples.isAttributeControlHidden = function (oAttribute) { var returnValue = false; if (oAttribute.controls.get(0) == null) { return true; } return returnValue; };
Set Lookup Attribute Value
The following example shows the definition of a setSimpleLookupValue helper function that sets the value for simple lookup attributes.
function setSimpleLookupValue(LookupId, Type, Id, Name) { /// <summary> /// Sets the value for lookup attributes that accept only a single entity reference. /// Use of this function to set lookups that allow for multiple references, /// a.k.a 'partylist' lookups, will remove any other existing references and /// replace it with just the single reference specified. /// </summary> /// <param name="LookupId" type="String" mayBeNull="false" optional="false" > /// The lookup attribute logical name /// </param> /// <param name="Type" type="String" mayBeNull="false" optional="false" > /// The logical name of the entity being set. /// </param> /// <param name="Id" type="String" mayBeNull="false" optional="false" > /// A string representation of the GUID value for the record being set. /// The expected format is "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}". /// </param> /// <param name="Name" type="String" mayBeNull="false" optional="false" > /// The text to be displayed in the lookup. /// </param> var lookupReference = []; lookupReference[0] = {}; lookupReference[0].id = Id; lookupReference[0].entityType = Type; lookupReference[0].name = Name; Xrm.Page.getAttribute(LookupId).setValue(lookupReference); }
The following is an example using the setSimpleLookupValue function to set the primarycontactid attribute value on an account form:
setSimpleLookupValue("primarycontactid", "contact", "{6D9D4FCF-F4D3-E011-9D26-00155DBA3819}", "Brian Lamee");
Because each attribute may be represented more than one time on the page, the controls collection provides access to all controls representing that attribute. If the attribute is represented by only one field in the page, the length of this collection will be 1.
The following sample represents a JavaScript library with two reusable functions that use the attribute controls collection:
-
SDK.Sample.hideAllAttributeControls: Hides all controls for the attribute.
-
SDK.Sample.showAllAttributeControls: Shows all controls for the attribute.
if (typeof (SDK) == "undefined") {SDK = { __namespace: true }; } SDK.Sample = { __namespace: true }; SDK.Sample.hideAllAttributeControls = function (attributeLogicalName) { /// <summary> /// Hides all controls for the attribute. /// </summary> /// <param name="attributeLogicalName" type="String" mayBeNull="false" optional="false" > /// The logical name of an attribute. /// </param> if ((typeof attributeLogicalName != "string") || (attributeLogicalName.length <= 3)) { throw new Error("SDK.Sample.hideAllAttributeControls attributeLogicalName parameter must be a string at least 4 characters long."); } Xrm.Page.getAttribute(attributeLogicalName).controls.forEach( function (control, i) { control.setVisible(false); } ); } SDK.Sample.showAllAttributeControls = function (attributeLogicalName) { /// <summary> /// Shows all controls for the attribute. /// </summary> /// <param name="attributeLogicalName" type="String" mayBeNull="false" optional="false" > /// The logical name of an attribute. /// </param> if ((typeof attributeLogicalName != "string") || (attributeLogicalName.length <= 3)) { throw new Error("SDK.Sample.showAllAttributeControls attributeLogicalName parameter must be a string at least 4 characters long."); } Xrm.Page.getAttribute(attributeLogicalName).controls.forEach( function (control, i) { control.setVisible(true); } ); }
To use these functions, pass the logical name of the attribute as shown here:
//Hide the controls for the subject attribute. SDK.Sample.hideAllAttributeControls("subject"); //Show the controls for the subject attribute. SDK.Sample.showAllAttributeControls("subject");
For more information about how to work with a controls collection, see Xrm.Page.ui.controls Collection.
Reference
Xrm.Page ReferenceConcepts
Form Scripting Quick ReferenceWrite Code for Microsoft Dynamics CRM Forms
Use the Xrm.Page Object Model
Other Resources
Sample: SDK.AttributeSamples.jsMicrosoft Dynamics CRM 2011 and Microsoft Dynamics CRM Online
Send comments about this topic to Microsoft.
© 2012 Microsoft Corporation. All rights reserved.