Friday, June 20, 2008

Changing Lookup Field Value using Javascript

/*
Author : Syed Ahmed Yasir
Sr. Software Eng.
Enterprise Business Solutions
yasir@bussolpk.com
0333-2219078

Dated : 20th June 2008
Purpose : To Set the Account in Customer Field and Set the Contact in Responsible Contact field
*/
/*Setting the contact ==================================================*/
var lookupItem = crmForm.all.customerid.DataValue;crmForm.all.responsiblecontactid.DataValue = lookupItem;

/*==================================================*/
//Restricting the Account Selection in CustomerID Field
//=================================================
crmForm.all.customerid.setAttribute("lookuptypes", "1");
//=================================================
//Accessing the Database to get the Parent Account of the Contact
//=================================================
var contactid = lookupItem[0].idvar connection = new ActiveXObject("ADODB.Connection");
var connectionString = "Provider=SQLOLEDB;Server=crmdev;Database=inbox_mscrm;Integrated Security=sspi";
connection.Open(connectionString);
var query = "select cb.accountid,ab.name from contactbase cb inner join accountbase ab on cb.accountid = ab.accountid where cb.contactid = '"+ contactid +"'";

var rs = new ActiveXObject("ADODB.Recordset");
rs.Open(query, connection, 1, 2);
rs.moveFirst();
var accountid;
var accountname;
while (!rs.eof)
{
accountid = rs.Fields(0).Value.toString() ;
accountname = rs.Fields(1).Value.toString();
rs.moveNext();
}
connection.Close();
/*=================================================
To Set the Fetched Account in Customer Field*/

var lookupData = new Array();
var lookupItem= new Object();
lookupItem.id = accountid;
lookupItem.typename = 'account';
lookupItem.name = accountname;
lookupData[0] = lookupItem;
crmForm.all.customerid.DataValue = lookupData;

No comments: