Thursday, December 31, 2009

MS CRM 3.0 "Expected '}'" and "Object expected" errors

Hello, there :-)

Have you ever encountered this situation? You write some decent, valid JS code, throw it inside a CRM field event box, Save & Close x2, publish, test, BAM!-error?

Well, provided your code is actually correct (no logical, typing or access errors), you should check the next thing. In CRM 3.0 at least (not having a 4.0 box around atm., feel free to test if you're kind enough) if you load a piece of code having its last line using a line comment, you're in for trouble. I was lucky to find that out while debugging a whole page and noticed that the line comment I was using was extended to the rest of the CRM JS code (due to CRM placing the CATCH(e){} part of the TRY{} block right on the same line with the last line of my code). That was causing havoc to the entire page.

So, remember this hint: never leave the last line of code having a line comment ( "//" ) or you're in for trouble :D

Cheers!

Friday, December 18, 2009

CRM 4.0 Server - Hardcore Installing

There are moments when installing the Microsoft CRM 4.0 Server can be a real pain, if your Organization doesn't offer you full "trust" in its AD (Active Directory). After some digging, finally found a working solution (start to end :P):

step 1: Find a nice Active Directory Admin to manually create 5 groups for you in the Organization Node, with full privileges for the username that you will install the CRM 4 Server with:
PrivUserGroup
PrivReportingGroup
ReportingGroup
SQLAccessGroup
UserGroup


step 2: Create a custom precreateconfig.xml file that will look like this (ask the same friendly guy from first step to assist you with this):
<crmsetup><server><groups autogroupmanagementoff="true"><privusergroup>CN=PrivUserGroup,OU=Company Name,OU=Company Name,DC=<domain>,DC=<domain_extension></privusergroup> <sqlaccessgroup>CN=SQLAccessGroup,OU=Company Name,OU=Company Name, DC=<domain>,DC=<domain_extension></sqlaccessgroup> <usergroup>CN=UserGroup,OU=Company Name,OU=Company Name,DC=<domain>,DC=<domain_extension></usergroup> <reportinggroup>CN=ReportingGroup,OU=Company Name,OU=Company Name, DC=<domain>,DC=<domain_extension></reportinggroup> <privreportinggroup>CN=PrivReportingGroup,OU=Company Name,OU=Company Name, DC=<domain>,DC=<domain_extension></privreportinggroup> </groups></server></crmsetup>

step 3: Go Start > Run > cmd (Command Prompt) and type:
<Drive:>\CRM 4 Kit\Server\i386\SetupServer.exe /config <Drive:>\Path_to_your_creation\precreateconfig.xml

step 4:
Pray for no further random errors while enjoying your progress bars @ Setup Screen:

step 5: After setup is completed successfully, ask the same kind AD Admin to re-add the CRM Administrator (the one you installed with) in the PrivUserGroup (with Full Privileges). See step 1 for details...
step 6: Go http://<hostname>:<port>/ and enjoy your CRM (now you will most likely have the right to finally use the product you <paid> for :)
step 7: ???
step 8: Profit!

For a detailed article about 90% of this issue, you can study this MSDN KB article.

Also, if your setup failed and cannot be uninstalled/repaired, while experimenting OTHER ways of installing than the one described above, I recommend you study this MSDN KB article as well. You gotta try this one once if you wanna train your fingers in deleting keys in RegEdit @ light speed...

Cheers and may the patience be with you :)

Wednesday, December 16, 2009

WSDL Error: "Schema item 'element' named 'string' from namespace 'http://schemas.microsoft.com/crm/2006/WebServices'. [...]"

Hey,

Have you ever encountered the following error?
"Schema item 'element' named 'string' from namespace 'http://schemas.microsoft.com/crm/2006/WebServices'. The global element 'http://schemas.microsoft.com/crm/2006/WebServices:string' has already been declared."

That's what I got when I update my webservice references. Totally nagging and a real pain. That error prevented me from being able to access all the custom fields I created on the CRM entities.
Silly as it might sound, the sole answer to this problem was to take a deep breath (no, no Skype chat), backup the WSDL file and perform some orcish style surgery.

My troubling line was this:
<s:element name="SecurityPrincipal" nillable="true" type="s2:SecurityPrincipal">
<s:element name="string" nillable="true" type="s:string">
<s:element name="TargetFieldType" type="tns:TargetFieldType">

Make sure you have a backup to the original WSDL file, then nuke (a.k.a. "remove") the red-highlighted line and save the changes to the WSDL file (in my case, it was the WSDL file corresponding to the CRM SDK service). Unless there are other errors, you will be able to build your project and your customizations will be available within Visual Studio.

Good luck and happy coding afterwards!
Cheers!

Wednesday, December 02, 2009

How to extract Email attachments IDs (CRM 3.0)

Howdy,

At some point, I was requested by a manager to extend the functionality of the email entity (multiplying the number of messages sent) and I came across the attachment side. The following function is my solution to the occurring issue:

// Author: Octavian Cucuta ( octavian.cucuta [ AT ] gmail.com )

// Release: 1.0.0.1 ( 2nd of December 2009 ) for MS CRM 3.0
// * Gets the IDs of the uploaded attachments, as a string constant with IDs separated by pipes '|' characters
function GetEmailAttachmentsIDs()
{
// Returning results as a string, but locally storing data into an array, for a more practical approach
var result = Array();
// Encasing it all within a TRY{}CATCH{} block, to prevent unwanted errors
try
{
// Get table containers
var myTables = document.getElementsByTagName("table");
// Since MS did not specify a unique ID or name for the table,
// checking which one has the correct 'oname' tag.
// Tip: on email, the table we need is the 48th
for(var i = 0; i < myTables.length; i++)
{
if(myTables[i].oname == "1001")
{
// Get attachment IDs
var index = 0;
for(var j = 0; j < myTables[i].rows.length; j++)
{
result[index] = "";
result[index++] = myTables[i].rows[j].oid;
}
// Cancelling the processing loop here
break;
}
}
}
catch (err)
{
// In case something went wrong, instead of providing a partial set of attachments, return none
result = Array();
}

// Format output as a single string constant, separated by pipes
return result.join('|');
}



// Usage: place this function inside the needed event code body, then use the following call
var ids = GetEmailAttachmentsIDs();

Monday, October 20, 2008

JavaScript code reuse in CRM

In most of the cases, you don't want to copy-paste your custom methods in your CRM events source code each time you need them.

So, here's an easy approach on javascript code reuse in CRM.

First of all, make sure you have these 2 files: LargeNumber.js and StringsExtended.js located in a custom folder - named CRMStuff - that is also located in the root folder of the Microsoft Dynamics CRM server application (the relative paths to the files should be "/crmstuff/LargeNumbers.js" and "/crmstuff/StringsExtended.js"). As you can see, these 2 files contain custom methods.

Click here to download the source code that allows you to load your *.js files at runtime.

Monday, September 08, 2008

Welcome Tavi

Let's give a big welcome to our newest author - Mr. Octavian Cucuta on CRM Stuff blog. He promises a lot :P

Large Integer division remainder

Recently, I had to obtain the remainder of a (seriously) large integer division (32 digits long divided by another shorter integer; base 10) and found that JavaScript reliably supports operations only on 15-digit long integers. Thus, this what I came up with:

// Gets the remainder from the division operation for LARGE numbers
// Support: positive integers; first parameter must not be 0
function GetRemainder(x, y) {
if ((y != 0) && (x.slice(0, 1) != "-") && (y.slice(0, 1) != "-"))
{
var nr = x;

// Removing the potential first '+' character
if (nr.slice(0, 1) == "+") {nr = nr.slice(1);}
if (y.slice(0, 1) == "+") {y = y.slice(1);}

// Removing leading blank and tab characters on both numbers
while ( (nr.charCodeAt(0) == 32) || (nr.charCodeAt(0) == 9) ){
nr = nr.slice(1);
}
while ( (y.charCodeAt(0) == 32) || (y.charCodeAt(0) == 9) ){
y = y.slice(1);
}

var len = nr.length;

// Checking if any non-digit characters are embedded into the input string
var i = 0;
for (i = 0; i < len; i++)
if (!((nr.charCodeAt(i) >= 48) && (nr.charCodeAt(i) <= 57)) ) {
return -1;
}

var number = parseInt(y,10);
var remainder = 0;
var ignore = 0;
var k = '';

while (ignore < len) {
if (remainder == 0) {
var w1 = y+' ';
var w2 = w1.length-1;
k = nr.slice(ignore, ignore+w2);

ignore += w2;
remainder = parseInt(remainder+k, 10) % number;
} else {
k = nr.slice(ignore, ignore+1);
ignore += 1;
remainder = parseInt(remainder+k, 10) % number;
}
}

return remainder;
}

return -1;
}


Usage:

document.write(GetRemainder("12345678901234567890123456789012", "123"))

will output 27.

Friday, September 05, 2008

OnChange Event

When starting to work with CRM, one of the first things you learn is how to put some script on the onChange event. It's easy to do this and it really works.
Let's say you want to fire that script at the first moment your page is loaded.
In CRM 3.0 you could do that writing a single line :
crmForm.all.yourfield.FireOnChange();
When starting to work with CRM 4.0 I tried to use the same code but it didn't work anymore. I was sooooo disappointed and I hardly found out the solution. That's why i thought of sharing it with you.
The code for firing a script put on the onchange event for CRM 4.0 is:
yourfield_onchange0();

I hope this helps. Happy coding! :)

Random Custom Serial Number Generator

A brief example on how to generate random serial numbers with JavaScript. Enjoy!

// Symbols Array (A-Z, 0-9)
var Symbols = new Array();

// Gets the ascii code for a certain character
function GetAsciiBySymbol(letter)
{
var code = letter.charCodeAt(0);
return code;
}

// Populates a certain array with the A-Z, 0-9 symbols
function PopulateArray(array) {
var Position = 0;

// Letters
for(i=GetAsciiBySymbol('A'); i<=GetAsciiBySymbol('Z'); i++) {

array[Position] = String.fromCharCode(i);
Position++;
}

// Numbers
for(i=GetAsciiBySymbol('0'); i<=GetAsciiBySymbol('9'); i++) {
array[Position] = String.fromCharCode(i);
Position++;
}

}

// Gets the array symbol by a certain index
function GetSymbol(index) {
return Symbols[index];
}

// Generates a new Serial Number
function GenerateSerial(serialLength, separatorSymbol, separatorPositions) {
var serial = "";
var Offset = 0;

for(i=0; i
var rnd = Math.floor(Math.random()*Symbols.length);
serial += GetSymbol(rnd);
}

if(SeparatorPositionsOk(separatorPositions,serialLength)) {
for(i=0; i
var position = separatorPositions[i];
serial = serial.substring(0,position + Offset) + separatorSymbol + serial.substring(position + Offset, serial.length); Offset++;
}
}

return serial;
}

// Checks if a certain array contains only items less than a maximum value
function SeparatorPositionsOk(array, maxValue) {
for(i=0; i maxValue) {
return false;
}
}

return true;
}


// Populate Symbols
PopulateArray(Symbols);

// Example 1 - Format: XXXXX-XXXXX-XXXXX-XXXXX-XXXXX
var SepPos = new Array();
SepPos[0] = 5;
SepPos[1] = 10;
SepPos[2] = 15;
SepPos[3] = 20;
var NewSerial = GenerateSerial(25, "-",SepPos);
alert("Serial Number: " + NewSerial);


// Example 2 - Format: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
SepPos = new Array();
SepPos[0] = 8;
SepPos[1] = 12;
SepPos[2] = 16;
SepPos[3] = 20;
SepPos[4] = 24;
NewSerial = GenerateSerial(32, "-",SepPos);
alert("Serial Number: " + NewSerial);

// Example 3 - Format: XXX-XXXXXXX
SepPos = new Array();
SepPos[0] = 3;
NewSerial = GenerateSerial(10, "-",SepPos);
alert("Serial Number: " + NewSerial);

Replace Chars in String - prototype example

An elegant way to replace certain chars from a string with new ones:

// Custom method for replacing certain chars from a string with new ones
function ReplaceChars(oldValue,newValue) {
var newText = this.split(oldValue);
newText = newText.join(newValue);
return newText;

}

// Attach the custom method to string object
String.prototype.Replace = ReplaceChars;

// Usage
var myString = "Hello World!";
alert(myString.Replace("o","0")); // will output "Hell0 W0rld!"