Tuesday, November 27, 2007

How to retrieve the ID of a parent entity

I saw this topic several times in the Microsoft CRM Developer Community and I decided to share this line of code with you :)

Let's take a simple example. We want to retrieve the Quote's ID (guid) when we open the Quote Details form (pressing the New Quote Product on the Quote entity). All we have to do is add the following code on the OnLoad() Event of the child entity (Quote Detail in our case):

var MyOpenerID = opener.document.crmForm.ObjectId;
alert(MyOpenerID);

In the same way, we can also take other values (fields) from the parent entity. Feel free to test ;)

Wednesday, November 21, 2007

Microsoft Dynamics CRM Exam - MB2-423

Done... Complete... 95% passing score... Thanks for the support everyone, especially EVO Software and Thomas Davin :)

Sunday, November 18, 2007

Microsoft Dynamics CRM Exam

This week, on the 21st of November, I'll sustain my first CRM Exam, CRM-30-423 - Microsoft Business Solutions - CRM 3.0 Applications. Wish me luck all :)

Saturday, November 17, 2007

Using the "onmousemove" event

The "onmousemove" event can help you make sure that your script runs as well as your cursor on the form :) I'll give you a brief example of its usage. We want to trap the cursor's coordinates as it moves in a certain entity's form in CRM.

Add the following code on the OnLoad() event of a form you want:

document.onmousemove = function() {
window.status = event.clientX + "x"+event.clientY;
}



Of course, you may attach the "onmousemove" event to the crmForm too, using:

crmForm.onmousemove = function() {
window.status = event.clientX + "x"+event.clientY;
}


But this way, your status will update only if your cursor moves inside the crmForm, not in the entire window like in our first example.

Saturday, November 10, 2007

Using the "onkeyup" event

Let's add some interactivity to our customizations :) For example, we have 4 fields: Amount (Schema Name amount, Type float(2)), Discount
(Schema Name discount, Type float(2)), Tax (Schema Name tax, Type float(2)) and Total Amount (Schema Name totalamount, Type float(2)) and we
want to calculate in real time the Total Amount based on the Amount, Discount and Tax.

First let's consider the following formulas:

Tax = 1.19 * Amount

Discount - between 1 and 100 (percentage of Amount)

Total Amount = (100 - Discount)/100 * Amount + Tax


Second, we create the function that calculates the Total Amount on the basis of the other 3 and we type the following code on the OnLoad() event of a form which contains our fields:

function updateTotalAmount() {

var amount = crmForm.all.amount.DataValue;

var discount = crmForm.all.discount.DataValue;

var tax = crmForm.all.tax.DataValue;

var total;

tax = 0.19 * amount; //we assume that the tax represents 19% of the amount

total = (100 - discount) / 100 * amount + tax;

crmForm.all.tax.DataValue = tax;

crmForm.all.totalamount.DataValue = total;

}


And now, we attach an "onkeyup" event to the amount, discount and tax fields. The event will call our function, like this:


crmForm.all.amount.attachEvent("onkeyup",updateTotalAmount);

crmForm.all.tax.attachEvent("onkeyup",updateTotalAmount);

crmForm.all.discount.attachEvent("onkeyup",updateTotalAmount);


Now, we may test the interactivity of our form. Type a value for the Amount and Discount fields and you'll see, in real time, how the Total Amount field updates.

Here's a printscreen of our example:


onkeyup event

Sunday, November 04, 2007

How to read / write files using JavaScript

To read and write files from JavaScript, we will use an ActiveX Object from the Scripting.FileSystemObject library, which knows how to handle files. The second parameter of the OpenTextFile function represents what we want the object to do with the file (read = 1, write = 2, append = 8).

And here are the scripts:

1. Reading from file

function ReadFromFile(sText){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.OpenTextFile("C:\\example.txt", 1, true);
row = s.ReadLine(); //also we can use s.ReadAll() to read all the lines;
alert(row);
s.Close();
}


2. Writing to file

function WriteToFile(sText){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.OpenTextFile("C:\\example.txt", 8, true); //if we use 2 instead of 8, the file will be overwritten;
s.WriteLine("new line added");
s.Close();
}


Though, to make this scripts work in Internet Explorer, you must go to Tools/Internet Options/Security, add your page to Trusted sites, then go to Custom level... and look for Initialize and script ActiveX controls not marked as safe for scripting. Enable it and restart the Internet Explorer.