Retrieving and Comparing Dates from Appointments
-
Thursday, May 31, 2012 3:53 PM
CRM 4.0 Rollup 11
Requirement: Find the date of the appointment that is scheduled (not completed) and is >= today and closest to today's date. I have several issues going on... (this is to place a date in a field that is the next scheduled appointment. If several are scheduled, and the next gets completed, I need the code to go check for the next date an appointment is scheduled and update the next visit date field.
The code below is to run on the OnSave event of an appointment.
I have the SAOP message below that brings me back my dates.
Question 1: How can I only bring back the dates of scheduled appointments only?
I found on another thread the function listed below to convert the String date into something I use, but I get an unsupported method error.
Question 2: Any idea what's wrong with the function?
Once I get all that done, I'll need to compare all the dates to find out which one is >= to today and closest to today. I'm thinking about going through the array and and comparing each one but I don't know...
Question 3: How can I find the date that is <= to today and closer to today than the others?
Any help on any of these questions would be great!
Code:
function StringToDateTimeConvert(source) { var source1 = source.split('T')[0]; var parts = source1.split('-'); var dat = new Date(); dat.setYear(parts[0]); dat.setMonth(parts[1] - 1); dat.setDate(parts[2]); return dat; } if(event.Mode ==58) { var compid = crmForm.all.regardingobjectid.DataValue[0].id;//company I want products for var authenticationHeader = GenerateAuthenticationHeader(); // Prepare the SOAP message. var xml = "<?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'>"+ authenticationHeader+ "<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>appointment</q1:EntityName>"+ "<q1:ColumnSet xsi:type='q1:ColumnSet'>"+ "<q1:Attributes>"+ "<q1:Attribute>scheduledstart</q1:Attribute>"+ "<q1:Attribute>activityid</q1:Attribute>"+ "</q1:Attributes>"+ "</q1:ColumnSet>"+ "<q1:Distinct>false</q1:Distinct>"+ "<q1:Criteria>"+ "<q1:FilterOperator>And</q1:FilterOperator>"+ "<q1:Conditions>"+ "<q1:Condition>"+ "<q1:AttributeName>regardingobjectid</q1:AttributeName>"+ "<q1:Operator>Like</q1:Operator>"+ "<q1:Values>"+ "<q1:Value xsi:type='xsd:string'>"+compid+"</q1:Value>"+ "</q1:Values>"+ "</q1:Condition>"+ "</q1:Conditions>"+ "</q1:Criteria>"+ "</query>"+ "</RetrieveMultiple>"+ "</soap:Body>"+ "</soap:Envelope>"; // Prepare the xmlHttpObject and send the request. var xHReq = new ActiveXObject("Msxml2.XMLHTTP"); xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false); xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple"); xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); xHReq.setRequestHeader("Content-Length", xml.length); xHReq.send(xml); // Capture the result. var resultXml = xHReq.responseXML; // Check for errors. var errorCount = resultXml.selectNodes('//error').length; if (errorCount != 0) { var msg = resultXml.selectSingleNode('//description').nodeTypedValue; alert(msg); }//end of error check else { var results = resultXml.getElementsByTagName('BusinessEntity'); var dates = new Array(); for (var i =0; i < results.length; i++) { dates[i] = StringToDateTimeConvert(results[i]); alert(dates[i]); } } }
All Replies
-
Thursday, June 07, 2012 7:21 PM
In case anyone was looking to do this, below is what I came up with:
//Function to get Time Object so I can compare and sort function StringToDateTimeConvert(source) { var source1 = source.split('T'); var source2 = source1[0]; var parts = source2.split('-'); var dat = new Date(); dat.setYear(parts[0]); dat.setMonth(parts[1] - 1); dat.setDate(parts[2]); return dat; } //Function to get date that I can pass via SOAP to other entity function parseTime(timeString) { if (timeString != '') { var m = timeString.getMonth() + 1;//Jan is 0 var d = timeString.getDate(); var y = timeString.getFullYear(); var datestring = y + '-' +m + '-' +d; return datestring; } //if null, give back null return ''; } /*********************************************** **************GetInfoSOAP Function************** ************************************************/ function GetInfoSOAP () { // Is the Company At Risk? var compid = crmForm.all.snc_productid.DataValue[0].id;//Product I want to check for At Risk var authenticationHeader = GenerateAuthenticationHeader(); // Prepare the SOAP message. var xml = "<?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'>"+ authenticationHeader+ "<soap:Body>"+ "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+ "<entityName>snc_product</entityName>"+ "<id>"+compid+"</id>"+ "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>"+ "<q1:Attributes>"+ "<q1:Attribute>snc_atrisk</q1:Attribute>"+ "</q1:Attributes>"+ "</columnSet>"+ "</Retrieve>"+ "</soap:Body>"+ "</soap:Envelope>"; // Prepare the xmlHttpObject and send the request. var xHReq = new ActiveXObject("Msxml2.XMLHTTP"); xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false); xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Retrieve"); xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); xHReq.setRequestHeader("Content-Length", xml.length); xHReq.send(xml); // Capture the result. var resultXml = xHReq.responseXML; // Check for errors. var errorCount = resultXml.selectNodes('//error').length; if (errorCount != 0) { var msg = resultXml.selectSingleNode('//description').nodeTypedValue; alert(msg); }//close if errors else try { //alert(resultXml.selectSingleNode("//q1:" +attribute).nodeTypedValue); if(resultXml.selectSingleNode("//q1:snc_atrisk").nodeTypedValue != null) { return (true);//the product is at risk } } catch (err) { return (false); } } /********End of GetInfoSOAP Function************/ /*********************************************** ************SOAP function to update************* ********* the NextVisitDate on Product********** ************************************************/ function UpdateNextVisitDate(date) { var ProductId = crmForm.all.snc_productid.DataValue[0].id; var authenticationHeader = GenerateAuthenticationHeader(); // Prepare the SOAP message. var xml = "<?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'>"+ authenticationHeader+ "<soap:Body>"+ "<Update xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+ "<entity xsi:type='snc_product'>"+ "<snc_atrisknextvisitdate>"+date+"</snc_atrisknextvisitdate>"+ "<snc_productid>"+ProductId+"</snc_productid>"+ "</entity>"+ "</Update>"+ "</soap:Body>"+ "</soap:Envelope>"; // Prepare the xmlHttpObject and send the request. var xHReq = new ActiveXObject("Msxml2.XMLHTTP"); xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false); xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Update"); xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); xHReq.setRequestHeader("Content-Length", xml.length); xHReq.send(xml); // Capture the result var resultXml = xHReq.responseXML; // Check for errors. var errorCount = resultXml.selectNodes('//error').length; if (errorCount != 0) { var msg = resultXml.selectSingleNode('//description').nodeTypedValue; alert(msg); } } /****End of UPdate NextVisitDate**************************/ /********Function calls*************************/ //Save as Completed && COmpany is At Risk try { if(event.Mode ==58 && GetInfoSOAP()) { // Prepare variables to retrieve all the dates from all Scheduled Appt var compid = crmForm.all.regardingobjectid.DataValue[0].id;//Company to get Appt from var authenticationHeader = GenerateAuthenticationHeader(); // Prepare the SOAP message. var xml = "<?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'>"+ authenticationHeader+ "<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>appointment</q1:EntityName>"+ "<q1:ColumnSet xsi:type='q1:ColumnSet'>"+ "<q1:Attributes>"+ "<q1:Attribute>scheduledstart</q1:Attribute>"+ "<q1:Attribute>activityid</q1:Attribute>"+ "</q1:Attributes>"+ "</q1:ColumnSet>"+ "<q1:Distinct>false</q1:Distinct>"+ "<q1:Criteria>"+ "<q1:FilterOperator>And</q1:FilterOperator>"+ "<q1:Conditions>"+ "<q1:Condition>"+ "<q1:AttributeName>regardingobjectid</q1:AttributeName>"+ "<q1:Operator>Equal</q1:Operator>"+ "<q1:Values>"+ "<q1:Value xsi:type='xsd:string'>"+compid+"</q1:Value>"+ "</q1:Values>"+ "</q1:Condition>"+ "<q1:Condition>"+ "<q1:AttributeName>statecodename</q1:AttributeName>"+ "<q1:Operator>Equal</q1:Operator>"+ "<q1:Values>"+ "<q1:Value xsi:type='xsd:string'>Scheduled</q1:Value>"+ "</q1:Values>"+ "</q1:Condition>"+ "</q1:Conditions>"+ "</q1:Criteria>"+ "</query>"+ "</RetrieveMultiple>"+ "</soap:Body>"+ "</soap:Envelope>"; // Prepare the xmlHttpObject and send the request. var xHReq = new ActiveXObject("Msxml2.XMLHTTP"); xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false); xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple"); xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); xHReq.setRequestHeader("Content-Length", xml.length); xHReq.send(xml); // Capture the result. var resultXml = xHReq.responseXML; // Check for errors. var errorCount = resultXml.selectNodes('//error').length; if (errorCount != 0) { var msg = resultXml.selectSingleNode('//description').nodeTypedValue; alert(msg); }//end of error check else { var results = resultXml.getElementsByTagName('BusinessEntity'); var dates = new Array();//put resluts in an array var today = new Date();//used to compare var NextVisit = '';//declaring only var flag = false;//declaring only for (var i =0; i < results.length; i++) {//change all dates to date object, unless it's the appt completed right now //then set that date to 1/1/2010 if (results[i].selectSingleNode('./q1:activityid').nodeTypedValue != crmForm.ObjectId) { dates[i] = StringToDateTimeConvert(results[i].selectSingleNode('./q1:scheduledstart').nodeTypedValue); } else {//set to a date that is out of range dates[i] = StringToDateTimeConvert('2010-01-01T01:00:00-02:00'); } }//sort the dates earliest to latest dates.sort(function(a,b){return a-b}); //find the first in the list that is equal or greater than today //use the flag to stop the loop when that date is found for(var i=0; i< dates.length && flag == false; i++) { if(dates[i]>=today) { NextVisit = dates[i]; flag = true; } }//close for loop //update the product with the next visit date (date or null) UpdateNextVisitDate(parseTime(NextVisit)); }//Close else statement }//end of if }//end if product is not null catch (err) { }- Marked As Answer by lifeIsPunny Thursday, June 07, 2012 7:21 PM