Showing posts with label form. Show all posts
Showing posts with label form. Show all posts

Monday, November 14, 2011

Status Duration


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!

Tuesday, November 8, 2011

Don't allow complete appointment unless "Status" is empty

You can don't allow close appointment as completed or cancelled unless user don't specify necessary field. 
Use following script:
function closeAppointment()
{
 var CRM_FORM_SAVE_MODE_SAVEASCOMPLETED = 58;
 var CRM_FORM_SAVE_MODE_CLOSE = 5;
 if ((event.Mode == CRM_FORM_SAVE_MODE_SAVEASCOMPLETED) ||
    (event.Mode == CRM_FORM_SAVE_MODE_CLOSE))
 {
  var qualifiedstatus = Xrm.Page.getAttribute("new_qualifiedstatus");
  if (!IsNull(qualifiedstatus))
  {
   var option = qualifiedstatus.getSelectedOption();
   if (IsNull(option) || IsNull(option.value) || IsNull(option.text))
   {
    alert("Please provide a value for the Qualified Status!");
    document.all.new_qualifiedstatus.focus();
    event.returnValue = false;
    return false;
   }
  }
 }
}

Hide control "Est. Revenue" on Opportunity form

If you want to hide "Est. Revenue" field for all except special CRM role and owner of this opportunity you can use following javascript code:
function GetCurrentUserRoles()
{
 var xml = "" +
"" +
"" +
GenerateAuthenticationHeader() +
" " +
" " +
" " +
" role" +
" " +
" " +
" name" +
" " +
" " +
" false" +
" " +
" " +
" roleid" +
" role" +
" systemuserroles" +
" roleid" +
" Inner" +
" " +
" " +
" systemuserid" +
" systemuserroles" +
" systemuser" +
" systemuserid" +
" Inner" +
" " +
" 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;
 return (resultXml);
}


function hideRevenue()
{
 UserID = Xrm.Page.context.getUserId();
 OwnerID = Xrm.Page.getAttribute("ownerid").getValue()[0].id;
 if (UserHasRole("Boss") || UserID == OwnerID)
 {
  Xrm.Page.ui.controls.get("estimatedvalue").setVisible(true);
 }
 else
 {
  Xrm.Page.ui.controls.get("estimatedvalue").setVisible(false);
 }
}