29. August 2011 14:53
var msg; // ist die zu übermittelnde Nachricht
var resultSet; // ist die Antwort auf die SOAP-Nachricht
var results; // Speichert die Ergebnisse der Antwort im Array
// ----------------------------------------------------------------------------
// Methoden
// ----------------------------------------------------------------------------
// Setzt bei Neuauswahl alle zur Firma gehörigen Felder
// ----------------------------------------------------------------------------
SetAccountFields = function() {
//ID des Lookup-Feldes holen
var lookupID = GetLookupId(Xrm.Page.getAttribute("new_kundeid").getValue());
alert(lookupID);
msg = BuildSoapMsg(1, lookupID); // Aufbau der SOAP-Nachricht die gesendet werden soll (1 = Typ der Nachricht)
resultSet = SendSoap(msg, 1); // Sendet die SOAP-Nachricht und speichert die Antwort in resultSet
if (resultSet != null) {
alert(resultSet.getElementsByTagName('result'));
results = resultSet.getElementsByTagName('result');
if (results.length > 0) {
alert("Test2");
}
}
}
GetLookupId = function(source) {
var result = '';
if (source[0] != null) {
result = source[0].id;
}
return result;
}
// Baut die SOAP-Nachricht je nach type zusammen
// Nachrichtentyp: Account = 1, Contact = 2, UserId = 3, UserRollen = 4
BuildSoapMsg = function(type, param) {
var soapmsg = "<?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>";
switch (type) {
case 1:
soapmsg += FetchAccount(param);
break;
case 2:
soapmsg += FetchContact(param);
break;
case 3:
soapmsg += GetUserId();
break;
case 4:
soapmsg += GetUserRoles();
break;
default:
alert("Fehler\n--------------------\nKein gültiger Nachrichtentyp!\n\nMethod: BuildSoapMsg)");
break;
}
soapmsg += "</soap:Body></soap:Envelope>"; // Der Nachricht wird geschlossen
return soapmsg;
}
// case 1
// Setzt die Abfrage für eine Firma zusammen
FetchAccount = function(accountid) {
var soapbody = "<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
"<fetchXml>" +
"<fetch mapping='logical'>" +
"<entity name='account'>" +
"<attribute name='name'/>" +
"<attribute name='address1_city'/>" +
"<attribute name='address1_line1'/>" +
"<attribute name='address1_line2'/>" +
"<attribute name='address1_postalcode'/>" +
"<attribute name='accountnumber'/>" +
"<attribute name='primarycontactid'/>" +
"<filter type='and'>" +
"<condition attribute='accountid' operator='eq' value='" + accountid + "'/>" +
"</filter>" +
"</entity>" +
"</fetch>" +
"</fetchXml>" +
"</Fetch>";
return soapbody;
}
// case 2
// Setzt die Abfrage für einen Kontakt zusammen
FetchContact = function(contactid) {
var soapbody = "<Fetch xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
"<fetchXml>" +
"<fetch mapping='logical'>" +
"<entity name='contact'>" +
"<attribute name='new_anrede'/>" +
"<attribute name='lastname'/>" +
"<attribute name='firstname'/>" +
"<filter type='and'>" +
"<condition attribute='contactid' operator='eq' value='" + contactid + "'/>" +
"</filter>" +
"</entity>" +
"</fetch>" +
"</fetchXml>" +
"</Fetch>";
return soapbody;
}
// Senden eines CRM-Statements zur Datenabfrage und Rückgabe des Ergebnisses
// Statement: Fetch = 1, Execute = 2, RetrieveMultiple = 3
SendSoap = function(soapmsg, stmt) {
var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
switch (stmt) {
case 1:
xHReq.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Fetch");
break;
case 2:
xHReq.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");
break;
case 3:
xHReq.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
break;
default:
alert("Fehler\n--------------------\nKein gültiges CRM Statement!\n\nMethod: SendSoap)");
break;
}
xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xHReq.setRequestHeader("Content-Length", soapmsg.length);
xHReq.send(soapmsg);
var xHResult = xHReq.responseXML;
var errorCount = xHResult.selectNodes('//error').length;
if (errorCount != 0) {
var msg = xHResult.selectSingleNode('//description').nodeTypedValue;
alert("Fehler\n--------------------\nWebService meldet folgenden Fehler:\n" + msg + "!\n\nMethod: SendSoap)");
return null;
} else {
var oXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
oXmlDoc.async = false;
if (stmt == 2 || stmt == 3) {
oXmlDoc.loadXML(xHResult.xml);
} else {
oXmlDoc.loadXML(xHResult.text);
}
return oXmlDoc
}
var nodeNumber = xHResult.selectNodes("./address1_city");
if (nodeNumber.length == 1) {
alert(nodeNumber[0].text);
}
else {
alert("Kein Inhalt");
}
}
30. August 2011 14:54