Tuesday, February 9, 2010

CRM - Get and Set the current logged in user to a lookup control

There are many posts around finding the current user logged in and setting values in lookup controls for MSCRM 4.0. However, this posts puts them together. Some pratical uses may include:

--Stamping changes when multiple people save to a particular record where the Last Updated only shows one person.
--Auditing control.

Please let me know of any other applications for this code. The code listed in red is the only code you have to change to implement this in your environment.

/********************************************************************/
/* Get the current logged in user and set a systemuser lookup control
/********************************************************************/
var xml = "" +
"" +
"" +
GenerateAuthenticationHeader() +
" " +
" " +
" " +
" systemuser" +
" " +
" " +
" businessunitid" +
" firstname" +
" fullname" +
" lastname" +
" organizationid" +
" systemuserid" +
"
" +
"
" +
" false" +
" " +
" And" +
" " +
" " +
" systemuserid" +
" EqualUserId" +
"
" +
"
" +
"
" +
"
" +
"
" +
"
" +
"
" +
"";

var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);

var resultXml = xmlHttpRequest.responseXML;
var entityNode = resultXml.selectSingleNode("//RetrieveMultipleResult/BusinessEntities/BusinessEntity");

var systemUserIdNode = entityNode.selectSingleNode("q1:systemuserid");
var fullNameNode = entityNode.selectSingleNode("q1:fullname");

//fill current user
var lookupData = new Array();

var lookupId = systemUserIdNode.text;
var lookupTypecode = 8;
var lookupName = fullNameNode.text;

lookupData[0] =new LookupControlItem( lookupId, lookupTypecode, lookupName);

crmForm.all.new_userid.DataValue = lookupData;
crmForm.all.new_userid.ForceSubmit = true;
}

No comments:

Post a Comment