Error in field calculation

This question is answered

I am trying to get a field to do a simple calculation and put the result in another field.  Here is what I have

Field 1 = Estimated Project Amount (estimated amount of a project)

Field 2= Fee % (Fee percentage we'll apply to that)

Field 3 = Construction Fee (the result - for instance 15% on a $1,000,000 project would result in 150,000)

In the Fee% field i have the following event in the Fee% field - On Change

function calculate()

{

 var val1 = Xrm.Page.entity.attributes.get('new_estimatedprojectamt").getValue();

 var val2 = Xrm.Page.entity.attributes.get("new_fee").getValue();

 if (val1 == null) return;

 if (val2 == null) return;

 var result = val1 * val2;

 Xrm.Page.entity.attributes.get("new_constructionfees").setValue(result);

 }

When i try to enter a number into the field i get the following error

There was a error with thie field's customized event

Field: new_fee

Event:onchange

Error:'calculate' is undefined

Verified Answer
All Replies
  • You have mis-matched quotation marks in the line setting val1

     

    function calculate()
    {
        var val1 = Xrm.Page.entity.attributes.get("new_estimatedprojectamt").getValue();
        var val2 = Xrm.Page.entity.attributes.get("new_fee").getValue();
        if (val1 == null) return;
        if (val2 == null) return;
        var result = val1 * val2;
        Xrm.Page.entity.attributes.get("new_constructionfees").setValue(result);
    }
    

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

  • Thanks for the quick response.

    I now get the following error:

    There was an error with this fields customized event.

    Field: new_fee

    Event: onchange

    Error: Unable to get value of the property 'attributes': object is null or undefined.

  • Use use Xrm.Page.data.entity...

     

    function calculate() {
        var val1 = Xrm.Page.data.entity.attributes.get("new_estimatedprojectamt").getValue();
        var val2 = Xrm.Page.data.entity.attributes.get("new_fee").getValue();
        if (val1 == null) return;
        if (val2 == null) return;
        var result = val1 * val2;
        Xrm.Page.data.entity.attributes.get("new_constructionfees").setValue(result);
    }
    

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

  • Jason, again thanks for your response.  I made the change but still get the exact same error.

    I apologize for my ingnorance on this issue but this is my first attempt and adding this type of functionality to CRM.

  • After updating the web resource - did you publish it?

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

  • I did publish it - twice for good measure.  Do I need any type of code\formula in the "Construction Fee" field which will simply show the result?

  • Nothing special besides the field actually being present on the form. Just to verify - is the schema name of the field "new_constructionfees" or "new_constructionfee" (not plural)? The code implies it is plural but you just referred to it as singular.

    I tested the code with matching fields - maybe just double check the field names.

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

  • "new_constructionfees" is correct.  I double checked the actual schema name on all fields to ensure they match the code.

  • Does it matter they the "type" is on the custom field that has been created?  The new_fee for example is a Decimal Number.  The Construction Fees is a whole number.

    Also, i did have a typo in the code on construction_fees

  • Ok.  The new_fee field now accepts the entry but there is nothing returning in the the new_constructionfees field.

  • Yes - the data types matter. If you are trying to take a decimal result and put it into a whole number (integer) field you'll need something like this:

    Xrm.Page.data.entity.attributes.get("new_constructionfees").setValue(parseInt(result.toString()));

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

  • Well, I spoke to soon.  The Fee% field will accept the entry but only after it returns the same error.  I did change the code as shown above.  I didn't know this could be so frustrating.

    Is there maybe an easier way to achieve this?

  • Interesting, I opened CRM in Chrome and do not get the error when entering the % number but nothing returns in the Construction Fees field.

  • This is the simplest way to handle this.

    Do you have both fields used in the calculation populated when the OnChange event fires? If this fires when the "Fee" field is changed, both it and the other field comprising the total need values based on what we've got so far. If one doesn't have a value, the code exits before making the calculation.

    Maybe post the code you have now, the current error, the datatypes of all 3 fields, which events you are binding the code to, and which fields are populated when you get the error. Just to get a clear picture right now what is happening.

    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

  • Estimated project amount is populated.  Fee% is populated which at that point i'd like the construction fees to be calculated.  So, it should fire when the Fee field changes.  I am not certain that is what's happening

    Current Code:

    function calculate()

    {

      var val1 = Xrm.Page.entity.attributes.get("new_estimatedprojectamt").getValue();

      var val2 = Xrm.Page.entity.attributes.get("new_fee").getValue();

      if (val1 == null) return;

      if (val2 == null) return;

      var result = val1 * val2;

      Xrm.Page.data.entity.attributes.get("new_contructionfees").setValue(parseInt(result.toString()));

      }

    new_estimatedprojectamt - type=Currency

    new_fee - type=decimal

    new_contructionfees - type=whole number

    This is the event currently in tied to the construction fee field.  It was in the new_fee schema and generated the above error.  maybe i had it tied to the wrong schema.  Now, i can enter the fee with no error but nothing is returned in the new_contructionfees field.