Thursday, September 11, 2008

CRM 4.0 Form How To Tips

It occurred to me that most of the samples in my blog are more of a “complete solution” or too complicated. Sometimes you’re only looking for a line or two to integrate with your own code.
In order to address that issue I have decided to write a series of How-To posts which are simple and usually address a specific requirement.

The how-to posts are categorized by common client side development tasks like CRM Form, CRM Fields, CRM IFRAME, CRM Lookup and so on. Each time something new comes up I’ll simply update the relevant “Repository” How-To post. If you have a specific requirement that you don’t see in my post and you feel that should be then comment on that and if it is missing I’ll share that solution when I can.

This How-To post addresses CRM Form development tasks:

Addressing Form Type (crmForm.FormType) in script.
This should be the BASIC TEMPLATE for all your scripts. Since crmForm has more then 1 state (type) your code should always address that.
If for example the create and update Types share the same code than simply put your code in one method and call it from the other.

/* Form Types */
var FormTypes =
Undefined : 0,
Create : 1,
Update : 2,
ReadOnly : 3,
Disabled : 4,
QuickCreate : 5,
BulkEdit : 6

/* Script Entiry Point */
function OnCrmPageLoad()
switch( crmForm.FormType )
case FormTypes.Create:OnNewFormLoad();break;
case FormTypes.Update:OnUpdateFormLoad();break;
case FormTypes.ReadOnly:OnReadOnlyFormLoad();break;
case FormTypes.Disabled:OnDisabledFormLoad();break;
case FormTypes.QuickCreate:OnQuickCreateFormLoad();break;
case FormTypes.BulkEdit:OnBulkEditFormLoad();break;
case FormTypes.Undefined:alert("Error");break;

/* Implement each Form Type you wish to address */
function OnNewFormLoad(){}
function OnUpdateFormLoad(){}
function OnReadOnlyFormLoad(){}
function OnDisabledFormLoad(){}
function OnQuickCreateFormLoad(){}
function OnBulkEditFormLoad(){}


Change Field Required level

Parameter1 - Field scheme name
Parameter2 - is required
crmForm.SetFieldReqLevel( "<FieldId>" , true );

Check if the form has changed ( by user or code ) Since it was loaded

alert( crmForm.IsDirty() );

Binding to crmForm save event
This coding method is easier to manage and more productive then putting your code In the OnSave event box.

crmForm.attachEvent( "onsave" , OnCrmPageSave );

This relates to the former bullet.
The code checks the form validity. If its not valid then The returnValue is set to false and the form save does not fire.

function OnCrmPageSave()
var valid4Submit = true;
if( /* Form Is NOT Valid */ )
valid4Submit = false;

return (event.returnValue = valid4Submit);

Retrieve Entity Object Type Code

alert( crmForm.ObjectTypeCode );

Retrieve Entity Schema Name

alert( crmForm.ObjectTypeName );

Remove (detach) MS window close alert



Anonymous said...

I really like the basic template you have!


Anonymous said...

I've just found the same post on different blog, most probably posted after yours.
A plagiary? :/

Anonymous said...

This post really helped me.. Thanks a lot..


Blogger said...

In case you are looking into making money from your visitors using popunder advertisments - you can embed one of the most reputable networks - PropellerAds.