Friday, April 11, 2008

Using JavaScript to access the CRM offline database

The stunnware site has lots of great CRM JavaScript examples. One of them shows how to use JavaScript to access the CRM database. The same technique can be used to access the local offline database by changing the connection string to: "Provider=SQLOLEDB;Server=.\\CRM;Database=MSCRM_MSDE;Integrated Security=sspi".

Here is an example:


var accountGuid;

accountGuid = document.getElementById("your fieldname here");


if (IsOnline())

{

// write code for online version here.

}

else

{

var iCount = 0;

var connection = new ActiveXObject("ADODB.Connection");

var connectionString = "Provider=SQLOLEDB;Server=.\\CRM;Database=MSCRM_MSDE;Integrated Security=sspi";

connection.Open(connectionString);

var query = "SELECT count(*) FROM AccountBase WHERE AccountID='" + accountGuid.value + "'";

var rs = new ActiveXObject("ADODB.Recordset");

rs.Open(query, connection, /*adOpenKeyset*/1, /*adLockPessimistic*/2);

rs.moveFirst();

while (!rs.eof) {

iCount = rs.Fields(0).Value;

rs.moveNext();

}

connection.Close();


if (iCount > 0 ) {

//do something useful here

}

else {

//do something else here

}

}

No comments: