Quantcast
Channel: CRM Development forum
Viewing all articles
Browse latest Browse all 8223

MS CRM 2011 Online - How to retrieve unit for order product automatically from price list item?

$
0
0

How can I retrieve a unit for product automatically from a price list when I am creating a new order product?

I have managed to get a default unit of the product but now I need to get the unit which are defined under the price list.

This is the code which I used for retrieving the default unit.

/*
When a product value is changed set the Unit field to the Default Unit value specified on the Product entity
//The JSON2 Javascript library must be added to the form as a webresource and set to load before this javascript library
Set the Timer function to be fired onchange of the product field - this will wait for 100ms then launch the SetProductUnit function
*/
function Timer()

//This is the function that is fired onchange of the product field
//A timer is required before firing the SetProductUnit function
//More info here http://promx.wordpress.com/2011/06/27/setting-the-default-unit-for-opportunity-quote-and-salesorder-product-in-crm-2011-using-javascript/
{

    window.setTimeout(SetProductUnit(), 100);
}
 
function SetProductUnit()
//Set the Unit value to the default Unit of the product
{

    //Start building URI of rest endpoint
    //var serverUrl = "http://" + window.location.host + "/" + Xrm.Page.context.getOrgUniqueName();
	var serverUrl = document.location.protocol + "//" + document.location.host + "/" + Xrm.Page.context.getOrgUniqueName();
    //Get the value of the Product
    var productid = Xrm.Page.data.entity.attributes.get('productid').getValue();


 
    if (productid != null)
    {
 
        var productguid = productid[0].id;
 
         // Creating the Odata Endpoint
		
		
		var clear = productguid;
		var n=clear.replace("{", "").replace("}", "");
        var oDataPath ="https://destamatic.crm11.com/XRMServices/2011/OrganizationData.svc";
        var retrieveReq = new XMLHttpRequest();
        var Odata = oDataPath + "/ProductSet?$select=DefaultUoMId&$filter=ProductId eq Guid'" + n + "'";
        retrieveReq.open("GET", Odata, false);
        retrieveReq.setRequestHeader("Accept", "application/json");
        retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
		        retrieveReq.onreadystatechange = function ()
        {
            if (this.readyState == 4)
                    {
                        if (this.status == 200)
                                {
                                    var retrievedRecords = JSON.parse(retrieveReq.responseText).d;
                                    if(retrievedRecords.results.length > 0)
                                                    {
                                                        var product = retrievedRecords.results[0];
                                                        var uomguid = product.DefaultUoMId.Id;
                                                        var uomname = product.DefaultUoMId.Name;
                                                        var uomlogical = product.DefaultUoMId.LogicalName;
                                                        //Set the Unit lookup field with the value returned
                                                        Xrm.Page.getAttribute('uomid').setValue([{id: uomguid, name: uomname, entityType: uomlogical}]);
                                                    }
                                }
                    }
        }
    //Send the request
    retrieveReq.send();
    }
    else
    {
        //If the onchange event has been a delete of the Product or no product exists then clear the Unit field
        Xrm.Page.getAttribute('uomid').setValue(null);
    }
}


Viewing all articles
Browse latest Browse all 8223

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>