Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Auto-increment Quote Products if Criteria is met

Posted on by 602

I need to auto-increment my Quote Products if Criteria x is met.  I found this thread https://community.dynamics.com/crm/f/117/t/107623 with some JavaScript by Jason Lattimer.  I am new to JavaScript:

1. The thread was dated 2013 - will these properties & methods work for 2016 Online CRM?

2. Do I need to change SalesOrderDetailSet to QuoteProduct?

3. I set an alert after the initial IF statement, but I'm not reaching it.  What needs to be changed? 

This is the JavaScript I used:

function Form_OnLoad() {
    if (Xrm.Page.ui.getFormType() != 1) {
        return;
    }
 alert("Yes!");

    var serverUrl = Xrm.Page.context.getClientUrl();
    var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/SalesOrderDetailSet?$select=LineItemNumber&$top=1&$orderby=LineItemNumber desc&$filter=SalesOrderId/Id eq guid'" + Xrm.Page.context.getQueryStringParameters()["_CreateFromId"] + "'";

    var retrieveReq = new XMLHttpRequest();
    retrieveReq.open("GET", oDataSelect, false);
    retrieveReq.setRequestHeader("Accept", "application/json");
    retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
    retrieveReq.onreadystatechange = function () {
        GetData(this);
    };
    retrieveReq.send();
}

function GetData(retrieveReq) {
    if (retrieveReq.readyState == 4) {
        if (retrieveReq.status == 200) {
            var retrieved = JSON.parse(retrieveReq.responseText).d;
            if (retrieved.results[0] != null) {
                Xrm.Page.getAttribute("lineitemnumber").setValue(parseInt(retrieved.results[0].LineItemNumber) + 1);
            } else {
                Xrm.Page.getAttribute("lineitemnumber").setValue(1);
            }
        }
    }
}

*This post is locked for comments

  • a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Auto-increment Quote Products if Criteria is met

    Feel free to ask additional questions if you need help.

  • Barbara Forsman Profile Picture
    Barbara Forsman 602 on at
    RE: Auto-increment Quote Products if Criteria is met

    Thank you!!!

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Auto-increment Quote Products if Criteria is met

    Barbara,

    For opportunities child products are OpportunityProduct so you should use OpportunityProductSet inside your query.

    Also you should check that lineitemnumber field is on form and you should include check that LineItemNumber  field is not null. What you can do to compose code in easier way - you should download and use Jason's CRMRestBuilder tool - github.com/.../CRMRESTBuilder

  • Barbara Forsman Profile Picture
    Barbara Forsman 602 on at
    RE: Auto-increment Quote Products if Criteria is met

    This did not work:

    function Form_OnLoad() {

    //    if (Xrm.Page.ui.getFormType() != 1) {

    //        return;

    //    }

    alert("Yes!");

       var serverUrl = Xrm.Page.context.getClientUrl();

       var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/OpportunityDetailSet?$select=LineItemNumber&$top=1&$orderby=LineItemNumber desc&$filter=OpportunityId/Id eq guid'" + Xrm.Page.context.getQueryStringParameters()["_CreateFromId"] + "'";

       var retrieveReq = new XMLHttpRequest();

       retrieveReq.open("GET", oDataSelect, false);

       retrieveReq.setRequestHeader("Accept", "application/json");

       retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");

       retrieveReq.onreadystatechange = function () {

           GetData(this);

       };

       retrieveReq.send();

    }

    function GetData(retrieveReq) {

       if (retrieveReq.readyState == 4) {

           if (retrieveReq.status == 200) {

               var retrieved = JSON.parse(retrieveReq.responseText).d;

               if (retrieved.results[0] != null) {

                   Xrm.Page.getAttribute("lineitemnumber").setValue(parseInt(retrieved.results[0].LineItemNumber) + 1);

               } else {

                   Xrm.Page.getAttribute("lineitemnumber").setValue(1);

               }

           }

       }

    }

  • Barbara Forsman Profile Picture
    Barbara Forsman 602 on at
    RE: Auto-increment Quote Products if Criteria is met

    Well... I am using Jason Lattimer's utility to uncover the mapping from Opportunity Product to Quote Product.  That is working just great.  If I can use JavaScript on the Opportunity Product to increment the Line Item Number when my custom field "Selected for Quote" changes to "Yes", I'm home free!!  almost...

    What would be the correct syntax to increment the Line Item Number if I call the JavaScript onChange on my Selected for Quote field?  

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Auto-increment Quote Products if Criteria is met

    Barbara,

    In this case JavaScript is not what you need. Unfortunately you will have to learn how to develop plugins using VS and .Net. Products from opportunity to quote are copied on the background so JavaScript will not trigger.

  • Barbara Forsman Profile Picture
    Barbara Forsman 602 on at
    RE: Auto-increment Quote Products if Criteria is met

    That's it!!  I need it to run on products that were added on the opportunity.  I've mapped them over from Opportunity Product to Quote Product.  I want to increment the Line Item Number only if the Opportunity Product has been selected for the Quote.  Should I be running this on the Opportunity Product instead and kick it off when that field changes?

    Thank you!!

  • Verified answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: Auto-increment Quote Products if Criteria is met

    If you are not getting that alert message, it either means there is something wrong with the script formatting (don't see anything wrong), or, possibly, you are opening an existing quote product? That first "if" condition makes sure the script will run for new records only..

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Auto-increment Quote Products if Criteria is met

    Good point that I missed! You should use QuoteId instead of SalesOrderId.

  • Barbara Forsman Profile Picture
    Barbara Forsman 602 on at
    RE: Auto-increment Quote Products if Criteria is met

    Should I also change SalesOrderId/Id to QId/Id?

    I added the web resource on the Quote Product, and added the Form OnLoad event and set the Function to Form_OnLoad.

    Thanks for your help!

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans