Solved

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

Posted on 2009-03-31
17
3,330 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
Automatically creating a Trello card using data from a Microsoft Dynamics CRM record turned out to be an easy project that yielded great results.  Here's how I did this for an internal team at General Code.
Viewers will learn the different options available in the Backstage view in Excel 2013.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…

708 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

15 Experts available now in Live!

Get 1:1 Help Now