by Webfortis
7. May 2012 08:59
One of the advantages of MS CRM 2011 is the introduction of Web Resources, which allows you to reuse JavaScript across mulitple Entities. By centralizing code that can be reused you reduce the amount of custom code that needs to be maintained and in this example you can create a function that completes the same procedure but allows for flexibility. In this example we want to centrally control the URL use by IFRAME located in different Entities. First step is to build a function that would allow for changing the URL based on variables being passed into the function.
Create a Web Resoucre JavaScript using the below code
var url1 = "http://sharepoint/sites/site1/Shared%20Documents/ ";
var url2 = “http://sharepoint/sites/site2/Shared%20Documents/";
function setIFrameSrc(IFrameName, ChangeContentURL)
{
if (!isNullOrEmptyString(IFrameName) && !isNullOrEmptyString(url) && !isNullOrEmptyString(value)) {
var iFrame = Xrm.Page.getControl(IFrameName);
if (iFrame != null && iFrame.getVisible()) {
if (typeof (ChangeContentURL) !== "undefined" && ChangeContentURL && !isNullOrEmptyString(url2)) {
url1 = url2
}
iFrame.setSrc(url 1);
}
}
}
Once you have the Script created with the function that was outlined above ensure that you load it above the Script in which you are going to invoke it.
From the OnLoad event of a entity you can invoke function with a simple call. To load url1 do not add in a ChangeContentURL value as part of invoking the function.
setIFrameSrc("IFRAME_Documents");
Changing the URL you simply have to add a value as part of invoking the function. By passing a true into the ChangeContentURL variable in the function it will step into the if statement and change to url2.
setIFrameSrc("IFRAME_Documents", true);
by Chuck Oldes, MCT, MCTS, MCSE
7. April 2012 16:02
One area that Microsoft CRM 2011 needs assistance in is establishing data input standards and validation. Simple data entry points can quickly start to lose shape if a system administrator doesn’t apply framework to ensure that the application users input data matches the desired requirements. Things like formatting a Phone Number or checking a Zipcode can go a long way to ensuring the data in a system is reliable and presented in a user friendly format, as well as making the data present correctly when used in views and reports.
Because MS CRM 2011 uses JavaScript as its client side scripting language you can rely on Regular Expression Objects (RegExp) to perform these sorts of tasks. I prefer not to restrict a user from entering data, but rather collect the data and if it matches an expression, ensure it is stored in a predetermined format. There are also many things to consider, when determining how to produce the data in the predetermined format. Consider a phone number, in some cases an application user might enter a seven digit number without the area code which would be less then desirable.
Following the North American Numbering Plan (NANP), long distance United States number would start with a 1 as the Country Code followed by the area code, then the prefix and then finally the last 4 digit of the number. So in this example we will assume that the Contacts in the application are primarily from the United States and it is the desire to format the number in the following format. +1 (XXX) XXX-XXXX, this format presents nicely for views and reports.
Phone Number entered into a field without formatting assigned:
The first step to setting up a feature to review a Phone Number that has been entered is to provide a function. The function will test what was entered by the application user, and if it matches the desired expression it will return it formatted. When adding these sorts of functions, it is best to design them to be used universally and not tied to a specific data attribute. In the example below a string variable is passed into the function to be tested against the expression. The method which you store these sorts of scripts in MS CRM 2011 are outside the scope of this Blog discussion, but will ensure we include that topic in a future blog.
function formatPhone(phonenum)
{
var regexObj = /^(?:\+?1[-. ]?)?(?:\(?([0-9]{3})\)?[-. ]?)?([0-9]{3})[-. ]?([0-9]{4})$/;
if (regexObj.test(phonenum))
{
var parts = phonenum.match(regexObj);
var phone = "";
if (parts[1]) { phone += "+1 (" + parts[1] + ") "; }
phone += parts[2] + "-" + parts[3];
return phone;
}
else
{
return phonenum;
}
}
Once you have establish the function you would simply call it from the onChange event of the data attribute you wish to test and format.
function phone_OnChange()
{
if (Xrm.Page.getAttribute("new_phonenumber").getValue() != null)
{
var startphnnum = Xrm.Page.getAttribute("new_phonenumber").getValue();
var finishphnnum = formatPhone(startphnnum);
Xrm.Page.getAttribute("new_phonenumber").setValue(finishphnnum);
}
}
Phone Number that has been passed to the formatting function:
You can enforce the entry of data in a certain format by clearing the value and triggering an alert that warns the application user that the data they entered matched the requirement. Caution should be used if enforcement is deployed. Ensure that you conduct comprehensive testing of your expressions as not to block valid data.
Sample Regular Expression Object string:
Zipcode var regexObj = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
Email var regexObj = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
Note that the Email Regular Expression above determines if the string appears to look like an email, there is a standard that outlines exactly what a constitutes a valid email. That standard is outlined in RFC2822 and to achieve that standard additional development would need to occur on the provided expression.
by Travis Sharp
28. February 2012 01:13
When customizing CRM for your business, you will undoubtedly run into the need for form-level customizations and business logic. In this tutorial we will be covering basic "Attribute" & "Control". Interactions in this tutorial which will help with implementing basic calculated fields. In order to understand this post, please read "A Brief Introduction to CRM 2011 JavaScript". This will cover creating and loading web resources into a solution as well as some other useful scripting resources.
Tutorial Scenario:
You need to trigger a calculation on a form when a field is updated
The user cannot edit the calculated field
Identified Needs:
Destination field needs to be disabled
Trigger field needs a trigger event
A web resource is needed to store the JavaScript
Each field has events associated with its control, you can see which triggers are associated with any control by opening the form customizations for any given entity and “Double Clicking” on a field.
NOTE: An attribute refers to the actual JavaScript object that contains data about the field, the control contains information about the base DOM element, visibility and whether or not a field is disabled.
Step 1: Disable field
Step 2: JavaScript Needed
I've named my fields new_triggerfield & new_calculatedfield. This function will update the calculated field when called. NOTE: I've created a web resource for this, and added it to the form.
function UpdateField() {
// Trigger Field
var trig = Xrm.Page.getAttribute("new_triggerfield");
// Destination Field
var dest = Xrm.Page.getAttribute("new_calculatedfield");
// Test to make sure the trigger field has a value, this will return null if blank.
if(trig.getValue() != null)
{
// Perform the calculation
var newValue = trig.getValue() * 3.14;
// Set the destination field's value to the new value (NOTE: To clear - dest.setValue(null);
dest.setValue(newValue);
}
}
Step 3: Add the event and test the changes
Now we can test the calculations, we should see results when the field loses focus.
Unfortunately, when we save the form and open it again we discover that the changes did not save! To fix this we must add an additional statement in order to save the changes back to the server. This happens because the field is disabled.
Step 4: Update Script To Save Changes
function UpdateField() {
var trig = Xrm.Page.getAttribute("new_triggerfield");
var dest = Xrm.Page.getAttribute("new_calculatedfield");
if(trig.getValue() != null)
{
var newValue = trig.getValue() * 3.14;
dest.setValue(newValue);
// Make sure the changes are saved back to the server - in CRM 4.0, this was crmForm.new_fieldname.ForceSubmit = true;
Xrm.Page.getAttribute("new_calculatedfield").setSubmitMode("always");
}
}
Finished: - Key Points
Updating field data by schema name
Retrieving field data by schema name
Disabling fields through UI
Force submit disabled field data
by Travis Sharp
15. August 2011 09:22
When CRM 2011 was rolled out, so was a new method of CRM JavaScript. Now available are libraries, solutions, web resources, etc. that you can use to attain fine-grained control over all of your customizations. With the introduction of web resources you can now load any number of compartmentalized libraries into your forms that can give you that extra feature or business logic that you need.
OnLoad, OnSave, OnChange ... what is that?
(CRM 4 Customizers Can Skip This)
OnLoad: JavaScript programmers will be used to seing something similar to this. This is the event that is triggered when your form is loaded but before any data begins to show. All form fields, aside from subgrids and iFrames, are available to you through the DOM.
OnSave: The OnSave event is triggered when the form Save or Save & Close button is clicked.
OnChange: The OnChange event is triggered when the field loses focus after the contents are modified. Additional methods will be explained in a future blog post as advanced events are beyond the scope of this post.
Form Customization
We will be begin by examining the form customization options on a form. It is important to note that you should use solutions rather than modifying the base system directly; however, for the sake of brevity we will save the topic of solutions for a later post. Once you have opened an entity form to customize, you will see a navigation bar at the top, aka the "Ribbon," which will aid you in all of your customizations. Currently we are only interested in form properties.
The form properties window will allow you to add additional scripts, aka "libraries," to your form. It also allows you to manage event handlers for the entire form, most notably OnLoad, OnSave and OnChange. CRM 4 Developers should take note of their new location. If we click the add button in the form libraries section, we will see all available libraries that we can add to the form and can create a new library if we wish. NOTE: Script libraries are managed in the Web Resources section of your solution.
Namespaces
Xrm.Page - Xrm.Page is the new crmForm, or if you are just starting: Xrm.Page is the JavaScript namespace where you can access CRM form Components ( UI Elements ) and fields.
Xrm.Page.ui - This namespace contains functions for manipulation of the form UI. This includes tabs, sections and fields.
Xrm.Page.context - Provides access to the context object. The context object provides methods to retrieve information specific to an organization, a user, or parameters that were passed to the form in a query string. ( http://msdn.microsoft.com/en-us/library/gg328399.aspx )
Xrm.Page.data.entity - Provides methods to retrieve information specific to the record displayed on the page, the save method and a collection of all the attributes included in the form. Attribute data is limited to attributes represented by fields on the form. ( http://msdn.microsoft.com/en-us/library/gg334720.aspx )
* Note: There are more methods, namespaces and general objects, but these are sufficient for an introduction.
* Note: Calling crmForm from the IE debugging tools no longer functions as it once did. To obtain the old functionality for debugging purposes, use:
crmForm = window.frames[0].crmForm;
Xrm = window.frames[0].Xrm;
It is also important to note that when developing for CRM 2011, you should use the "Xrm" methods rather than the older crmForm methods in your javascript.
Xrm.Page ( formerly crmForm )
Xrm.Page is the new crmForm, or if you are just starting: Xrm.Page is the JavaScript namespace where you can access CRM form Components ( UI Elements ) and fields. Almost everything that you need to do to the CRM form you can get to through here. Of note, crmForm still does exist and can be used within scripts.
Xrm.Page.getAttribute("new_field");
This function returns an attribute on the form using the lowercase schema name. The object that it returns can be used to update the field data, fire onChange, tell if it is dirty, add additional CRM appropriate events, etc.
Xrm.Page.getControl("new_field");
This retrieves a control of a field, which can be used to change visibility, enable or disable the field, get the type, etc.
Xrm.Page.ui
Xrm.Page.ui.getFormType();
This returns a numeric value for the type of form that is currently being displayed, i.e.: update, create, etc. (The exact numbers corresponding to each type are not covered in this post.)
Xrm.Page.ui.getViewPortWidth();
Returns how many pixels wide the view is.
Xrm.Page.ui.getViewPortHeight();
Returns how many pixels high the view is.
Xrm.Page.ui.getCurrentControl();
Returns the current active control.
Xrm.Page.ui.controls.get("new_field");
Works just like Xrm.Page.getControl() except the Xrm.Page.ui.controls namespace has a few more functions for dealing with controls.
Xrm.Page.ui.tabs.get("tabName");
This works like getControl except that it is for tabs. It will return a tab object that you can use to change the properties of the tab, the most useful of which include the visibility and the sections object.
Xrm.Page.context
Xrm.Page.context.getUserId();
This will return the GUID of the current user
Xrm.Page.context.getUserRoles();
Returns the current user's role/roles.
Xrm.Page.context.isOutlookClient();
Returns whether or not a user is in the Outlook client. If you need to use a different version of a javascript function or Outlook does not support a particular feature, you can use this.
Xrm.Page.context.isOutlookOnline();
For the times when you need to determine if the outlook client is online, use this. e.g.: If you have an oData call to retrieve certain data from somewhere else in the system or you have mapping code that depends on an external web service, use this to determine if you're online.
Xrm.Page.context.getOrgUniqueName();
Returns the unique org name for the current active organization. This is helpful for On-Premise installations that have multiple organizations or online instances.
Xrm.Page.context.getServerUrl();
Returns the url of the server.
Xrm.Page.context.getAuthenticationHeader();
Returns the authentication header required to make SOAP calls.
Xrm.Page.data.entity
Xrm.Page.data.entity.getId();
Returns the current entity's GUID
Xrm.Page.data.entity.getEntityName();
Returns the current entities schema/logical name.
Xrm.Page.data.entity.save();
Forces a manual save of the entity. - Note: this function can also take an "Action"
Xrm.Page.data.entity.getIsDirty();
Determines whether or not the form is dirty, i.e. if there are any unsaved changes.
Additional references for the Xrm.Page.* objects/namespaces can be found at: http://msdn.microsoft.com/en-us/library/gg334351.aspx