locked
Calculated Fields RRS feed

  • Question

  • I am trying to customize fields on a form, so that values in five different fields total the value of the sixth field.  How do I script this and is it done on the onload event of the form, or on-click events of the field(s)?  Please help.  Thanks, in advance!  <Time-Sensitive>
    Monday, August 3, 2009 6:53 PM

Answers

  • I just tried this on two integer fields:

                crmForm.all.dmi_sumc.FireOnChange();

    with this on a third:

                crmForm.all.dmi_sumc.DataValue = crmForm.all.dmi_addenda.DataValue + crmForm.all.dmi_addendb.DataValue;

    and it works perfectly.

    Are your fields of number data type instead of string type?

    Did you click the checkbox to activate the event handlers?
    • Marked as answer by LinCar Friday, September 11, 2009 1:54 PM
    Tuesday, August 4, 2009 6:20 PM

All replies

  • Hi.

    The best way is to write OnChange event handlers for each of source fields.
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Monday, August 3, 2009 7:40 PM
    Moderator
  • Hi

    You could use the following code in the on-load or on-save event for the form:

    crmForm.all.new_gp.DataValue = (crmForm.all.price.DataValue + crmForm.all.currentcost.DataValue);

    The above calculates the sum of 2 fields - price and currentcost and the result is displayed in new_gp
    I used it to calculate Gross Profit from 2 fields, but you can easily adapt it for your fields.

    Rob

    www.crmconsult.info www.chorusit.com
    Tuesday, August 4, 2009 11:43 AM
  • Hi.

    The best way is to write OnChange event handlers for each of source fields.
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com

    I need someone with a better memory than mine to finish this answer.  Put the calculation and assignment on the onChange event of field 6.  Then on 1 through 5 call the onChange event of field 6 -- I can't remember the syntax off the top of my head.  This will not only keep field 6 up-to-date but effectively stop anyone from editing field 6 directly.

    OnChange events are cared for with jscript, fyi.
    Tuesday, August 4, 2009 3:21 PM
  • Hello.  Thank you for your suggestion.  I tried this, but it does not calculate.  I am also trying to load activities, so in all, here is what is in my event handler.  I think I have something extra...can you or someone please look to see what I did wrong?  Thanks, in advance.



    crmForm.all.estimatedvalue_base.datavalue = (crmForm.all.renewal.DataValue + crmForm.all.newclient.DataValue + crmForm.all.services.DataValue + crmForm.all.newproduct.datavalue + crmForm.all.training.datavalue);}

    function GetFrameSource(tabSet)
    {
    var oId = crmForm.ObjectId;
    var oType = crmForm.ObjectTypeCode;
    var security = crmFormSubmit.crmFormSubmitSecurity.value;
    return "areas.aspx?oId=" + oId + "&oType=" + oType + "&security=" + security + "&tabSet=" + tabSet;
    }
    else
    {
    return "about:blank";
    }
    }
    crmForm.all.IFRAME_HistoryFrame.src = GetFrameSource("areaActivityHistory");

    Tuesday, August 4, 2009 4:10 PM
  • Jscript is case sensitive and you have datavalue and DataValue in your statement.
    Tuesday, August 4, 2009 4:13 PM
  • How do I simply call the OnChange event for field 6?  Does anyone recall the syntax?  Thanks!  I think this will give me the calculations AND validation that I need.  Please help.
    Tuesday, August 4, 2009 4:14 PM
  • Hi. Also you have to correct your script in the following way:


    change
    crmForm.all.estimatedvalue_base.datavalue = (crmForm.all.renewal.DataValue + crmForm.all.newclient.DataValue + crmForm.all.services.DataValue + crmForm.all.newproduct.datavalue + crmForm.all.training.datavalue);}

    to

    crmForm.all.estimatedvalue_base.DataValue = (crmForm.all.renewal.DataValue + crmForm.all.newclient.DataValue + crmForm.all.services.DataValue + crmForm.all.newproduct.DataValue + crmForm.all.training.DataValue);
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Tuesday, August 4, 2009 4:14 PM
    Moderator
  • From SDK:

    Remarks

    You can force the OnChange event to occur for a field from the Microsoft Dynamics CRM form OnLoad event or from the OnChange event of some other field. To do this, use the FireOnChange method.

    For example, you may use the following sample script:

    crmForm.all.YourFieldID.FireOnChange();

    I would pseudo-code it as "Field6.FireOnChange()" if that makes it any easier to understand.
    • Edited by mardukes Tuesday, August 4, 2009 4:39 PM bad grammar
    • Proposed as answer by mardukes Tuesday, August 4, 2009 4:41 PM
    Tuesday, August 4, 2009 4:38 PM
  • Ahh!  I did not pick up on that.  I will change now and see what I get.  Thanks!
    Tuesday, August 4, 2009 4:45 PM
  • I just changed the casing, but it still doesn't calculate.  right now, I have it on the onload and onsave event handlers, as well as on each field that is a part of the statement.  Is this too much?  It's not helpful that I get no error messages.  Thanks for your help.
    Tuesday, August 4, 2009 5:05 PM
  • I just tried this on two integer fields:

                crmForm.all.dmi_sumc.FireOnChange();

    with this on a third:

                crmForm.all.dmi_sumc.DataValue = crmForm.all.dmi_addenda.DataValue + crmForm.all.dmi_addendb.DataValue;

    and it works perfectly.

    Are your fields of number data type instead of string type?

    Did you click the checkbox to activate the event handlers?
    • Marked as answer by LinCar Friday, September 11, 2009 1:54 PM
    Tuesday, August 4, 2009 6:20 PM