web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

good way for automatic tax creation?

(0) ShareShare
ReportReport
Posted on by 75

good afternoon dear community,

i was wondering if there is an elegant way to set up automatic population for tax fields in products with a given taxrate of 19%

I am on CRM 365 8.2

kind regards, 

Max

I have the same question (0)
  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    yes it's possible, you can use Business Rules or JavaScript

  • Esteban Coto Alfaro Profile Picture
    on at

    Hello, you can create a business rule, here is the documentation that could help: docs.microsoft.com/.../create-business-rules-recommendations-apply-logic-form

    Or creating a JavaScript function that can be triggered

    Thanks!

    Community Support Team - Esteban

    If this Post helps, then please consider Accept as solution to help the other members find it more quickly.

  • MusterMax Profile Picture
    75 on at

    Hi, for the time being I used a script on the main sales order form which will fire on save. Via Rest API and HTTP Requests I am pulling the individual products of said sales order and PATCH them with the correct tax value. Is there a better approach for that? Optimally I'd like to just add an product to my sales order and it'll already have the desired tax.

    I tried business rules but those wont fire unless I open the forms for each individual product, which is a bit sub-optimal (unless I did something wrong here)

    Thank you both for your suggestions so far, if you could stick around to give me a couple more pointers to solve this predicament, I'd be most delighted

    Kind regards

    Max

  • Guido Preite Profile Picture
    54,086 Moderator on at

    you can update the script that you already have to run under the "onchange" event of the product, so it runs when the product is selected instead of the save event of the form.

    Without knowing how the system works it's difficult to provide additional suggestions

  • MusterMax Profile Picture
    75 on at

    var ID = parent.Xrm.Page.data.entity.getId().substring(1, 37);
    var entity = parent.Xrm.Page.data.entity.getEntityName();
    
    function tax(){
    var requestUrl = parent.Xrm.Page.context.getClientUrl()
                  "/api/data/v8.2/"   entity   "details?$filter=_"   entity   "id_value eq ("
                  ID   ")";
        var request = new XMLHttpRequest();
        request.open("GET", encodeURI(requestUrl), true);
    
        request.setRequestHeader("Accept", "application/json");
        request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        request.setRequestHeader("OData-MaxVersion", "4.0");
        request.setRequestHeader("OData-Version", "4.0");
    
        request.onreadystatechange = function () {
            if (this.readyState === 4 /* Complete */) {
                request.onreadystatechange = null;
    
                if (this.status === 200) /* OK */ {
                    updateTax(JSON.parse(this.responseText).value, ID);
                } else { /* failed */
                    console.log(JSON.parse(this.response).error.message);
                    informationNotRetrieved();
                }
            }
        };
    
        request.send();
    }
        
        
        function updateTax(records, ID) {
                    for (i = 0; i < records.length; i  ) {
                        var existingProduct = records[i];
                        setTax(existingProduct.salesorderdetailid, existingProduct.priceperunit, existingProduct.manualdiscountamount);
                        
            }
        }
        
        function setTax(salesorderdetailid, priceperunit, manualdiscountamount) {
                var entity = {};
        entity.tax = (priceperunit-manualdiscountamount)*0.19;
    
        var req = new XMLHttpRequest();
        req.open("PATCH", Xrm.Page.context.getClientUrl()   "/api/data/v8.2/salesorderdetails("   salesorderdetailid   ")", true);
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.onreadystatechange = function () {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 204) {
                    //Success - No Return Data - Do Something
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send(JSON.stringify(entity));
        }

    this is how my script looks at the moment. I would appreciate suggestions for improvement a lot!
    About the on change event: which field(s) would I need to use?

    kind regards

    Max

  • Guido Preite Profile Picture
    54,086 Moderator on at

    the code you posted is probably inside a webresource and not as a javascript attached to the form (meaning you probably trigger this tax function somewhere and not by the save function of the form.

    The script is practically retrieving and updating all the order products of an order (entity salesorderdetail) with this formula:

       entity.tax = (priceperunit-manualdiscountamount)*0.19;

    now if you just put a javascript (or a business rule) inside the order product form to calculate the tax with the same formula when the priceperunit or the manualdiscountamount change (and set it to the tax field) you will get the same result.

    this kind of things it's easier to do than explain (because I don't know your knowledge regarding adding a function or a business rule) but the basic idea is the one I wrote above.

    hope it helps

  • MusterMax Profile Picture
    75 on at

    yeah its a web resource which is attached to the sales order form, triggering on-save.

    If I were to implement js/business rule with the formular above in the order product form, would it still fire if I created a fresh sales order and  added (write-in) products? As in: immediately display the total tax on the sales order calculation?

    My knowledge in coding is limited, as you might have been able to tell from the snippet above but I do know how add/create business rules. However so far when relying on business rules I was still required to open each product's form to have the tax value set!

  • Guido Preite Profile Picture
    54,086 Moderator on at

    I understand your point, if you add an order product from a subgrid the JavaScript/Business Rule may be not be triggered, so the webresource approach is better.

    An alternative is to create a plugin to update that tax value, it will always be triggered when the order product is added or that two specific fields are modified.

    But the webresource approach isn't bad, it requires the user to trigger it but if you are satisfied with it no problems.

  • MusterMax Profile Picture
    75 on at

    I have never dabbled into C# or plugins as that matter seemed very overwhelming and potentially CRM-breaking. Would you happen have a plugin that calculates taxes?

  • Guido Preite Profile Picture
    54,086 Moderator on at

    usually a plugin is not so generic, so it should be created to trigger for your requirement and with your values (in your case 0.19)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 70

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans