Set a default pricelist to an Invoice etc…

The solution below implements Using the FetchXML CRM 2011 Service within a JavaScript Web Resource.

99% of our customisations require the pricelist to be pre-filled.  Here is a very quick snippet to populate the price list based on a supplied name and currency.

Firstly, the onload event:

  1. try {
  2.  
  3.     if (Xrm.Page.getAttribute("pricelevelid").getValue() == null) {
  4.         var priceLookup = GetPriceLevelId("Personal", Xrm.Page.getAttribute("transactioncurrencyid").getValue()[0].id);
  5.  
  6.         Xrm.Page.getAttribute("pricelevelid").setValue(priceLookup);
  7.  
  8.     }
  9. }
  10. catch (err)
  11. { }

 

Library function:

  1. function GetPriceLevelId(Name, CurrencyId) {
  2.  
  3.     var fetchXml = "<fetch mapping='logical'> " +
  4.                         "<entity name='pricelevel'> " +
  5.                         "    <attribute name='name'/> " +
  6.                         "   <filter type='and'> " +
  7.                         "        <condition attribute='name' operator='eq' value='" + Name + "'/> " +
  8.                         "        <condition attribute='transactioncurrencyid' operator='eq' value='" + CurrencyId + "'/> " +
  9.                         "    </filter> " +
  10.                         "</entity> " +
  11.                     "</fetch> "
  12.  
  13.     fetchService = new FetchUtil();
  14.     var pricelevelid = fetchService.Fetch(fetchXml)[0].attributes["pricelevelid"].value;
  15.  
  16.     var lookupData = new Array();
  17.     var lookupItem = new Object();
  18.     lookupItem.id = pricelevelid;
  19.     lookupItem.entityType = 'pricelevel';
  20.     lookupItem.name = Name;
  21.     lookupData[0] = lookupItem;
  22.     return lookupData;
  23. }

 

Enjoy , and Happy Christmas to all.

This entry was posted in CRM 2011 and tagged , , . Bookmark the permalink.

Comments are closed.