This is a function I find myself writing quite often, so I thought I would just post it here for future reference. It's purpose is simple, check if an array contains an element with a given key and value, if it does, return it other wise return a null value.
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: https://jsfiddle.net/zdsv09wc/
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:
http://www.statisticalconsultants.co.nz/blog/how-the-world-separates-its-decimals.html
I have an Angular project in which I need to interact with a CRM 2016 instance, that is, I need to create a record and open/navigate to it from within Angular.
I dont want to use the javascript SDK files because well they just seem klunky and non angular -ish.. So instead I wanted to do it with just a regular service.. Turns out it was quite easy. The hardest part was figuring out where Microsoft stored the GUID of the created record (spoiler: they stuck it in the header, more details on that here).
First off you need to add a service (or copy paste the CreateRecord method from this service to your own).
myApp.service('dataService', function ($http, $rootScope, $timeout, $q) {
var pub = {};
var crmAPIURL = Xrm.Page.context.getClientUrl() + "/api/data/v8.1/";
pub.CreateRecord = function (entityName, record) {
var url = crmAPIURL + entityName;
return $http
(
{
method: "POST",
url: url,
data: record,
dataType: 'json',
timeout: 15000
}).then(function (data) { return data; });
}
return pub;
});
Now with that service method defined we can call it like this from one of our controllers:
$scope.CreateCompany = function ()
{
// define the entity record with all the fields you want to set.
var account = {
name: "My Test Company",
address1_line1: "SyntaxWarriors road 1337"
};
// call the service defined previously with the data and the name of the entity according to the web api
// the entity names are listed here: https://your_organization_name.crm4.dynamics.com/api/data/v8.1/
// in my case I wanted to create an account record which is called "accounts" (yes, the service tries to pluralize all things).
$dataService.CreateRecord("accounts", account).then(function (data)
{
// this will return something like: "https://your_organization_name.crm4.dynamics.com/api/data/v8.1/accounts(59e11f14-4489-e611-80de-c4346bacbdc4)"
// the service returns the GUID of the created record in the header.. for some unexplained annoying reason I dont know.
var entityUrl = data.headers("OData-EntityId");
// do some regex to get the data in between parentheses.
var getDataBetweenParenthesesRegex = /\(([^)]+)\)/;
var matches = getDataBetweenParenthesesRegex.exec(entityUrl);
// uncomment these if you want to see what you're actually getting back.
// console.log(data)
// console.log(matches); the guid matches
// the second match (1) is the guid without the (). Lets open a new window to that.
if (matches.length >= 1) {
Xrm.Utility.openEntityForm("account", matches[1]);
}
});
}
Many times when you are working with CRM it is usefull to be able to get the GUID of a record. It certainly makes my life alot easier when I can tell a coworker exactly which record I'm talking about, or when someone is testing they can give me the guid of the record that does not work.
There is a really simple way of getting the guid using a bookmark in your browser. Heres how to do it:
Now when you want the GUID of a crm record do this:
You should now get a popup with the GUID which looks something like this:
Also, if your interested, here is the unminified version of the javascript code.
If your are still using CRM 2011 use this code instead:
Unminified: