To hide the Ribbon toolbar of a form, use the below mentioned code snippet.
window.top.document.getElementById(“crmTopBar”).style.display = “none”;
MS CRM 2011 – Accessing Parent Form
If you want to access a parent form’s Xrm.Page object in MS CRM 2011, use the below mentioned code.
window.top.opener.parent.Xrm.Page
MS CRM 2011 – Auto populate unit on selection of a product in Opportunity Product
In MS CRM 2011, In the Opportunity Product form when a product is selected the unit field will not be automatically populated. So in order to populate value in the unit based on the product selected, we require a java script code to be written. So your function would look like the one mentioned below:
On product on-change, the unit field will still be read-only and hence the value set by you through code would not be assigned. So to solve this issue use the below mentioned code. On the Product on-change event call your SetUomId() method with a slight delay, so that the unit field would not be a read-only field when you set the value.
function ProductOnChange_SetUOM() { if (Xrm.Page.getAttribute("productid").getValue() != null) { SetUomId(); } }But there is a problem here. After placing the above code in your Product on-change event, still the unit value would not populate. Even though the Code written by you is absolutely correct, still unit value is not set. In such scenarios do not spend much time debugging or analyzing the problem, just follow the solution mentioned below.
On product on-change, the unit field will still be read-only and hence the value set by you through code would not be assigned. So to solve this issue use the below mentioned code. On the Product on-change event call your SetUomId() method with a slight delay, so that the unit field would not be a read-only field when you set the value.
function ProductOnChange_SetUOM() { if (Xrm.Page.getAttribute("productid").getValue() != null) { // delay of 100ms, because uomid is readonly window.setTimeout("SetUomId()", 100); } }
MS CRM 2011 – JavaScript – getUserRoles
getUserRoles
Returns an array of strings representing the GUID values of each of the security roles that the user is associated with.
Xrm.Page.context.getUserRoles()
Return ValueType: Array
Example: This user has only one security role associated with their user account. The JSON representation of
this array with a single value is: ["cf4cc7ce-5d51-df11-97e0-00155db232d0"]
Xrm.Page.context.getUserRoles()
Return ValueType: Array
Example: This user has only one security role associated with their user account. The JSON representation of
this array with a single value is: ["cf4cc7ce-5d51-df11-97e0-00155db232d0"]
MS CRM 2011 – JavaScript – Entity Methods – Xrm.Page.data.entity
addOnSave – Sets a function to be called when the record is saved.
Example:Xrm.Page.data.entity.addOnSave([function reference]) function addMessageToOnSave() { Xrm.Page.data.entity.addOnSave(displayMessage); } function displayMessage() { alert("message"); }getDataXml – Returns a string representing the xml that will be sent to the server when the record is saved.
Example:Xrm.Page.data.entity.getDataXml() In this example, the following three fields for an account record were updated:
name , accountnumber, telephone2
"<account> <name>Contoso</name> <accountnumber>55555</accountnumber> <telephone2>425 555-1234</telephone2> </account>"getEntityName – Returns a string representing the logical name of the entity for the record.
Example:Xrm.Page.data.entity.getEntityName() In this example, an account record was being edited. "account"getId – Returns a string representing the GUID id value for the record.
Example:Xrm.Page.data.entity.getId() The GUID Id value for the record. "{825CB223-A651-DF11-AA8B-00155DBA3804}"getIsDirty – Returns a Boolean value that indicates if any fields in the form have been modified.
Example:Xrm.Page.data.entity.getIsDirty()removeOnSave – Removes a function from the OnSave event hander.
Example:Xrm.Page.data.entity.removeOnSave([function reference]) function removeMessageFromOnSave() { Xrm.Page.data.entity.removeOnSave(displayMessage); } function displayMessage() { alert("message"); }save- Saves the record. This method has three possible parameters.
Example:Xrm.Page.data.entity.save( null | "saveandclose" |"saveandnew" ) Argumentssave()->
If no parameter is included the record will simply be saved. This is the equivalent of the user clicking the Save button in the ribbon.save("saveandclose")->
This is the equivalent of the user clicking the Save and Close button in the ribbon.save("saveandnew")->
This is the equivalent of the user clicking the Save and New button in the ribbon.
MS CRM 2011 – Set focus to a field
var field = Xrm.Page.ui.controls.get(“AttributeName”);
field.setFocus();
field.setFocus();
MS CRM 2011 – Show/Hide Tab/Section
Hide a tab/section
Xrm.Page.ui.tabs.get(1).SetVisible(false);
Show a tab/section
Xrm.Page.ui.tabs.get(1).SetVisible(true);
Show a tab/section
Xrm.Page.ui.tabs.get(1).SetVisible(true);
MS CRM 2011 – JavaScript – Attribute Methods – Xrm.Page.data.entity
Method - addOnChange
Applicable - All
Description - Sets a function to be called when the value is changed.
Applicable - All
Description - Causes the OnChange event to occur on the attribute so that any script associated to that event can execute.
Applicable - All
Description - Returns the type of attribute (string).
Applicable - All
Description - Returns formatting options for the attribute (string).
Applicable - boolean, optionset
Description - Returns the initial value for Boolean or optionset attributes.
Applicable - All
Description - Returns a Boolean value indicating if there are unsaved changes to the attribute value.
Applicable - money, decimal, integer, double
Description - Returns the maximum allowed value for an attribute (number).
Applicable - string, memo
Description - Returns the maximum length of a string or memo attribute (number).
Applicable - money, decimal, integer, double
Description - Returns the minimum allowed value for an attribute (number).
Applicable - All
Description - Returns the logical name of the attribute.
Applicable - optionset
Description - Returns an option object with the name matching the argument passed to the method.
Applicable - optionset
Description - Returns the valid options for an optionset attribute (an array of option objects).
Applicable - All
Description - Returns the entity object that is the parent to the attribute.
Applicable - money, decimal, double, integer
Description - Returns the number of digits allowed to the right of the decimal point.
Applicable - All
Description - Returns a string value indicating whether a value for the attribute is required or recommended.
Applicable - optionset
Description - Returns the option selected in an optionset attribute.
Applicable - All
Description - Returns a string indicating when data from the attribute will be submitted when the record is saved.
Applicable - optionset
Description - Returns the text for the currently selected option for an optionset attribute.
Applicable - All
Description - Returns an array of privileges that contain Boolean values indicating if the user can create, read or update data values for an attribute.
Applicable - All
Description - Retrieves the data value for an attribute.
Applicable - All
Description - Removes a function from the OnChange event hander.
Applicable - All
Description - Sets whether data is required or recommended for the attribute before the record can be saved.
Applicable - All
Description - Sets whether data from the attribute will be submitted when the record is saved.
Applicable - All
Description - Sets the data value for an attribute.
Applicable - All
Description - Sets a function to be called when the value is changed.
Example - Xrm.Page.getAttribute(“[Attribute Schemaname]”).addOnChange(displayMessage); function displayMessage() { alert("message"); }Method - fireOnChange
Applicable - All
Description - Causes the OnChange event to occur on the attribute so that any script associated to that event can execute.
Example – Xrm.Page.getAttribute(“[Attribute Schemaname]”).fireOnChange();Method - getAttributeType
Applicable - All
Description - Returns the type of attribute (string).
Example – Xrm.Page.getAttribute(“[Attribute Schemaname]”).getAttributeType();Method - getFormat
Applicable - All
Description - Returns formatting options for the attribute (string).
Example – Xrm.Page.getAttribute(“[Attribute Schemaname]”).getFormat();Method - getInitialValue
Applicable - boolean, optionset
Description - Returns the initial value for Boolean or optionset attributes.
Example – Xrm.Page.getAttribute(“[Attribute Schemaname]”).getInitialValue();
Method - getIsDirtyApplicable - All
Description - Returns a Boolean value indicating if there are unsaved changes to the attribute value.
Example – Xrm.Page.getAttribute(“[Attribute Schemaname]”).getIsDirty();Method - getMax
Applicable - money, decimal, integer, double
Description - Returns the maximum allowed value for an attribute (number).
Example – Xrm.Page.getAttribute(“[Attribute Schemaname]”).getMax();Method - getMaxLength
Applicable - string, memo
Description - Returns the maximum length of a string or memo attribute (number).
Example – Xrm.Page.getAttribute(“[Attribute Schemaname]”).getMaxLength();Method - getMin
Applicable - money, decimal, integer, double
Description - Returns the minimum allowed value for an attribute (number).
Example – Xrm.Page.getAttribute(“[Attribute Schemaname]”).getMin();Method - getName
Applicable - All
Description - Returns the logical name of the attribute.
Example – Xrm.Page.getAttribute(“[Attribute Schemaname]”).getName();Method - getOption
Applicable - optionset
Description - Returns an option object with the name matching the argument passed to the method.
Example – Xrm.Page.getAttribute(“[Attribute Schemaname]”).getOption([Attribute Value]);Method - getOptions
Applicable - optionset
Description - Returns the valid options for an optionset attribute (an array of option objects).
Example – Xrm.Page.getAttribute(“[Attribute Schemaname]”).getOptions();Method – getParent
Applicable - All
Description - Returns the entity object that is the parent to the attribute.
Example - Xrm.Page.data.entity.attributes.get(0).getParent()Method - getPrecision
Applicable - money, decimal, double, integer
Description - Returns the number of digits allowed to the right of the decimal point.
Example – Xrm.Page.getAttribute(“[Attribute Schemaname]”).getPrecision();Method - getRequiredLevel
Applicable - All
Description - Returns a string value indicating whether a value for the attribute is required or recommended.
Example – Xrm.Page.getAttribute(“[Attribute Schemaname]”).getRequiredLevel();Method - getSelectedOption
Applicable - optionset
Description - Returns the option selected in an optionset attribute.
Example – Xrm.Page.getAttribute(“[Attribute Schemaname]”).getSelectedOption();Method - getSubmitMode
Applicable - All
Description - Returns a string indicating when data from the attribute will be submitted when the record is saved.
Example – Xrm.Page.getAttribute(“[Attribute Schemaname]”).getSubmitMode();Method - getText
Applicable - optionset
Description - Returns the text for the currently selected option for an optionset attribute.
Example – Xrm.Page.getAttribute(“[Attribute Schemaname]”).getText();Method - getUserPrivilege
Applicable - All
Description - Returns an array of privileges that contain Boolean values indicating if the user can create, read or update data values for an attribute.
Example – Xrm.Page.getAttribute(“[Attribute Schemaname]”).getUserPrivilege();Method - getValue
Applicable - All
Description - Retrieves the data value for an attribute.
Example – Xrm.Page.getAttribute(“[Attribute Schemaname]”).getValue();Method - removeOnChange
Applicable - All
Description - Removes a function from the OnChange event hander.
Example – Xrm.Page.getAttribute(“[Attribute Schemaname]”).removeOnChange(displayMessage); function displayMessage() { alert("message"); }Method - setRequiredLevel
Applicable - All
Description - Sets whether data is required or recommended for the attribute before the record can be saved.
Example – Xrm.Page.getAttribute(“[Attribute Schemaname]”).setRequiredLevel(“required“);Method - setSubmitMode
Applicable - All
Description - Sets whether data from the attribute will be submitted when the record is saved.
Example – Xrm.Page.getAttribute(“[Attribute Schemaname]”).setSubmitMode(“always“);Method - setValue
Applicable - All
Description - Sets the data value for an attribute.
Examples – var today = new Date(); var futureDate = new Date(today.setDate(today.getDate() + 5)); Xrm.Page.getAttribute(“[Attribute Schemaname]”).setValue(futureDate); var textToAdd = "sample"; Xrm.Page.getAttribute(“[Attribute Schemaname]”).setValue(textToAdd); var olookup = new Object(); olookup.id = “[Guid]” olookup.entityType = “[entityName]”; olookup.name = “[Name]”; var olookupValue = new Array(); olookupValue[0] = olookup; lookupAttribute.setValue(olookupValue);
Format Phone number
//Handles 7, 10 and 11 digit phone numbers function formatPhoneNumber(val) { try { if (val == null) return ""; var fmtPhone = (val).replace(/^\s*|\s*$/g, ''); fmtPhone = fmtPhone.split(' ').join(''); if (fmtPhone.length == 10) { fmtPhone = fmtPhone.replace(/\D/g, '').replace(/^(\d{3})(\d{3})/, '$1-$2-'); } else if (fmtPhone.length == 7) { fmtPhone = fmtPhone.replace(/\D/g, '').replace(/^(\d{3})(\d{4})/, '$1-$2'); } else if (fmtPhone.length == 11) { fmtPhone = fmtPhone.replace(/\D/g, '').replace(/^(\d{1})(\d{3})(\d{3})/, '$1-$2-$3-'); } return fmtPhone; } catch (e) { alert("Error formatting phone number \"" + val + "\": " + e.message); } }
