Mar 25, 2012

Issue of using Onload JavaScript for “create form” of Quote

This is just a simple warning. We all use onload JavaScript for any entity and we simply know how to handle them separately for Create form and Update form, when necessary.  For example, below is a popular way of doing something (for onLoad) just for “create form”.

var CRM_FORM_TYPE_CREATE = 1;     

if (crmForm.FormType == CRM_FORM_TYPE_CREATE)
  //some script create form onload

Now I am going to use this for Quote entity. Can you guess the issue related to this attempt?

Quote can be created from other entities; for example from Opportunity. This script will not work when we create a quote from opportunity. That’s because, when quote is being created from opportunity, quote is first created in the background before pop-up a form for user. What we really see is an Update form not a create form. In such cases, you may move in to plug-ins than Java Scripts. Actually, you don’t get a create event. Quite tricky! Isn’t it?

Mar 21, 2012

onChange vs onClick for checkbox?

Previously I wrote a post to tell how to use onClick event in CRM 4.0 for checkboxes. Recently I felt it is not completed. I think, it’s a matter of usability engineering expectations. You can simply use either onChange or onClick for checkboxes.

Will check onChange;
You can simply double click the field in customization form view and see the event. Then you know how to add the script in the relevant box and enable it. It’s as simple as that.

For example put below alert statement to check the firing of the event.

alert("onChange event fired");

Now if you browse the form after publishing... try to change the value of checkbox. What you will notice is you have to do two clicks to fire the event. You got to click the checkbox (nothing happens) and click somewhere else (to take the focus away) to confirm your change... then you can see the alert message pops up.

Will check onClick;
Here you are not touching anything in the relevant field... but write a method in the loading of the form as below. Now if you browse the form and do the previous test here.. you will notice, once you click the checkbox alert triggers. This means you only need one click.


deliveryCheckbox = function()
    crmForm.all.msn_isDelivery.onclick = function()
        alert("onClick event fired");

As a conclusion, I would suggest you to use onClick for all the cases, if there is no special reason to use onChange. My point is “not just saving one mouse click” our users are not always technical people. I have seen users just click the check box and try to save the form. If we have used a script to work for onChange event, it wouldn’t have happened in such cases.

Related Posts;
Issues of using checkbox in CRM 2011

Mar 4, 2012

Calculate business days in c# (with variable business days per week)

One of my past posts (i.e. here) I discussed calculating hire days in JavaScrips for equipments (including start and end days) with variable number of days per week. It could be 5 as a default, but one can work on Saturdays too and also there can be non-stop operations which we should count 7 days per week. Here is the appropriate c# code.

public static int getHireDays(DateTime startDate, DateTime endDate, int NoOfDays)
        int _saturdays = 0;
        int _sundays = 0;
        int _Bdays = 0;

        endDate += TimeSpan.FromDays(1); //add one beacuse we need days INCLUDING end date
        long ticks = (endDate.Ticks - startDate.Ticks) / 10000000; //seconds
        int days = (int)(ticks / 86400);

        endDate -= TimeSpan.FromDays(1);
        for (DateTime i = startDate; i < endDate; i += TimeSpan.FromDays(1))
            if (i.DayOfWeek == DayOfWeek.Saturday) { _saturdays = _saturdays + 1; }
            if (i.DayOfWeek == DayOfWeek.Sunday) { _sundays = _sundays + 1; }

        switch (NoOfDays)
            case 5:
                _Bdays = days - (_sundays + _saturdays);
            case 6:
                _Bdays = days - _saturdays;
            case 7:
                _Bdays = days;
        return _Bdays;

Since I am doing a loop here, this can be bit inappropriate for long periods, but I haven’t tested the performances for long periods.