Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CRM 4.0 - script on quote product form to calculate tax amount

Posted on 2009-03-31
17
Medium Priority
?
3,373 Views
Last Modified: 2013-11-24
Hi,
We have a script set against the onsave event for the Quote Product form in CRM 4.0, as below:

This script calculates the tax amount based on the rate selected by the user (pick list).

This works fine if the value first value in the picklist is selected i.e. 10%.
on saving the record all amounts are set correctly.

However if the user changes the tax rate pick list to any other another value e.g. 0% and then saves the record, the value in the picklist is set back to the first value.

any ideas as to why this would be happening?

thanks
//check value of tax rate pick list - if 1 = 10%
if (crmForm.all.new_taxrate.DataValue = 1)
{
 var dtaxrate = 10;
}
//if 2 = 0%
else if (crmForm.all.new_taxrate.DataValue = 2)
{
 var dtaxrate = 0;
}
// if 3 = 12.5%
else if (crmForm.all.new_taxrate.DataValue = 3)
{
 var dtaxrate = 12.5;
}
 
 
 
var dTax = (crmForm.all.baseamount.DataValue - crmForm.all.manualdiscountamount.DataValue) * dtaxrate / 100;
var dNet = (crmForm.all.baseamount.DataValue - crmForm.all.manualdiscountamount.DataValue);
crmForm.all.tax.DataValue = dTax;
crmForm.all.new_netamount.DataValue = dNet;
crmForm.all.extendedamount.DataValue = crmForm.all.baseamount.DataValue - crmForm.all.manualdiscountamount.DataValue + crmForm.all.tax.DataValue;

Open in new window

0
Comment
Question by:hcshosting
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
17 Comments
 
LVL 11

Expert Comment

by:rakeshAgarwal
ID: 24036843
if (crmForm.all.new_taxrate.SelectedText = '10%')
{
 var dtaxrate = 10;
}
//if 2 = 0%
else if (crmForm.all.new_taxrate.SelectedText = '0%')
{
 var dtaxrate = 0;
}
// if 3 = 12.5%
else if (crmForm.all.new_taxrate.SelectedText = '12.5%')
{
 var dtaxrate = 12.5;
}

Rakesh Agarwal
www.dynamicsexchange.com 

0
 
LVL 10

Expert Comment

by:crm_info
ID: 24038134
If the above doesn't work, then I suspect you need to do one of two things:
(1) Is the tax field read only?  If so, then CRM won't save changes to the field by default.  Add the following statement the end of your code (you may need to do this for all 3 fields): crmForm.all.tax.ForceSubmit = true;
(2) Are you calling your function in the OnChange event for the picklist?  This would be the correct place to call it to properly calculate tax.  
Some other notes to consider:
(1) You tax calculation is going to change when the cost changes (i.e. user changes quantity or product) OR when the tax picklist changes.  The best practice is to turn your javascript into a global function and place it into the OnLoad event and then call it from each of the OnChange events for the relevant attributes.  That way, if you need to update your code, it is all in one place.
(2) Keep in mind that some things can change your total outside of the form - and your JavaScript cannot take this into account (JavaScript only runs when the form is open).  For example, if a product price changes or a workflow updates a product, then your calculations will not run.  If this is a possibility for you, then you need to use a plugin and not javascript to handle your calculations.  See item #4, below, for more info.
(3) I believe CRM automatically calculates the extendedamount without the user entering any data - after the form is saved.  You may find that you don't have to add this calcuation to your JavaScript.
(4) If you're performing calculations in the Product list for the Quote entity, then you're probably going to want to roll those calculations up to the parent entity (i.e. so the user can see total tax, etc).  To do this, you'll need a .NET plugin to handle the job.  We've written a calculator plugin (handles calculating fields within a single form) and a rollup plugin (subtotals items back to a parent entity) that we can setup for you if you are interested.
Customer Connect
www.customer-connect.com
www.askcrm.com
 
0
 

Author Comment

by:hcshosting
ID: 24044665
Hi,

thanks for the suggestions.

the script i had was running on the form onsave event.

if i update the script to include the example above and leave it on the onsave event, the same thing happens, the pick list value defaults back to the 10% value straignt away. it wont even stay set to 0% just after changing the field.

if i then put the updated script on the onchange event for the tax drop down field, when you change the select a different value in the list, it changes the label to be 10%.. i.e. you end up with 3 values all of 10%.

if add a new field to just store a int value and use this to enter the rate the script works fine. it just seems to be something with the pick list.

the picklist field isnt set to be readonly so i dont think the forcesubmit will make a difference?

0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 11

Accepted Solution

by:
rakeshAgarwal earned 2000 total points
ID: 24057261
sorry

This should be on change  or on save

Instead of  =  it should be ==


if (crmForm.all.new_taxrate.SelectedText == '10%')
{
 var dtaxrate = 10;
}
//if 2 = 0%
else if (crmForm.all.new_taxrate.SelectedText == '0%')
{
 var dtaxrate = 0;
}
// if 3 = 12.5%
else if (crmForm.all.new_taxrate.SelectedText == '12.5%')
{
 var dtaxrate = 12.5;
}
0
 

Author Comment

by:hcshosting
ID: 24074058
Hi,

Changing the script to use == instead of = has now worked. The picklist is changing correctly and the tax is being calculated correctly. Thank you!

Ive put this script on the Form Onsave event as well as the onchange events on the relevant fields in the form.

This works fine if you change a field value and save. but on the initial save of the form, the script isnt being run? i.,e you select the product, the units and then quanity and then click save - only the standard fields are calculated. You then need to click Save again before the script will run and the tax will be calculated.

Any ideas why this would happen?
0
 
LVL 11

Expert Comment

by:rakeshAgarwal
ID: 24074336
No ideas !!!

 I have never seen  this.  Can you double check on this ?

regards
Rakesh Agarwal
0
 

Author Closing Comment

by:hcshosting
ID: 31565164
closing as the original issue has been resolved. the script still wont run on the initial save though
0
 
LVL 11

Expert Comment

by:rakeshAgarwal
ID: 24184039
put the whole event onchange of quantity  or fields you have created

base amount can be calculated  by

priceperunit * quantity

regards
rakesh Agarwal



0
 

Author Comment

by:hcshosting
ID: 24189569
Hi,

thanks for the suggestion, ive added it to the fields onchange event, but the same thing happens, the tax is only calculated after clicking the Save button the second time. on the first click the value is still set to null.

0
 
LVL 11

Expert Comment

by:rakeshAgarwal
ID: 24191099
Can you update the code here  please ?


regards
Rakesh Agarwal
www.dynamicsexchange.com 
we deliver  CRM 4.0 reports in 24 hrs :)
We deliver Javascript in crm 4.0 in 10 hrs
0
 

Author Comment

by:hcshosting
ID: 24191174
Here it is!
if (crmForm.all.new_taxrate.SelectedText == '10%')
{
 var dtaxrate = 10;
}
//if  0%
else if (crmForm.all.new_taxrate.SelectedText == '0%')
{
 var dtaxrate = 0;
}
// if  12.5%
else if (crmForm.all.new_taxrate.SelectedText == '12.5%')
{
 var dtaxrate = 12.5;
}
 
 
var dNet = (crmForm.all.baseamount.DataValue - crmForm.all.manualdiscountamount.DataValue);
 
var dTax = (crmForm.all.baseamount.DataValue - crmForm.all.manualdiscountamount.DataValue) * dtaxrate / 100;
 
crmForm.all.tax.DataValue = dTax;
 
crmForm.all.new_netamount.DataValue = dNet;

Open in new window

0
 
LVL 11

Expert Comment

by:rakeshAgarwal
ID: 24192019
instead of

crmForm.all.baseamount.DataValue


(crmForm.all.priceperunit.DataValue * crmForm.all.quantity.DataValue)
0
 
LVL 11

Expert Comment

by:rakeshAgarwal
ID: 24193228
0
 

Author Comment

by:hcshosting
ID: 24221876
Beautiful! thanks working now

thank you very much
0
 
LVL 11

Expert Comment

by:rakeshAgarwal
ID: 24222202
Can you post the solution now? What did you do so that it will be useful for others

Thanks
Rakesh
Help community to grow.
0
 

Author Comment

by:hcshosting
ID: 24246627
The following code worked onload and onsave events. code was also included on change event of the relevant fields.
//Calc tax values
if (crmForm.all.new_taxrate.SelectedText == '10%')
{
 var dtaxrate = 10;
}
//if  0%
else if (crmForm.all.new_taxrate.SelectedText == '0%')
{
 var dtaxrate = 0;
}
// if  12.5%
else if (crmForm.all.new_taxrate.SelectedText == '12.5%')
{
 var dtaxrate = 12.5;
}
 
 
var dNet = ((crmForm.all.priceperunit.DataValue * crmForm.all.quantity.DataValue) - crmForm.all.manualdiscountamount.DataValue);
 
var dTax = ((crmForm.all.priceperunit.DataValue * crmForm.all.quantity.DataValue) - crmForm.all.manualdiscountamount.DataValue) * dtaxrate / 100;
 
crmForm.all.tax.DataValue = dTax;
 
crmForm.all.new_netamount.DataValue = dNet;
 
crmForm.all.extendedamount.DataValue = dNet + dTax;

Open in new window

0
 
LVL 11

Expert Comment

by:rakeshAgarwal
ID: 24247656
Thanks
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our personal lives, we have well-designed consumer apps to delight us and make even the most complex transactions simple. Many enterprise applications, however, are a bit behind the times. For an enterprise app to be successful in today's tech wo…
"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question