if you're statistics of your opportunities that counting status duration can be very useful for you.
Status Duration - this is how many days opportunity is the current status.
Add new field on opportunity form "new_statusduration" to set Status Duration value.
function getDuration() {
var thetime = new Date();
var oneday = 1000 * 60 * 60 * 24;
var currentMonth = thetime.getMonth() + 1;
var currentYear = thetime.getFullYear();
var currentDay = thetime.getDate();
var currentdate = currentMonth + "/" + currentDay + "/" + currentYear;
var newdate = new Date(currentdate);
if (Xrm.Page.getAttribute("opportunityratingcode").getValue() == null) {
Xrm.Page.getAttribute("new_statusduration").setValue(null);
}
else {
var statusduration = "";
var xml = "";
xml += '';
xml += '';
xml += '';
xml += '';
xml += '';
xml += '';
xml += '';
xml += ''
xml += ' ';
xml += ' ';
xml += ' ';
var doc = Fetch(xml);
var nodes = doc.selectNodes("//resultset/result/createdon");
if (nodes[0] != null) {
var statusnode = nodes[0].getAttribute("date");
var statusdate = new Date(statusnode);
var duration = Math.round((newdate - statusdate) / oneday);
if (duration < 1) {
statusduration = duration + " d";
}
else {
statusduration = Math.floor(duration / 30) + " m " + (duration % 30) + " d";
}
Xrm.Page.getAttribute("new_statusduration").setSubmitMode("always");
Xrm.Page.getAttribute("new_statusduration").setValue(statusduration);
}
}
}
To use this script you need "Fetch" function
function Fetch(xml) {
var Xml = "";
Xml += GenerateAuthenticationHeader();
Xml += "";
Xml += "";
Xml += "";
Xml += CrmEncodeDecode.CrmXmlEncode(xml); // Microsoft _HtmlEncode function
Xml += " ";
Xml += " ";
Xml += " ";
Xml += " ";
var XmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); // Microsot CreateXmlHttp function
XmlHttp.open("POST", "/mscrmservices/2007/crmservice.asmx", false); //Sync Request
XmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
XmlHttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Fetch");
XmlHttp.send(Xml);
var XmlDoc = new ActiveXObject("Msxml2.DOMDocument");
XmlDoc.async = false;
XmlDoc.resolveExternals = false;
XmlDoc.loadXML(XmlHttp.responseXML.text);
return XmlDoc;
}
Result:
Don't forget enable auditing for Opportunity entity!

No comments:
Post a Comment