Solved

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

Posted on 2009-03-31
17
3,334 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
  • 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
 
LVL 11

Accepted Solution

by:
rakeshAgarwal earned 500 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This collection of functions covers all the normal rounding methods of just about any numeric value.
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now