Showing posts with label Java Script. Show all posts
Showing posts with label Java Script. Show all posts

Tuesday, January 11, 2011

Loop through all elements on a CRM Form

Sometime we require to set all attributes on a form to disabled or checking which attribute has been changed.

To achieve this, you need to loop through each attribute of the CRM form. Below is the code snippet :


var iLen = crmForm.all.length;

for (i = 0; i < iLen; i++)
{
   o = crmForm.all[i];   switch (o.tagName)
   {
      case "INPUT":
      case "SELECT":
      case "TEXTAREA":
      case "IMG":
      case "IFRAME":
      if (o.id != "leadqualitycode")
      {
            o.disabled = true;
      }
      break;

      default:
            break;
   }
}

The above code will set all attributes to disabled.

Prevent CRM from saving a record.

The OnSave event is fired when a user presses the Save or Save and Close button on the form. The event is fired whether the data in the form has been modified or not.

Validating data is a common reason to use the OnSave event. The OnSave event can cancel the save operation before it is sent back to the server. To cancel the save operation, the script should return false as shown in the following code.

event.returnValue = false;

The SDK helps in this situation. Look at the following page for more information. MSDN SDK: OnSave Event







Thursday, January 6, 2011

Hide ISV Button.

I needed to hide a button that I added to my form using the ISV.config file. IN ISV file you can specify the button to be display in create / update mode. You can have only one tag per entity.

Here is the requirement:
I have to display 5 ISV button in the form. Out of which 4 to be displayed in both Create and Update mode. But there is one button which has to be displayed only in Update Mode. If you use below XML line in ISV.config file

It will display all button in both mode. But there is no provision where i can specify some of the button in create and update mode.
So I've to hide the button using Client side scripting on Load of the form.

Below is the fucntion which hides the button:
// HIDE ISV Button
function HideISVButton(strButtonToolTip){
   var tag = document.getElementsByTagName("LI");
   for(x = 0; x < tag.length; x++)
   {
      if(tag[x].getAttribute("title") == strButtonToolTip)
      {
         button = document.getElementById(tag[x].getAttribute("id"));
         if(button != null)
            button.style.display = "none";
         x = tag.length;
      }
   }
}

// Call the function to hide the button:
HideISVButton("Click this button to Line Activate");

The above code loops through all objects on the webpage with a tag name of "LI" looking for one that has a title of ‘Click this button to Line Activate’ which is specified in the ISV.Config file. Once the code finds the correct ToolTip of a button, it gets hidden.

Thursday, August 26, 2010

Declare Global Access Level functions in MS CRM Form.

Global functions in MS CRM Form.

The way CRM adds the javascript to the page, any function defined in the onload event will only have a local scope and can only be called from within the same onload event. However, if you put the function on the window object, then it should have global scope:
window.MyCustomFunction = function() { ... }

And then you should be able to call the function from an onchange event.

Ex:
// function name = MyCustomFunction
window.MyCustomFunction = function() {
    alert("My Global Level Function");
}

1. Go to MS CRM -> Customisations -> Customise Entity -> Choose Entity -> Form and View
2. Go to OnLoad()
3. Put the above fucntion onLoad() // Should be first statment.
4. Go to OnSave()
5. Call this function, MyCustomFunction();
6. When you try to save the entity record this fucntion will get executed.

Hope you enjoy this tip.



Wednesday, March 25, 2009

Global Javascript Variables in MS CRM 4.0


Global variables provide information about the Microsoft Dynamics CRM deployment and options chose by the user. Below Global variables you can use in ISV, Event Scripts of forms and across all CRM Web Pages.

The following table shows the available global variables.

SERVER_URL : Provides a string that represents the base server URL. When a user is offline, the SERVER_URL points to the local Microsoft Dynamics CRM Web services.

USER_LANGUAGE_CODE : Provides an LCID value that represents the Microsoft Dynamics CRM Language Pack that the user has selected.

ORG_LANGUAGE_CODE : Provides an LCID value that represents the Microsoft Dynamics CRM Language Pack that is the base language for the organization.

ORG_UNIQUE_NAME : Provides the unique text value of the organizations name.

Example
This script displays the values of these global variables.
alert("SERVER_URL="+SERVER_URL );
alert("USER_LANGUAGE_CODE="+USER_LANGUAGE_CODE);
alert("ORG_LANGUAGE_CODE="+ORG_LANGUAGE_CODE);
alert("ORG_UNIQUE_NAME="+ORG_UNIQUE_NAME);

Monday, January 19, 2009

Read and assign Owner attribute value to another owner attribute.


When it come to copying Owner attribute value aand assinging it to another owner attribute in CRM Form, its not just like a simple textbox value (where u can directly assing .DataValue property to the another attribute)

Have a look at the below code, which Reads and assign Owner attribute value to another owner attribute on CRM Form.

// Get hold of Owner and default it to the Commission Owner 1
var oOwner = new Array();
oOwner = null;

oOwner = crmForm.all.ownerid.DataValue;

//Create an array to set as the DataValue for the Owner control.
var lookupData = new Array();
var oCommissionOwnerOne = new Object();

if (oOwner[0] != null)
{
// Set the id, typename, and name properties to the object.
oCommissionOwnerOne.id = oOwner[0].id;
oCommissionOwnerOne.typename = oOwner[0].typename;
oCommissionOwnerOne.name = oOwner[0].name;

// Add the object to the array.
lookupData[0] = oCommissionOwnerOne;

// Set the value of the lookup field to the value of the array.
crmForm.all.new_commissionmarginownerone.DataValue = lookupData;
}


Happy copying (and Coding too) !!!!

Wednesday, September 24, 2008

CRM Form Types


Is the user creating a new record?
crmForm.FormType == 1

Is the user updating an existing record
crmForm.FormType ==2

Is the user unable to update this record?
crmForm.FormType == 3

Is this record deactivated?
crmForm.FormType == 4

Is the user using the Quick Create form?
crmForm.FormType == 5

Is the user using the Bulk Edit form?
crmForm.FormType == 6

What is the unique ID for this record?
= crmForm.ObjectId

What type of record is this?
= crmForm.ObjectTypeCode

What type of record is this (Entity Name)?
= crmForm.ObjectTypeName

Is the user using the Outlook Client?
crmForm.IsForOutlookClient==true

Is the user using the Outlook Light Client?
crmForm.IsForOutlookLightClient == true

Is the user working On line?
crmForm.IsOnline==true

Have any fields in this form been changed?
crmForm.IsDirty==true

Hide all Tabs on CRM Form



//Below is the javascript code, which will hide all tabs on CRM form.
document.getElementById("crmTabBar").style.display = "none";


//If you want to hide a specific tab, use below code:
document.getElementById("tab0Tab").style.display = "none";


//Here "tab0Tab" is first tab on the Crm Form.

Tuesday, September 23, 2008

How to attach onClose event to MS CRM Entity.


When you work with CRM Forms, if you want to capture / attach a onClose() Event, just use below javascript code.

window.onunload = function() {
//add code here
}

Tuesday, August 12, 2008

Set Default Value to a Lookup Attribute using JavaScript


Below Example will set the Default Primary Contact ID for Account Entity.

if(crmForm.all.primarycontactid.DataValue == null)
      
        //Create an array to set as the DataValue for the lookup control.

        var lookupData = new Array();

       //Create an Object add to the array.
        var lookupItem= new Object();
 
       //Set the id, typename, and name properties to the object.
        lookupItem.id = 'SPECIFY GUID of an Entity';
        lookupItem.typename = 'SPECIFY Entity Name';
        lookupItem.name = 'SPECIFY Attribute DISPLAY Value'; 
       // Add the object to the array.
          lookupData[0] = lookupItem; 
      // Set the value of the lookup field to the value of the array.
        crmForm.all.primarycontactid.DataValue = lookupData; 
} 

Saturday, September 29, 2007

Reference javascript file in Form_onLoad Event of an Entity.

Reference javascript file in Form_onLoad Event of an Account Entity.

Create a sub virtual directory under MS CRM Virtual root directory and place your .JS files in sub virtual directory.

E.G.,
Name of the sub virtual directory under MS CRM Virtual root directory is: "Customizations".
and Name of the JavaScript file you have created is "Account_Entity_Customizations.js"

Go to
Settings => Customizations => Custom Entitites => Select Account Entity => Open (double click) => Forms and Views => Form =>Form Properties => Edit OnLoad Event.

Copy paste below code in the OnLoad Event Box (select enable javascript event check box):

var oScript_Account = document.createElement("<script src='/Customizations/Account_Entity_Customizations.js' language='JavaScript'>");
 
document.getElementsByTagName("head")[0].insertAdjacentElement("beforeEnd", oScript_Account); 

Save and Publish the Entity.

Refresh MS CRM Portal.

Open Account Entity, you will see your JavaScript code working.

Happy Coding !!!!!!!!!!!!!!.