Formatting numbers in general is something I disslike and if I have to do it I only want to do it once. So here is a small javascript function I wrote to format currencies with configurable thousand separator as well as number of decimals and what separator for decimals. It also lets you append a string to the end such as "SEK" if you wish to add some form of currency or symbol.

Here is an example of different outputs.

formatMoney('0', 2, '.', ' ', ' SEK')  > 0.00 SEK
formatMoney('', 2, '.', ' ', ' SEK')  > 0.00 SEK
formatMoney('1.23', 2, '.', ' ', ' SEK')  > 1.23 SEK
formatMoney('12345.009', 2, '.', ' ', ' SEK')  > 12 345.01 SEK
formatMoney('5234231.9', 2, '.', ',', ' USD')  > 5,234,231.90 USD

And the code itself.

var formatMoney = function(value, decimals, decimalSeparator, thousandSeparator, currencyString) {
   if (value == null || isNaN(value))
      return "";

   var decimals = isNaN(c = Math.abs(decimals)) ? 2 : decimals;
   var decimalSeparator = decimalSeparator == undefined ? "." : decimalSeparator;
   var thousandSeparator = thousandSeparator == undefined ? " " : thousandSeparator;

   var negativeSign = value < 0 ? "-" : "";

   var valueNoDecimals = String(parseInt(value = Math.abs(Number(value) || 0).toFixed(decimals)));

   var spacingStart = 0;
   if ((valueNoDecimals.length) > 3)
      spacingStart = valueNoDecimals.length % 3;

   var leadingNumber = (spacingStart ? valueNoDecimals.substr(0, spacingStart) + thousandSeparator : "");
   var separatedMiddle = valueNoDecimals.substr(spacingStart).replace(/(\d{3})(?=\d)/g, "$1" + thousandSeparator);
   var decimals = (decimals ? decimalSeparator + Math.abs(value - valueNoDecimals).toFixed(decimals).slice(2) : "");

   var result = negativeSign + leadingNumber + separatedMiddle + decimals + currencyString;
   return result.trim();

Here is also a fiddle with the examples above:

If you're curious as to whom uses what decimal separator there is a nice chart here explaining how everyone except the EU is wrong:

CRM Fielder

2016-11-18 14:36 



This is a tool I've been creating over the last few months to simplify my work with Microsoft CRM. It's purpose is to expose and make it extremly quick to reach CRM Metadata such as entity names and  and field types etc.

The program currently has these functions implemented.

  • Search through all entities using logical or display names.
  • Search through all fields using logical or display names.
  • Search through both entities and fields at the same time finding any that match your query.
  • Display detailed information about specific fields
  • Generate enums that are copy paste ready for optionsets (se pictures below for examples).
  • Generate Excel documentation of entire CRM database metadata.
    • This includes all entities, both custom and standard
    • As well as all fields custom or not.
    • It lists all display names and field types.
    • Creates a separate sheet for all optionsets.
  • Lists all users and their guids (copy paste friendly for C#).


Read more about it and download here


Today I found out that you don't need a Mac if you want to debug a quirky looking web site on a iOS device.. something I did not think was possible..
But as it turns out this can quite easily be done using a windows 10 machine, a USB cable and an iOS device.

Here are the steps to do it.

Start out by getting these prerequisites: 

  • Install Itunes on your windows 10 Machine (there are drivers that come with it that we want) link.
  • Install Firefox link
  • Download iOS WebKit Debug Proxy Win32 but don't do anything with it yet. Make sure to take the x64 version if you have an x64 OS installed.
  • Have USB cable for you iOS device.

Once you have those things ready, enable debug mode in Safari on your iOS device by going to Settings > Safari > Advanced > Enable: Web Inspector.


Now connect you iOS device using a USB cable and make sure it comes up in ITunes, don't connect it through any type of USB hub (it refused to work through a HUB for me, and searching revealed several others had the same issue). 

Now unzip the iOS WebKit Debug Proxy Win32 into a folder of your choosing and run the .exe file. You might need to go through and unblock ALL the DLL files for it to work properly (open up their properties and click UNBLOCK, before I did this the console app just opened and closed with no error).


When you run the program you should see a console window with the following text. If you get errors here, make sure the device is connected and visible in Itunes and that it is unlocked. You might also want to try the x64 version if you are running the x86 version of the Webkit debug program. If that dosen't work, try disabling your firewall and see if that fixes it.


Once you have the console app working, open up Safari on the iOS device and start Firefox. Press Shift + F8 to open up the firefox WebIDE. It should look something like this, you might want to open the Console as well as it might provide som insight into possible errors when connecting (Ctrl + Shift + K with the main firefox window active):


Now you might assume that you should press the Safari Firefox and others button in the WebIDE, and you can try. For me this just threw a bunch of annoying errors in the console. What you should press is Chrome Desktop, which oddly enough just worked right away.

There are a lot of people complaining about this not working here: but as far as I can tell, as long as you press the Chrome button and not the Safari button it works.

The way this works is that you browse on the device as normal and in the Firefox WebIDE you can inspect the DOM and debug the scripts. When selecting a DOM element it is actually highlighted on the device too and updates and changes you make are reflected on the device as well. The network traffic itself seems to be unavailable though. 



I think most people whom use the generated service context for Microsoft CRM quickly note that it does not use the full fledged entity framework and as such does not support all queries. One of these limitations that I usually hit on is that I cant use contains in a Lambda query.

Ie. This does not work and fails with this error: Invalid 'where' condition. An entity member is invoking an invalid property or method.

private List<Xrm.Account> GetAccountsFromOrgNoList(string[] orgNos)
    return serviceContext.Xrm.AccountSet
        .Where(x => orgNos.Contains(x.lgc_Organizationnumber))  // <-- contains is invalid
        .Select( x=> x)

The work around for this is to use Query Expressions. Because they can do this, although not as nicely. The below code does the same as the above code with the added exception of actually working.

private List<Xrm.Account> GetAccountsFromOrgNoList(string[] orgNos)
    var qe = new QueryExpression(Xrm.Account.EntityLogicalName);
    qe.ColumnSet = new ColumnSet("accountid", "name", "cst_organizationnumber"); // can use new ColumnSet(true); if you just want all columns instead of specifying specif ones
    qe.Criteria.AddCondition("cst_organizationnumber", ConditionOperator.In, orgNos.Cast<Object>().ToArray());
    qe.Distinct = true;

    var results = serviceContext.Instance.RetrieveMultiple(qe).Entities
        .Select(x => x.ToEntity<Xrm.Account>())

    return results;

This is a piece of code I tend to need every few months but always need a few minutes to find. So I'm just going to put it on here for mine and others future reference.

The code returns a KeyValuePair list of all options in a CRM optionset.

public static List<KeyValuePair<string, int>> GetOptionSets(string entityName, string attributeName, IOrganizationService service)
    string AttributeName = attributeName;
    string EntityLogicalName = entityName;
    RetrieveEntityRequest retrieveDetails = new RetrieveEntityRequest
        EntityFilters = EntityFilters.All,
        LogicalName = EntityLogicalName
    RetrieveEntityResponse retrieveEntityResponseObj = (RetrieveEntityResponse)service.Execute(retrieveDetails);
    Microsoft.Xrm.Sdk.Metadata.EntityMetadata metadata = retrieveEntityResponseObj.EntityMetadata;
    Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata picklistMetadata = metadata.Attributes.FirstOrDefault(attribute => String.Equals(attribute.LogicalName, attributeName, StringComparison.OrdinalIgnoreCase)) as Microsoft.Xrm.Sdk.Metadata.PicklistAttributeMetadata;
    Microsoft.Xrm.Sdk.Metadata.OptionSetMetadata options = picklistMetadata.OptionSet;
    var ret = options.Options
        .Where(x => x.Label != null && x.Label.UserLocalizedLabel != null && x.Label.UserLocalizedLabel.Label != null && x.Value.HasValue)
        .Select(x => new KeyValuePair<string, int>(x.Label.UserLocalizedLabel.Label, x.Value.Value))

    return ret;

To use it call it as such:

GetOptionSets(Account.EntityLogicalName, "cst_sttributeWithOptionset", serviceContext.Instance);