2009-08-20

Embeded notification

We were needed some kind of notify message for user without any pop-up or an alerts.


function CreateRequestObject() {
if (window.XMLHttpRequest) {
try {
return new XMLHttpRequest();
} catch (e) { }
} else if (window.ActiveXObject) {
try {
return new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
try {
return new ActiveXObject('Microsoft.XMLHTTP');
} catch (e) { }
}
}
return null;
}

function RetrieveEntity(sEntityName, GUID, sAttributeNames) {
var attributes = "";
if (sAttributeNames.length == 0)
return null;
for (var i = 0; i < sAttributeNames.length; i++) {
attributes += "" + sAttributeNames[i] + "";
}
var soapBody = "" +
"" +
"" + sEntityName + "" +
"" + GUID + "" +
"" +
"" + attributes + "" +
"
" +
"
" +
"
";
var soapXml = " "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
"xmlns:xsd='http://www.w3.org/2001/XMLSchema' " +
"xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>";
soapXml += GenerateAuthenticationHeader();
soapXml += soapBody;
soapXml += "
";

xmlhttp = CreateRequestObject();
xmlhttp.open("POST", "/mscrmservices/2007/crmservice.asmx", false);
xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Retrieve");
xmlhttp.setRequestHeader("Content-Length", soapXml.length);
xmlhttp.send(soapXml);

// Capture the result.
var resultXml = xmlhttp.responseXML;
// Check for errors.
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0) {
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert(msg);
return null;
}
// Display the retrieved value.
else {
var retEntityValues = new Array();
var retrieveNodes = resultXml.selectNodes("soap:Envelope/soap:Body/RetrieveResponse/RetrieveResult/q1:*");
for (var i = 0; i < retrieveNodes.length; i++) {
var nodeName = retrieveNodes[0].baseName;
var nodeValue = retrieveNodes[0].nodeTypedValue;
retEntityValues[nodeName] = nodeValue;
}
return retEntityValues
}
}

function AddNotification(noticationId, text) {
if (crmForm.all.Notifications.all[noticationId] != null)
crmForm.all.Notifications.all[noticationId + '_text'].innerHTML = text;
else {
crmForm.all.Notifications.innerHTML += '
' + text + '
';
crmForm.all.Notifications.style.display = 'block';
}
}

function RemoveNotification(noticationId) {
if (crmForm.all.Notifications.all[noticationId] != null) {
crmForm.all.Notifications.all[noticationId].removeNode(true);
if (crmForm.all.Notifications.childNodes.length == 0)
crmForm.all.Notifications.style.display = 'none';
}
}

var customer = crmForm.all.customerid;
if (customer.DataValue != null && customer.DataValue[0].typename == 'account') {
var cs = new Array();
cs[0] = 'new_attention';
var acc = RetrieveEntity('account', customer.DataValue[0].id, cs);
if (acc != null && acc['new_attention'] == 1) {
AddNotification('NotificationAccount', 'Клиент требует внимания! Для более подробной информации перейдите в карточку клиента.');
crmForm.all.customerid_d.getElementsByTagName('SPAN')[0].style.color = 'Red';
}
else {
RemoveNotification('NotificationAccount');
}
}
else {
RemoveNotification('NotificationAccount');
}

2 комментария:

Vlad комментирует...

Это моя разработка :P

hyper комментирует...

Ну, в общем, я её себе и не приписываю ;)
Публикую сюда как в базу знаний, чтобы оставалась легко доступной отовсюду.