Friday, June 10, 2011

Date as per User Settings


//*********************************************************************
//**  DEFAULT DUE DATE:  UPON LOAD OF THE FORM, FOR CREATE ONLY, WE 
//**  SET THE DUE DATE TO THE CURRENT DATE AND TIME FOR THE CRM USER.
//*********************************************************************
var result = new Date();
if(crmForm.FormType ==1 )
{

fetchUserTime();
crmForm.all.scheduledend.DataValue = result;
}
//**************** **************************************
//*****************Get Current User ************************
//*******************************************************
function getCurrentUser()
{
//Create the XML that will fetch the required info.
var XMLRequest = "" + 
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" + GenerateAuthenticationHeader() +
" <soap:Body>" + 
" <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" + 
" <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" + 
" <q1:EntityName>systemuser</q1:EntityName>" + 
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" + 
" <q1:Attributes>" + 
" <q1:Attribute>systemuserid</q1:Attribute>" + 
" <q1:Attribute>fullname</q1:Attribute>" + 
" </q1:Attributes>" + 
" </q1:ColumnSet>" + 
" <q1:Distinct>false</q1:Distinct>" + 
" <q1:Criteria>" + 
" <q1:FilterOperator>And</q1:FilterOperator>" + 
" <q1:Conditions>" + 
" <q1:Condition>" + 
" <q1:AttributeName>systemuserid</q1:AttributeName>" + 
" <q1:Operator>EqualUserId</q1:Operator>" + 
" </q1:Condition>" + 
" </q1:Conditions>" + 
" </q1:Criteria>" + 
" </query>" + 
" </RetrieveMultiple>" + 
" </soap:Body>" + 
"</soap:Envelope>" + 
"";
try
{
//Create Http request object
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", XMLRequest.length);
xmlHttpRequest.send(XMLRequest);

//Store the response which would be XML
var Result = xmlHttpRequest.responseXML;

/*
The return is of type "BusinessEntity" if you were using similar code one server side.
Hence we need to select node of type "BusinessEntity"

In our case It should be not more one than one node
*/
var BusinessEntityNodes = Result.selectNodes("//RetrieveMultipleResult/BusinessEntities/BusinessEntity");

// Check If data was retrived
if (BusinessEntityNodes.length != 0)
{
    var BusinessEntityNode = BusinessEntityNodes[0]; 
    var SystemUserId = BusinessEntityNode.selectSingleNode("q1:systemuserid");
    var FullName = BusinessEntityNode.selectSingleNode("q1:fullname");
    var SystemUserId = (SystemUserId == null) ? null : SystemUserId.text;
    var FullName = (FullName == null) ? null : FullName.text;
}
return SystemUserId ;
}
catch (e)
{
alert(e.message);
}

}
//****************************************************************************
//***********************Fetch User Time in CRM**********************************
//****************************************************************************
function fetchUserTime()
{
var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
"  <soap:Header>" + 
"    <CrmAuthenticationToken xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" + 
"      <AuthenticationType xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">0</AuthenticationType>" + 
"      <OrganizationName xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">"+ORG_UNIQUE_NAME +"</OrganizationName>" + 
"      <CallerId xmlns=\"http://schemas.microsoft.com/crm/2007/CoreTypes\">00000000-0000-0000-0000-000000000000</CallerId>" + 
"    </CrmAuthenticationToken>" + 
"  </soap:Header>" + 
"  <soap:Body>" + 
"      <Request xsi:type=\"RetrieveUserSettingsSystemUserRequest\" ReturnDynamicEntities=\"true\">" + 
"        <EntityId>"+getCurrentUser()+"</EntityId>" + 
"        <ColumnSet xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:ColumnSet\">" + 
"          <q1:Attributes>" + 
"            <q1:Attribute>timezonebias</q1:Attribute>" + 
"          </q1:Attributes>" + 
"        </ColumnSet>" + 
"      </Request>" + 
"    </Execute>" + 
"  </soap:Body>" + 
"</soap:Envelope>" + 
"";

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

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

var resultXml = xmlHttpRequest.responseXML;
//Create an XML document that can be parsed.
var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
oXmlDoc.async = false;
oXmlDoc.loadXML(resultXml.xml);
//Value the user is interesed in in string form
var times = oXmlDoc.getElementsByTagName("Property");
var difference = Number(times[0].text);

//And the result - Date object
var gmtday= result.getUTCDate();
var gmtmonth = result.getUTCMonth();
gmtmonth=gmtmonth;
var gmtYear = result.getUTCFullYear();
var gmthours = result.getUTCHours();
var gmtminutes = result.getUTCMinutes();

result.setFullYear(gmtYear);
result.setMonth(gmtmonth);
result.setDate(gmtday);
result.setHours(gmthours);
result.setMinutes(gmtminutes);
result.setMinutes ( result.getMinutes() - difference);
}

//********************************************************************
//***************End of Fetch User Time**********************************
//********************************************************************