Welcome to MSDN Blogs Sign in | Join | Help

East Region Microsoft CRM Blog

This Blog is focused on Microsoft CRM. It specalizes in news for CRM partners and customers, customization tools for demos and local events in the Microsoft East Region.

News

  • Welcome to the Mid-Atlantic CRM Blog, formerly owned and operated by Ben Vollmer, a CRM legend ;). My name is Chris Kahl and Ben has asked me to take over the care and feeding of this most valuable resource. Like Ben, my job is to help partners and customers with Microsoft CRM in the Eastern US. This blog is a great and searchable information source. Feel free to subscribe to the RSS feed to stay current with Dynamics CRM. Any tools discussed here are for DEMO only. You should NOT use these in a production environment unless you test them. Microsoft Support DOES NOT support these tools. Everything here is provided as-is with no warranty.
Doing Math in Microsoft CRM aka Calculated Fields

This is a question I get about once a week and got tired of typing up the same email over and over again. J Doing Math in Microsoft CRM using Jscript is mind numbingly easy. If you can do an Excel (Or VisiCalc) math equation, then you can do one in Microsoft CRM.

How you ask? Insert a Jscript on the field that you want the math done in. (So if you want Annual Income plus Alimony, this is how you could do it… And this is in case you can forget about the Anniversary date or month. J)

So you will have three fields:

1.   Annual Income – new_AnnualIncome

2.   Annual Alimony – new_AnnualAlimony

3.   Total Annual Income – new_totalannualincome

On the Total Annual Income, put a OnChange script that looks a little like this:

crmForm.all.new_totalannualincome.DataValue = crmForm.all.new_annualincome.DataValue + crmForm.all.new_annualalimony.DataValue;

Under normal circumstances CRM will not include disabled fields when saving forms even when the data has been changed.  We can, however, force CRM to include this field by using the ForceSubmit property as seen below:

 crmForm.all.new_totalannualincome.ForceSubmit = true;

I would disable the field. Then on the other two fields, paste this Jscript into their OnChange event:

new_totalannualincome_onchange0()

So the math you can do includes:

Addition

+

Subtraction

-

Division

/

Multiplication

*

Posted: Thursday, August 10, 2006 7:00 AM by Ben Vollmer

Comments

Zulu said:

what would the Jscript look like if you want to Calculated more than one Field ?
# August 11, 2006 5:11 AM

Ben Vollmer said:

Give me an example...
# August 11, 2006 7:46 AM

TrackBack said:

# August 12, 2006 7:27 AM

Ivan said:

Cool, thanks man, that was an eye opener for someone that had no ideia of how to do it.

# October 18, 2006 10:33 AM

East Region Microsoft CRM said:

My apologies in not getting this up sooner. I flew home on the red-eye on Wednesday night, drove from

# March 16, 2007 12:12 PM

East Region Microsoft CRM said:

My apologies in not getting this up sooner. I flew home on the red-eye on Wednesday night, drove from

# March 16, 2007 1:22 PM

David said:

Hey i am curious about a calculated field like the estimated opportunity revenue. where the field is calculated by x - sum(child:x). is there an easy way to do that on the form?

# April 12, 2007 10:36 AM

Ben Vollmer said:

Easy answer? No. :-) With some work, yes... But easily... No...

# April 12, 2007 4:59 PM

Maina said:

How do i set up a similar calculated totals in a structure similar to contracts and their contract lines? When you add a figure in contract lines the total figure in the contract gets updated

# June 6, 2007 10:01 AM

Ben Vollmer said:

Maina,

That would require a little custom work... You should do a post call out with an application calling CRM's web service to make the update. :-)

Thanks!

Ben

# June 10, 2007 8:55 PM

Panez said:

Hi to all! I'm going mad,

my math customization problem is this:

When you go to

Sales -> Offers and than open an existing Offer the toolbar have a button Recalculate.

This calcolous find actual(at the moment of the launch) prices of all products and sum for a new total (if the prices are changed from the previous launch), this is very interesting because manage the list of products associated to the offer, I need this too.

Well, I have to do a simple thing (but I'm walking on the darkness!) I've added a picklist (simple!) with some voice of type "add 10% to the total amount of offer" or "apply Christmas discount to tech products" ecc

I've added a new button "MyRecalc" to the toolbar (simple!)

BUT I'don't have idea to how apply my personal calcolous related to the picklist choice.

Wich is the way to do that? There's examples, exist somewhere the source code of the original "Recalculate" method?

Heeeeeelp :)

Panez

PS if the solution exist I hope it's doesn't use Webservices, I build a custom web service that work if called in a web application inside an Iframe but when I call web service methods inside javascript I've got empty response (I don't know if the web service really respond to my call).

PPS Sorry for my English

# August 3, 2007 6:18 AM

Mike Menser said:

Is it possible to do dependant math? i.e.: form1 + form2 only if form3 = x

The usage I am looking for is a total of grant funds. Funds from form1, plus funds from form2 but only if the funds in form2 are eligible as specified in a yes or no menu on form3.

Thanks in advance for the help!

# August 20, 2007 11:10 AM

Mike Menser said:

I apologize. I think that my usage gives the wrong impression. This is all on the same form, but in 3 different fields.

# August 20, 2007 11:11 AM

mmenser said:

I keep getting an error... I do not get the output from the equation in my ending field, and when I try to enter text into the field I get "This control only accepts strings as null or input"

# August 20, 2007 12:16 PM

Ben Vollmer said:

Could you share your formula?

# August 20, 2007 3:27 PM

mmenser said:

Thanks for all the wonderful tips Ben.. you are my CRM hero! I did get the last thing figured out and the code works now. I still never figured out doing dependent math, as stated in my first post. I do have a NEW question... I was wondering if you can do math with date fields. Here is the usage:

Say there is a field that says "Date Letter Was Mailed", and then there is a second field for "Followup Date". Well, my follow up date should be 30 days after the letter was sent. So if I fill in the "Date Letter Was Mailed" field, I would like the Followup Date to populate automatically with the date that would be 30 days from the date the letter was sent. Any ideas??

# September 19, 2007 3:55 PM

Ben Vollmer said:

M-

Lookup in the JavaScript Bible doing math on Date Fields. It takes a little more work, but is pretty easy. I don't have it in front of me, but it works VERY well. :-)

Thanks!

Ben

# September 24, 2007 3:56 PM

Dave Secker said:

Ben-

Thanks so much for the info - it's much appreciated.  Would you happen to know why I might be getting 'object exptected' errors whenever I update either factor variable?  I've tried your method and the fireonchange() function and I end up with the same error.  If I manually change the sum field it then calls the onchange function and works properly...

Thanks!!

# October 3, 2007 2:01 AM

Stacy said:

I used an addition function on quotes, orders, and invoices that worked in 3.0 but after the upgrade, something is working quite right.

I cannot convert an order to an invoice whenever I have data in my fields that are calculated.  I can convert a quote to an order but my total doesn't show on the order after the conversion.

I keep wondering what changed?

# March 13, 2008 9:34 AM

Maria said:

I keep getting a "object is null" error... I followed your steps exacly. Could someone help me figure this out?  All I am trying to do is add two money fields...

# April 4, 2008 10:30 AM

Travis Engler said:

I have a custom entity that I created with a "Amount Received" field.  This Entity have a N:1 relationship with an account.  I am trying to get help on the web service code it would require to have a total field on the account that will look at & sum all the related "Amount Received" fields for the child entity.  Could you give me some direction?

# May 1, 2008 4:31 PM

Kona Sirohi said:

In CRM Live (which doesn't support plugin and custom workflow actions), how do we calculate a custom sub-total of Quote Products based on the category / Subject of the product. For example, if a Quote has multiple Quote Product, each Quote Product can be categorized by the subject of the Product (like Materrial + Labour), how do we calculate the sub-total of all the materials and sub-total of all the Labour?

I added 2 new attributes to Quote entity for holding the sub-total and I can to an extent achieve the goal by using 2 different workflows on the Quote Product entity (one each for record created and record deleted triggers), how ever I cam not able to do the calculations if someone updates an existing Quote Product record's price or quantity. this would have been easy with plugins, but CRM Live doesn't support plug-ins.

Any input on this is truely appreciated. Thanks

# May 21, 2008 11:51 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker