Tuesday, December 1, 2009

How to access frame level attribute(fields) from CRM form?

Suppose we have used the iFrame in CRM form to access outside pages or even CRM pages, and we need to access any field of the form in iFrame to populate the crm values in that field or to get the field value etc.

Suppose the iFrame used is "IFRAME_Name" and the field in the form of IFRAME is "testField".
The following can get the object from thr form of IFRAME

var objTestField = crmForm.all.IFRAME_Name.contentWindow.document.getElementById("testField");

How to Export more than 10,000 records ?

The default setting for MS CRM for exporting to excell from a view is maximum 10,000 records.
If you have than 10,000 records it will export the 1st 10,000 records only, where as the rest wo't be exported.
So just a change can make a great difference .
Follow the following steps

1. Log-in to the SQL Server where the _MSCRM database is stored
2. Open the "OrganizationBase" table
3. Find the Column: "MaxRecordsForExportToExcel" (it will be towards last)
4. Change the value from 10,000 to the desired value
5. Commit the change to the database
6. If required start SQL & IIS also.

After that you will get it done.

Adding button on CRM Form

We all know that CRM gives some listed attributes(fields).
Suppose we need a button(which is not avail in MS CRM as attribute), suppose for any purpose like onclick of button another pop-up will open(which can save a tab,ofcourse).

>> for this we need a place holder(suppose a textbox) named new_buttonName.

//this will create a button with label "Click 4 Pop-up" inplace of the place holder(textbox)
if(crmForm.all.new_buttonName != null){
crmForm.all.new_buttonName_d.innerHTML = '';
crmForm.all.searchButton.attachEvent("onclick", _openPopup);
crmForm.all.sdv_googlelocationbutton_c.innerHTML = " ";
}

//this function will be called on click of the button created earlier
function _openPopup(){
window.open("http://positiveedge.net","PositiveEdge","height=500,width=950,scrollbars=1");
}

Increasing the no of Tabs on a form

Most of us aware of it , that the no. of tabs on a form is not unlimited.(If not aware keep it in mind).The limitation is 8 tabs max.
So if at any instance there is requirement of more than 8 tabs(index actually started from 0-7), then we need to make our fingure bend(even if its going against and risky).

Just need to be changed a variable in a file as follows.

find "formeditor.aspx" from the Tools\FormEditor of the installed CRM instance.
open in edit mode and find the javascript variable "“_iMaxTabs", wchich is set to 8.
change it to your desire value.
save and close and reset IIS.
there you go.

get form GUID Means the current record GUID

sometimes we need the record's guid on client side, may be for the use of fetchXML query or other use.
Its very2 simple to get it done.

var objectidguid = crmForm.ObjectId;

MARK BUSINESS REQUIRED MANUALLY

Sometimes we need to make the attributes(fields) "Business Required" manually , when the crmForm.SetFieldReqLevel("fieldname", 1); stops working ie any changes has been done in the attribute level.

//for business required which will put a red asterisk side to the attribute
//it only takes one parameter as new_attributeName_c
function markRequired(lbl)
{
if(lbl !=null){
if(lbl.childNodes[0] != null && lbl.childNodes[0].innerText != undefined){
html = lbl.childNodes[0].innerHTML +"<IMG alt='Required' src='/_imgs/frm_required.gif'/>";
}else{
html = lbl.innerHTML +"<IMG alt='Required' src='/_imgs/frm_required.gif'/>";
}
lbl.innerHTML = html;
}
}

//for not business required which will remove the red asterisk side to the attribute
//it only takes one parameter as [i]new_attributeName_c
function markOptional(lbl) {
if(lbl !=null){
if(lbl.childNodes[0] != null && lbl.childNodes[0].innerText != undefined){
html = lbl.childNodes[0].innerText ;
}else{
html = lbl.innerText ;
}
lbl.innerHTML = html;
}


//on save also we need to check it manully and return back to the same form without saving the details and
//the focus need to be on the required attribute(field) provided the field should not be read-only
if(crmForm.all.new_attributeName.DataValue == null){
crmForm.all.tabName.click();
crmForm.all.new_attributeName.focus();
alert("Attribute is Required.");
event.returnValue = false;
return false;
}

//[i]Just some Re-engineering will give a lot

Default value for lookups

Hi, the following with some care will help to populate the default value for lookup fields( as we can do for picklist and text boxes).

for this the record should present earlier.
and we need to get the following 3 values.
GUID,ObjectTypeCode for entity,displayValue.

onchange of the lookup attribute add the following code and capture the 3 values as follows.

var lookupItem = new Array;
lookupItem = crmForm.all..DataValue;
// If there is data in the field, show it in a series of alerts.
if (lookupItem[0] != null){
// The text value of the lookup
alert("Display Value"+lookupItem[0].name);
// The GUID of the lookup
alert("GUID"+lookupItem[0].id);
// The entity type name. by using the entityname get the ObjectTypeCode from
//the MetadataSchema.Entity table
alert("Entity Name"+lookupItem[0].typename);
}

by using the entity name get the "ObjectTypeCode" from the "MetadataSchema.Entity" table from DB
now its ready to call the function.
call it onLoad of the form and it need to be done on creation of new record.

//the following will take 4 parameters
//1st GUID of the record to be set as default
//
function setDefault4Lookup(guid,entityName,displayValue,attributeID)
{
var lookupItem = new Array();
// Values on the signature of LookupControlItem are: GUID, type code and finally the name of the
//lookup value
lookupItem[0] = new LookupControlItem (guid, entityName, displayValue);
//Set the form control value to the lookupItem just created
attributeID.DataValue = lookupItem;
}

Hide a full Section

Some times we have also some requirements like to hide or show a full section which conatins some bunch of attributes(fields) as per the user action.
for this we need to catch a particular attribute of that particular section.Here its "new_attributeName"

//hiding a section
crmForm.all.new_attributeName_c.parentElement.parentElement.parentElement.style.display = "none";

//showing a section
crmForm.all.new_attributeName_c.parentElement.parentElement.parentElement.style.display = "block";

Business required and not !!!

Sometimes we need some attributes to be Business Required and not as per the actions taken by the user
So its very simple to do so.

// Set field to not required
crmForm.SetFieldReqLevel("attributeName", 0);

// Set field to business required
crmForm.SetFieldReqLevel("attributeName", 1);

How to set focus to a Tab ?

Sometimes we need the focus should go to a particular attribute.
So we should focus to the particular tab at first(if multitab is there), then focus the attribute.
This simple line will help out.

// Set focus to the tab.
crmForm.all.tabName.click();
// Set focus to the field.
crmForm.all.attribute_id.SetFocus();

Function to show/hide CRM controls on a CRM form

// Function to show/hide CRM controls on a CRM form
// such as text boxes, lookups, pick-lists etc.
//this function takes two parameters, the crm attribute name and the visibility('none','block')
function setCrmControlVisible(elementName, visibility)
{
setElementVisible(elementName + '_c', visibility); //this for the caption means the label
setElementVisible(elementName + '_d', visibility); //this is for the actual control
}

// Fuction to show/hide specific elements on a CRM form
function setElementVisible(elementId, visibility)
{
var elem = document.getElementById(elementId); //getting the element
if (elem != null)
{
elem.style.display = visibility;
}
}

Adding JavaScript file as Source in MS CRM

All of we know how it is tedious to write js code in MS CRM.
We can include the js files in each entities as it used to be done in html,jsp,aspx etc files.

//create a script element
var script = document.createElement("script");
//set the language to janscript [optional]
script.language = "javascript";
//set the source of the js file with relative or absolute path
script.src = "http://PositiveEdge/js/Sample.js";
//finally add the script element to the head
document.getElementsByTagName("head")[0].appendChild(script);