Looking for PowerObjects? Don’t worry, you’re in the right place! We’ve been part of HCL for several years, and we’ve now taken the final step in our acquisition journey: moving our website to the HCL domain. Nothing else is changing – we are still fanatically focused on Microsoft Business Applications!

PowerObjects Blog 

for Microsoft Business Applications


Use JavaScript to Update Price Per Unit When Selecting a Unit in Dynamics CRM 2011

Post Author: Joe D365 |

This blog will review the JavaScript you can use to allow the user to see the 'Price' from the Price List for a selected Unit. If you use the Product Catalog in Microsoft Dynamics CRM 2011, this can save time. This can be used/modified for Quote Product, Order Product, or Invoice Product. The example below is for Opportunity Product. See also our previous example, where the user only has to select a Product, and the 'priceperunit' and Unit is populated.

Notice! – Before JavaScript will work, make sure to add the SDK.REST library, JQuery, and JSON2 library. These libraries are found in the SDK, available for download. The files must be added as web resources and added to the Form Library for Opportunity Product. Also, this JavaScript must be added as web resource and to trigger upon the OnChange event for Unit. See below.

We'll go through this step by step. (Some of the images may appear small. For a bigger view, right click and select View Image. JavaScript is also posted at the bottom of the blog post.)

We started by checking to make sure the Product has been selected on the form, and store that GUID in 'productFieldId'


Since the GUID is not null, we will enter more logic. Next we get the Attribute value of the selected Unit. This looks at field 'uomid' on the form. We won't be using this value in this function, but will pass it along to the next function.

Next we will grab the GUID for the parent opportunity. We check to make sure it is not null. You may notice that 'opportunityid' is not on the form. That's ok. We will use this GUID to lookup the Price List from the parent opportunity.

Once we have the Opportunity GUID, we can build our oData query for Ajax. Here we are grabbing the page context, the server URL, adding the DataPath (notice this is for CRM 2011 On-premise). As for oDataSelect, we add our predefined oData Query that we generated using the OData Query Designer. Notice I inserted our opportunityId into the query to have it return the PriceLevelId. PriceLevelId is really the Price List guid we are looking for.

NOTICE! – You will want to double check your own value for oDataPath as it may be different for your On-Premise or Online Dynamics CRM.

Now we can conduct our Ajax call. The Ajax call itself won't return the GUID to our main method, but will call a separate function upon success. This success function will implicitly return two values, data and status. If we want to pass along more parameters, we must call another method that will allow us to pass more than just data and status. Since I will need oDataPath, productFieldId, and the Unit GUID in our next Ajax call, I will pass these along to my custom function – NowWeHaveThePriceList()

The NowWeHaveThePriceList() function is now called. The 'data' parameter that we had passed holds the Price List Guid, and can be accessed from data.d.results. Here we store this in pricelevelId.

Now that we have the Unit Guid, the Product Guid, and the Price List Guid, we can use all three to query the Price List Item, and return the 'Amount' value. Here I have inserted all three GUIDs in my string query. Once again, I am calling a custom function NowWeHavePrice. Notice this time we are just calling the function without any added parameters. That is because upon Success, the Ajax call will implicitly pass two parameters, data and status. Since we don't need to pass any other parameters, we can just call NowWeHavePrice.

Once in the custom function, we will check to make sure that data is not null, and then store the Amount value into priceListItemAmount. Once here we can use eval to set the value for 'priceperunit'

Between the JavaScript described in this post and what we described in our last post, you now have some powerful additions to make life easier for entering products!

JavaScript below:

[sourcecode language="Javascript"]

function onChangeUnit(){

var productField = Xrm.Page.getAttribute('productid').getValue();

var productFieldId = productField[0].id; //store the product guid here

if (productFieldId != null)//if product not null get guid for unit

{

var unit = Xrm.Page.getAttribute('uomid').getValue();

//get guid for Parent opportunity

var opportunityId = Xrm.Page.getAttribute('opportunityid').getValue();

if (opportunityId != null) //if opportunity guid is null, discontinue

{

//build query

var pagecontext = Xrm.Page.context;

var serverUrl = pagecontext.getServerUrl();

var oDataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc/";

var oDataSelect = oDataPath + "OpportunitySet?$select=PriceLevelId&$filter=OpportunityId eq guid'" + opportunityId + "'";

//start AJAX call for the parent price list guid

$.ajax({

type: "GET",

contentType: "application/json; charset=utf-8",

datatype: "json",

url: oDataSelect,

beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },

success: function (data,status) { NowWeHaveThePriceList(data,status,productFieldId,unit,oDataPath);},

error: function (XmlHttpRequest, textStatus, errorThrown) {

alert('OData Select Failed: ' + errorThrown + ". There is no parent price list");

}

});

}

}

}

function NowWeHaveThePriceList(data,status,productFieldId,unit,oDataPath){

var priceLevelId = data.d.results[0].PriceLevelId.Id; //retrieved pricelevel

//start AJAX clal for Price List Item entities = Price List GUID and Product Guid and Unit Guid and return the Amount on that price list item. This will return one results.

var oDataSelect2 = oDataPath + "ProductPriceLevelSet?$select=Amount&$filter=(PriceLevelId/Id eq guid'" + priceLevelId +

"' and ProductId/Id eq guid'"+ productFieldId + "' and UoMId/Id eq guid'" + unit[0].id + "')";

$.ajax({

type: "GET",

contentType: "application/json; charset=utf-8",

datatype: "json",

url: oDataSelect2,

beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },

success: NowWeHavePrice,

error: function (XmlHttpRequest, textStatus, errorThrown) {

alert('OData Select Failed: '+ errorThrown + ". There is no price list item for this product");

}

});

}

function NowWeHavePrice(data,status){

if (data != null){

//set the price per unit

var priceListItemAmount = data.d.results[0].Amount.Value ;

Xrm.Page.getAttribute("priceperunit").setValue(parseFloat(eval(priceListItemAmount)));

Xrm.Page.getAttribute("priceperunit").setSubmitModeAlways;

}

}
[/sourcecode]

Happy CRM'ing!

Joe CRM
By Joe D365
Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.

PowerObjects Recommends