Question Status

Verified
Thadd.Bo asked a question on 17 Sep 2013 5:49 PM

Hello,

The quote products are sorted by product name but there is also a field called lineitemnumber, which seems to to be utilized. I would like to use it and order the products by this number.

Is there a little script that increased the lineitemnumber automatically if user add a new product?

Thank you for your help.

Reply
Suggested Answer
Jason Lattimer responded on 17 Sep 2013 7:22 PM

The best way would really be to use plugins but this code will increment line numbers. Basically it retrieves the last line item from the parent (in the case Opportunity) and adds 1. It doesn't do anything as far as re-ordering items is one of deleted of anything fancy - just the basic increment.

Make sure you have the LineItemNumber field on the form and add this code the the line item form's OnSave event.

function Line_OnSave() {
    if (Xrm.Page.getAttribute("lineitemnumber").getValue() == null) {
        var serverUrl = Xrm.Page.context.getClientUrl();
        var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/OpportunityProductSet?$select=LineItemNumber&$orderby=LineItemNumber desc&$filter=OpportunityId/Id eq guid'" + Xrm.Page.context.getQueryStringParameters()._CreateFromId + "'&$top=1";
        var req = new XMLHttpRequest();
        req.open("GET", oDataSelect, false);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json;charset=utf-8");
        req.onreadystatechange = function () {
            if (req.readyState === 4) {
                if (req.status === 200) {
                    var retrieved = JSON.parse(req.responseText).d;
                    if (retrieved.results.length > 0) {
                        Xrm.Page.getAttribute("lineitemnumber").setValue(parseInt(retrieved.results[0].LineItemNumber) + 1);
                    }
                    else {
                        Xrm.Page.getAttribute("lineitemnumber").setValue(1);
                    }
                }
            }
        };
        req.send();
    }
}

Jason Lattimer
My Blog -  Follow me on Twitter -  LinkedIn

Reply
Thadd.Bo responded on 18 Sep 2013 5:30 AM

Hi Jason,

this is great and the solution would with scripting would be ok but we do not use opportunitites we just right with the quote so does this code works also for quote or the quote producuts?

Does this also work in Outlook with offline mode?

Thank you already for your help.

Reply
Jason Lattimer responded on 18 Sep 2013 5:56 AM

Essentially you can change out 'Opportunity' with 'Quote' and it would work.

This would not work offline as when the line item is saved a query is being made to the system to get the last  line number used. Being that this is done in real-time there is't any capacity to handle this a user comes back online.

Jason Lattimer
My Blog -  Follow me on Twitter -  LinkedIn

Reply
Thadd.Bo responded on 19 Sep 2013 6:37 PM

Thank you, I will try this.

Regarding the offline function is the query to get the latest line number not been made to the local database? So then there would be no problem?

Reply
jregan responded on 20 Sep 2013 5:17 AM

Where would I add this script to create the line number?

Reply
Verified Answer
Jason Lattimer responded on 20 Sep 2013 5:20 AM

A couple links that might help:

Creating A JavaScript Web Resource

Use A JavaScript Web Resource In Your Form

It would attach to the form's OnSave event

Jason Lattimer
My Blog -  Follow me on Twitter -  LinkedIn

Reply
Verified Answer
Jason Lattimer responded on 20 Sep 2013 5:20 AM

A couple links that might help:

Creating A JavaScript Web Resource

Use A JavaScript Web Resource In Your Form

It would attach to the form's OnSave event

Jason Lattimer
My Blog -  Follow me on Twitter -  LinkedIn

Reply
Suggested Answer
Jason Lattimer responded on 17 Sep 2013 7:22 PM

The best way would really be to use plugins but this code will increment line numbers. Basically it retrieves the last line item from the parent (in the case Opportunity) and adds 1. It doesn't do anything as far as re-ordering items is one of deleted of anything fancy - just the basic increment.

Make sure you have the LineItemNumber field on the form and add this code the the line item form's OnSave event.

function Line_OnSave() {
    if (Xrm.Page.getAttribute("lineitemnumber").getValue() == null) {
        var serverUrl = Xrm.Page.context.getClientUrl();
        var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/OpportunityProductSet?$select=LineItemNumber&$orderby=LineItemNumber desc&$filter=OpportunityId/Id eq guid'" + Xrm.Page.context.getQueryStringParameters()._CreateFromId + "'&$top=1";
        var req = new XMLHttpRequest();
        req.open("GET", oDataSelect, false);
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json;charset=utf-8");
        req.onreadystatechange = function () {
            if (req.readyState === 4) {
                if (req.status === 200) {
                    var retrieved = JSON.parse(req.responseText).d;
                    if (retrieved.results.length > 0) {
                        Xrm.Page.getAttribute("lineitemnumber").setValue(parseInt(retrieved.results[0].LineItemNumber) + 1);
                    }
                    else {
                        Xrm.Page.getAttribute("lineitemnumber").setValue(1);
                    }
                }
            }
        };
        req.send();
    }
}

Jason Lattimer
My Blog -  Follow me on Twitter -  LinkedIn

Reply