Saturday, May 7, 2016

Using FetchXML in a CRM form without having to use string concatenation

I am a solution architect using Microsoft Dynamics CRM, and I must be the laziest programmer I know. I HATE to write FetchXML code embedded in Javascript. But I am getting over it. I started using FetchXML Builder add-on to XRM Toolbox and that was very helpful for working with CRM Online. But last week I needed to query the Product table in a custom form to pull back a list price of a product, and I was dreading the thought of having to add all the little quotes and doing string concatenation because I knew I would be tweeking it and repeatedly making changes in FetchXML builder, them moving the query code back to Javascript and making it a bunch of concatenated strings (wash, rinse, repeat).

If I were putting the FetchXML into a C# program, no problem because that language supports having multi-line strings. And I could use Format.string() to insert my parameters into the code so I don't have to concatenate that either.

Not so much in Javascript. So I did some research and found some articles on how it could be done. I can put the FetchXML between comments and then use a regular expression to make it into a legit string. As long as nobody turns on 'compression' on the web server, it should continue to work. The code looks like this:

function fetchGetProduct() {
try {
// Get the price of the product requested
var sFetch = (function () {/*
<fetch count="1" >
<entity name="product" >
<attribute name="price" />
<filter type="and">
<condition attribute="productid" operator="eq" value="{0}" />
</filter>
</entity>
</fetch>
*/}).toString().match(/[^]*\/\*([^]*)\*\/\}$/)[1];
// use this trick to insert the GUID into the fetch query at position {0}
var v1 = Xrm.Page.getAttribute("new_quickaddproductid").getValue()[0].id;
sFetch = sFetch.format(v1);
// alert(sFetch);
_oService = new FetchUtil(_sOrgName, _sServerUrl);
// get the price from CRM, and use updatePrice() as the callback
_oService.Fetch(sFetch, updatePrice);
}
catch (e) {
}
}
function updatePrice(results) {
// given the results of the FetchXML
// get the list price from the product it returned
if (results.length > 0) {
var listprice = results[0].attributes["price"].value;
// update the price field on the custom form
Xrm.Page.getAttribute("new_quickaddamount").setValue(parseFloat(listprice));
}
}
// this function lets me replace a positional parameter in
// First, checks if it isn't implemented yet.
if (!String.prototype.format) {
String.prototype.format = function () {
var args = arguments;
return this.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
view raw fetchGetProduct hosted with ❤ by GitHub
The last function in this takes care of replacing the {0} in my FetchXML with the GUID I need in the query. Line 15 of my code gets the GUID from a lookup field, and Line 16 calls the Format function to replace the {0} parameter with the GUID.

Friday, May 6, 2016


I am ready for email 2.0.
Have you ever heard someone say that your email inbox is really full of other peoples agendas?
I want to be able to say what my agendas are, and make the sender tell me why their message should be on my agenda.

In fact, when I get an email, why is it that I cannot add my own information (metadata) to it?
The best that any system seems to offer is to let me put it into a folder. Really?
Aren't we past the whole idea that everything lives in some kind of taxonomy?

What if an email relates to two different subjects?
For example, an expense receipt for a customer might be categorized under receipts, expense, and a customer name.
When I get an email, I want to add some of my own keywords to it so that I can cross reference it different ways.

Don't get me wrong, I still like the folders, but they are the _only_ metadata I can easily add to my mail.
Well...not the only metadata.
I have Outlook and I can add multiple categories, but it is limited to a small number and I bet I would have more than it supports.


Thanks for letting me get that out of my system.
I found this article on Outlook best practices that I like.
https://support.office.com/en-us/article/Best-practices-for-Outlook-2010-f90e5f69-8832-4d89-95b3-bfdf76c82ef8

Back to the inbox.