Error loading external JavaScript file on Win2k8, IIS7.0

May 16, 2011 by · Leave a Comment 

I’m using the following code in the entity’s OnLoad event to load an external JavaScript file in MS CRM.

===================================
function LoadFile(url, cache)
{
var httpRequest = new ActiveXObject('Msxml2.XMLHTTP');
httpRequest.open('GET', url, false);
if (!cache) {
   httpRequest.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
}
httpRequest.send(null);
return httpRequest.responseText;
}
LoadFile('/ISV/scripts/accounts.js', false)

We recently migrated our CRM Server to Windows 2008, with IIS 7.0.
This worked fine on Windows 2003 and IIS 6.0, but on Windows 2008 and IIS 7.0, we get the following error:

image

Apparently the error is caused by the header If-Modified-Since and it turned out, the date should have a starting zero in it (01, not 1), see line 07.

httpRequest.setRequestHeader('If-Modified-Since', 'Sat, 01 Jan 2000 00:00:00 GMT');

The script has been tested and now works on both Win2k3 and Win2k8.
I want to thank Jevgenij Martynenko for finding this out and sharing it online.

Dynamically make a field required or recommended

April 11, 2011 by · Leave a Comment 

This function can be used to dynamically make a field required or recommended in JavaScript. Copy the function below in the onload and call it using one the examples in the comments section.

//****************************************************
gSetFieldRequired = function (field, src, height, width){
 
 /*
 Description: This function is used to dynamically make a field required, recommended in JavaScript
 Params:    field  = name of the field
   src  = url with image eg. /_imgs/frm_required.gif
   height =  number, optional e.g. 50
   width  =  number optional e.g. 50
 Returns:   nothing
 Example 1 :  gSetFieldRequired(, 'new_partweight', 'required');     = add *-sign to label 
 Example 2:   gSetFieldRequired( 'new_partweight', 'recommended');     = add +-sign to label 
 Example 3:   gSetFieldRequired(, 'new_partweight', 'none');      = no image
 Calls:    nothing
 Author:  Geron Profet
 */

 var attribute = document.getElementById(field);
 if (!attribute){return};
 
 switch(src.toLowerCase())
 {
 case 'required':
  src = '/_imgs/frm_required.gif';
  attribute.setAttribute('req', 2);
  break;
 case 'recommended': 
  src = '/_imgs/frm_recommended.gif';
  attribute.setAttribute('req', 1);
  break;
 case 'none': 
  src = ''
  attribute.setAttribute('req', 0);
  break;
 }

 //if src is passed add image
 if (src != '') {
  var img = document.createElement("img");
  img.setAttribute('src', src);

  //check if custom height and width
  if (height && height != '' ){img.setAttribute('height', height);}
  if (width && width != '' ){img.setAttribute('height', width);}
  //img.setAttribute('width', width);

  var objLabel = document.getElementById(field+'_c');
  if (objLabel){objLabel.appendChild(img)};
 }
 else{ 
  document.getElementById(field+'_c').innerHTML = document.getElementById(field+'_c').innerText;
 }
}

Change the form title in MS CRM via JavaScript

February 15, 2011 by · Leave a Comment 

In MS CRM the document title and form title defaults to the primary name attribute of an entity. Sometimes you want to display a different name e.g. to display both the account name and account number. The originale idea came from: http://mscrm4ever.blogspot.com/2008/10/finding-crmform-elements-by-css-class.html

I made this function a bit more generic and changed it so you can now rename both the ms-crm-Form-Title as well as the document.title.  Copy the function below in the Onload event and call it using the example:

Read more

Dynamically filter a picklist in MS CRM – Part 2

February 14, 2011 by · 1 Comment 

Last year I wrote an article explaining how to filter a picklist based on another picklist in MS CRM. In this article I’ll explain how to filter a picklist based on an array of values. The original idea for this approach came from Greg Owens on: The CRM Grid – A Microsoft CRM Blog.

I made this function a bit more generic, you can simply pass a array of item values to filter your  picklist.

Code the code in the Onload event and use the example to filter the picklist:
Read more

How to get the Entity Type Code via the Metadata Webservice in JavaScript

January 25, 2011 by · 3 Comments 

Sometimes you want to fill in a default value in a lookup field. In order to do so you need to specify the Id, Value and Object Type Code, also known as Entity Type Code, of the entity (for an example see gLookupSetField() on our site).

The ObjectTypeCode of custom entities might be different when you deploy your customizations across different environments such as Development, Test , Acceptance or Production. You do not want to hard-code this ObjectTypeCode in your JavaScript.

The following function allows you to dynamically retrieve the ObjectTypeCode for any entity in JavaScript. In addition, this function is also capable of returning other metadata attributes for an entity e.g. Display Name. For a complete list op properties see http://msdn.microsoft.com/en-us/library/bb890357.aspx.

Security Roles
You must make sure that the appropriate security roles have read privileges on the metadata. Open each security role go to tab Customization and give read permission to the following options Entity, Attribute and Relationship, see example below:

    image

Note:
Only direct properties of the entity (EntityOnly) are returned, thus no arrays of attributes, relationships or privileges are returned !!

Code the code in the Onload event and call it using the examples below:

Read more

Execute workflow in JavaScript and wait for it to complete

December 17, 2010 by · Leave a Comment 

Sometimes you want to launch a workflow in JavaScript and wait for it to complete. Andriy Butenko wrote a script that would wait for a workflow to complete in JavaScript.

Scenario
Our customer requested that an email would be created via a workflow and that the user is notified immediately when the email is saved so that he could add some personal notes to the email before sending it out. This required that the form would remain open.

Solution
The system checks if certain fields are filled after which a workflow is started via JavaScript. Immediately, the user gets a notification – using gAddNotification() – that the email is being created. The form close event is set to false so that the user can not close the form. Once completed the system pops up an alert that the email is saved and the activities screen is displayed showing the newly created email. Finally, the form close event is set to true allowing the user to close the form once again.

image

Instructions

  1. Copy the function below in the Onload event.
  2. Create a global function called workflowOnCompleted (see example below) to place logic that needs to run after completion of the workflow.

Note: this function calls another function named gExecuteWorkflow() on our blog.

Read more

Retrieve one or more fields using FetchXML

December 17, 2010 by · Leave a Comment 

This function is used to retrieve multiple fields in MS CRM via fetchXml and return an array containing the requested values.

The function requires the following parameters:

  • xml            = fetchXml
  • arrFields   = array of attributes to return

When multiple records are found you can request the value of the attribute (example 1), recordcount (example 2) or the entire recordset (example 3). All these items are returned in an array whereby:

  • array [0] = record count
  • array [1] = array: to retrieve MULTIPLE records and multiple fields. The entire recordset (nodes) and fields (childnodes) are returned.
  • array [2] = values: Use to retrieve ONE record and multiple fields. (NOTE: if multiple records are returned it will return the last !! )

Instructions:

To determine the exact fetchxml and fetchlayout follow this procedure:

  1. Use FetchXMLBuilder ( www,fetchxmlbuilder.codeplex.com) to build your fetchXML , or…
  2. Build query in Advanced Find and follow procedure below to get the fetchXML:
  •  
    • Go to advanced find and press F11 to get the address bar.
    • Create the query and layout
    • In address bar paste this line of code:
      javascript:void( new function(){ prompt("Fetch Parameters:",getFetchParams());function getFetchParams(){ return "FetchXml:\n" + advFind.FetchXml + "\n\n" + "LayoutXml:\n" + advFind.LayoutXml + "\n\n" + "EntityName:\n" + advFind.EntityName + "\n\n" + "DefaultAdvancedFindViewId:\n" + advFind.DefaultAdvancedFindViewId } } )
    • Click Enter, all parameters are now in your clipboard and can be pasted in your code

Copy the code below in the OnLoad and see the examples below on how to call the function:

Read more

How to set an attribute’s IsDirty back to false and avoid "Your changes have not been saved" dialog?

December 8, 2010 by · 3 Comments 

Sometimes you need to set a default value on an attribute. When you close the form without making any changes the user will get the “Your changes have not been saved” dialog (In dutch: “Weet u zeker dat u deze pagina wilt sluiten…”).

There is a way to avoid this. Place the following code in the onchange event of the attribute in question:

if(crmForm.all.<your fieldname>.IsDirty){
       crmForm.detachCloseAlert();
} 

Note:
This does not turn off the message alltogether.
The message will still appear if the user makes changes to other fields (tested on CRM v4.0).

Another great post on form Scripting in CRM 2011

November 12, 2010 by · Leave a Comment 

Found on CRM Consultancy Blog:

The ability to insert Javascript into Client-side Events was one of the most common ways of extending MSCRM’s functionality, allowing bespoke logic to be implemented with a small amount of development effort. This largely acted as mid-way development between Functional Customisation and more Technical Development for Server-side Plugins and Custom Workflow Actions.

Continue to read this article…

Creating custom link fields in MS CRM 4.0

November 2, 2010 by · Leave a Comment 

David Vidmar came up with a script to convert a text field into a clickable email link (see below).

image

This inspired me convert this into a more a generic function.
Copy the code in the Onload event:


//*********************************************************
gConverTextToEmail=function(field){
 /*
 Description:   Funcion to convert a nvarchar (text) field into a clickable email  link
 Calls:   nothing
 Returns:   nothing
 Example:  gConvertTextToEmail('<your_textfield>');
 */

 var emailTextBox = document.getElementById(field);
 emailTextBox.style.color = "#0000ff";
 emailTextBox.style.textDecoration = "underline";
 //emailTextBox.attachEvent("onclick", openEmail1);   //activate this link if you want a single click
 emailTextBox.attachEvent("ondblclick", openEmail1); 
 emailTextBox.attachEvent("onmouseover", mouseover);
 emailTextBox.attachEvent("onmouseout", mouseout);

 //*********************************************************
 function openEmail1(){ 
  if (emailTextBox.DataValue != null){   
   document.location = "mailto:" + emailTextBox.DataValue;
  }
 }

 //*********************************************************
 function mouseover() {
  emailTextBox.style.cursor = 'hand';
 }
 //*********************************************************
 function mouseout() {
 emailTextBox.style.cursor = 'auto';
 }
} 
//now call the function
gConvertTextToEmail('<your_textfield>'); 

Next Page »