Question Status

Verified
Brad Baumert asked a question on 8 Mar 2013 6:21 AM

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

Reply
Verified Answer
Jason Lattimer responded on 8 Mar 2013 2:24 PM

In the "new_estimatedprojectamt " OnChange event - did you type in "calculation" or "calculate"? It should be "calculate" to match the function name in the script (case sensitive also).

Jason Lattimer
My Blog -  Follow me on Twitter -  LinkedIn

Reply
Suggested Answer
Jason Lattimer responded on 8 Mar 2013 6:32 AM

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

Reply
Suggested Answer
Jason Lattimer responded on 8 Mar 2013 6:48 AM

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

Reply
Suggested Answer
Jason Lattimer responded on 8 Mar 2013 12:12 PM

Try this - instead of binding it to the OnChange event of the field that holds the result (new_contructionfees), bind to both the fields used in the calculation (new_estimatedprojectamt & new_fee).

function calculate() {
    var val1 = Xrm.Page.getAttribute("new_estimatedprojectamt").getValue();
    var val2 = Xrm.Page.getAttribute("new_fee").getValue();
    if (val1 == null) return;
    if (val2 == null) return;
    var result = val1 * val2;
    Xrm.Page.getAttribute("new_contructionfees").setValue(parseInt(result.toString()));
}

Jason Lattimer
My Blog -  Follow me on Twitter -  LinkedIn

Reply
Suggested Answer
Jason Lattimer responded on 8 Mar 2013 6:32 AM

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

Reply
Brad Baumert responded on 8 Mar 2013 6:39 AM

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.

Reply
Suggested Answer
Jason Lattimer responded on 8 Mar 2013 6:48 AM

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

Reply
Brad Baumert responded on 8 Mar 2013 7:12 AM

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.

Reply
Jason Lattimer responded on 8 Mar 2013 7:23 AM

After updating the web resource - did you publish it?

Jason Lattimer
My Blog -  Follow me on Twitter -  LinkedIn

Reply
Brad Baumert responded on 8 Mar 2013 7:27 AM

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?

Reply
Jason Lattimer responded on 8 Mar 2013 7:33 AM

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

Reply
Brad Baumert responded on 8 Mar 2013 7:42 AM

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

Reply
Brad Baumert responded on 8 Mar 2013 7:49 AM

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

Reply
Brad Baumert responded on 8 Mar 2013 9:14 AM

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

Reply
Jason Lattimer responded on 8 Mar 2013 9:58 AM

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

Reply
Brad Baumert responded on 8 Mar 2013 10:55 AM

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?

Reply
Brad Baumert responded on 8 Mar 2013 11:01 AM

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

Reply
Jason Lattimer responded on 8 Mar 2013 11:21 AM

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

Reply
Brad Baumert responded on 8 Mar 2013 11:40 AM

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.

 

 

Reply