CRM 2011 Javascript Functions
Starting to use CRM 2011, here are some useful javascript reference (this list will get longer and longer as time goes):
Hide/Show Field
Hide/Show Field but removing white space
Disable/Not Disable Field
Set Requirement Level
Set Required Level
Validate Number Field and Character Limit
Validate Future DateTime
Example function to hide left navigation item based on a condition
Avoiding the checkbox OnChange bug so that it fires off immediately (put this in OnLoad event of the form)
Set Lookup Value
Hide Picklist Item Text
Hide/Show Field
1 2 3 4 5 6 | function HideShowField(fieldName, show) { var field = Xrm.Page.ui.controls.get(fieldName); if (field != null ) field.setVisible(show); } |
Hide/Show Field but removing white space
1 2 3 4 5 6 7 | function HideShowFieldRemoveSpace(fieldname, show) { var displayStyle = show ? 'block' : 'none' ; if (document.getElementById(fieldname + '_c' ) != null && document.getElementById(fieldname + '_d' ) != null ) { document.getElementById(fieldname + '_c' ).style.display = displayStyle; document.getElementById(fieldname + '_d' ).style.display = displayStyle; } } |
Disable/Not Disable Field
1 2 3 4 5 6 | function SetDisableField(fieldName, disabled) { var field = Xrm.Page.getControl(fieldName); if (field != null ) field.setDisabled(disabled); } |
Set Requirement Level
1 2 3 4 5 6 | function SetRequirementLevel(fieldName, reqLevel) { var field = Xrm.Page.getAttribute(fieldName); if (field != null ) field.setRequiredLevel(reqLevel); } |
Set Required Level
1 2 3 4 5 6 | function SetRequirementLevel(fieldName, reqLevel) { var field = Xrm.Page.getAttribute(fieldName); if (field != null ) field.setRequiredLevel(reqLevel); } |
Validate Number Field and Character Limit
1 2 3 4 5 6 7 8 9 10 | function validateNumberField(context, limit) { var value = context.getEventSource().getValue(); var fieldName = context.getEventSource().getName(); var labelName = Xrm.Page.ui.controls.get(fieldName).getLabel(); var valueRegex = new RegExp( "^\\d{" + limit + "}$" ); if (!value.match(valueRegex)) { event.returnValue = false ; alert( "The format of the " + labelName + " is incorrect" ); } } |
Validate Future DateTime
1 2 3 4 5 6 7 8 9 | function ValidateFutureDate(fieldname) { var today = new Date(); var dueDate = Xrm.Page.getAttribute(fieldname).getValue(); if (dueDate != null && dueDate < today) { alert( "Date value needs to be in the future" ); Xrm.Page.getAttribute(fieldname).setValue(today); } } |
Example function to hide left navigation item based on a condition
1 2 3 4 5 6 7 8 9 10 11 12 | function ShowBankDetails() { var payeeRole = "new_rolepayee" ; var payeeChkBox = Xrm.Page.ui.controls.get(payeeRole); var payeeChkBoxValue = payeeChkBox.getAttribute().getValue(); if (payeeChkBoxValue == true ) { Xrm.Page.ui.navigation.items.get( "nav_new_account_new_bankdetail" ).setVisible( true ); } else { Xrm.Page.ui.navigation.items.get( "nav_new_account_new_bankdetail" ).setVisible( false ); } } |
Avoiding the checkbox OnChange bug so that it fires off immediately (put this in OnLoad event of the form)
1 2 3 | function TriggerCheckBoxClick() { crmForm.all.new_checkbox.onclick = function () { crmForm.all.new_checkbox.FireOnChange(); }; } |
Set Lookup Value
1 2 3 4 5 6 7 8 9 10 11 | function SetLookupValue(fieldName, id, name, entityType) { if (fieldName != null ) { var lookupValue = new Array(); lookupValue[0] = new Object(); lookupValue[0].id = id; lookupValue[0].name = name; lookupValue[0].entityType = entityType; Xrm.Page.getAttribute(fieldName).setValue(lookupValue); } } |
1 2 3 4 5 6 7 8 9 10 11 | function HidePicklistItemWithText(fieldName, withText) { var picklist = document.getElementById(fieldName); for ( var i = picklist.options.length - 1; i >= 0; i--) { var picklistItemText = picklist.options[i].text; if (picklistItemText.match(withText) == withText && picklist.selectedIndex != i) { picklist.options.remove(i); } } } |
Where are the articles ?
ReplyDeleteSorry what articles are you referring to?
Delete