Microsoft Dynamics CRM 2011: JavaScript to calculate a percentage discount on the Quote Product

By - September 26, 2013

In Microsoft Dynamics CRM 2011, have you ever needed to apply a discount to a single product on a Quote while leaving the other products to be priced at the system level?  I recently had a client with this need, and was left to wonder why this isn’t part of the out of the box functionality.  To accommodate the need, we can use a simple JavaScript function called OnSave of the form.  For this article, I will focus on the Quote Product, but you can use the same JavaScript and form customizations on the Order Product without issue.

First you will need to add a custom field to the Quote Product form.  I called this new_percentagediscount.  Notice that I specified a Minimum and Maximum value for the field of 0.00 and 100.00 respectively.

FieldSetup

Next you will need to create a web resource to hold the JavaScript that will perform the calculation when a user specifies a Percentage Discount.  I used the following code to take the user defined discount percentage to calculate the discount for the product.  I then placed that value into the Manual Discount field to allow the system calculations to continue to function normally.  Just call the calcDiscount function OnSave of the form and you should be set.  Check out this article for more info on how to get started with JavaScript in CRM 2011.


function calcDiscount(){
//call function on save
var price;
var qty;
var perc;

    try {
        //get value of various buckets
        var priceField = Xrm.Page.getAttribute("priceperunit");
        if ((priceField.getValue() != null) && (priceField.getValue() != "undefined")) {
            price = priceField.getValue();
        }
        else {
            price = 0;
        }

        var qtyField = Xrm.Page.getAttribute("quantity");
        if ((qtyField.getValue() != null) && (qtyField.getValue() != "undefined")) {
            qty = qtyField.getValue();
        }
        else {
            qty = 0;
        }       
     
        var percField = Xrm.Page.getAttribute("new_percentagediscount");
        if ((percField.getValue() != null) && (percField.getValue() != "undefined")) {
            perc = percField.getValue();
        }
        else {
            perc = 0;
        }
 
        var disc = (price * qty)*(perc / 100);
        Xrm.Page.getAttribute("manualdiscountamount").setValue(disc);
        Xrm.Page.data.entity.attributes.get("manualdiscountamount").setSubmitMode("always");          
//because field is set to read only, we need to set this to insure save.
    }
    catch (err) {
        alert('Error with javascript on updating weighted revenue. ' + err);
    }
    return 0;
}

Note: I did disable the Percentage Discount field until after the first save so that the function will have values for “Price per Unit” and “Quantity”.

Here is an example of what a Quote Product looks like with a discount applied.

Discount

 

JavaScript is very useful when Out-of-the-Box functions are not enough to meet your business requirements.  For the CRM enthusiasts who want to learn how JavaScript can enhance Microsoft Dynamics CRM forms, here is a Quick Reference Guide for Form Scripting.

If you would rather not tackle these function changes on your own or have questions about JavaScripting for your Microsoft Dynamics CRM system, RSM offers a full range of services from implementation and optimization to development and support. Request our Rapid AssessmentSM process to identify how you can improve the use of your Microsoft Dynamics CRM system.

By: Jon Angell – Minnesota Microsoft Dynamics CRM Partner

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Receive Posts by Email

Subscribe to the Microsoft Dynamics categories and receive notifications of new posts by email.
  • This field is for validation purposes and should be left unchanged.