Answered by:
MS CRM, How can i set it by using javascript ?

Question
-
I have a field call Mediation Data is "Date and time" type....
How can I set it only allow to save between...8.00am to 6:00pm ? by using Javascript ?
Thank you
Friday, March 29, 2013 2:46 AM
Answers
-
Try this - remember to check the box to pass the execution context as the first parameter when binding to the form's OnSave event and change the field name accordingly.
function OnSave(executionObj) { var fieldDate = Xrm.Page.getAttribute("scheduledend").getValue(); if (fieldDate != null) { var fieldTime = fieldDate.getTime(); var year = fieldDate.getFullYear(); var month = fieldDate.getMonth() + 1; var day = fieldDate.getDate(); if (month < 10) month = "0" + month; if (day < 10) day = "0" + day; var startDate = month + "/" + day + "/" + year + " " + "8:00:00 AM"; var endDate = month + "/" + day + "/" + year + " " + "6:00:00 PM"; var startTime = new Date(startDate).getTime(); var endTime = new Date(endDate).getTime(); if (fieldTime < startTime || fieldTime > endTime) { alert("Invalid time"); executionObj.getEventArgs().preventDefault(); } } }
Jason Lattimer
My Blog - Follow me on Twitter - LinkedIn- Proposed as answer by JLattimerMVP, Moderator Friday, March 29, 2013 4:00 AM
- Edited by JLattimerMVP, Moderator Friday, March 29, 2013 4:00 AM
- Marked as answer by KennyChan6996 Tuesday, April 16, 2013 8:57 AM
Friday, March 29, 2013 4:00 AMModerator
All replies
-
Try this - remember to check the box to pass the execution context as the first parameter when binding to the form's OnSave event and change the field name accordingly.
function OnSave(executionObj) { var fieldDate = Xrm.Page.getAttribute("scheduledend").getValue(); if (fieldDate != null) { var fieldTime = fieldDate.getTime(); var year = fieldDate.getFullYear(); var month = fieldDate.getMonth() + 1; var day = fieldDate.getDate(); if (month < 10) month = "0" + month; if (day < 10) day = "0" + day; var startDate = month + "/" + day + "/" + year + " " + "8:00:00 AM"; var endDate = month + "/" + day + "/" + year + " " + "6:00:00 PM"; var startTime = new Date(startDate).getTime(); var endTime = new Date(endDate).getTime(); if (fieldTime < startTime || fieldTime > endTime) { alert("Invalid time"); executionObj.getEventArgs().preventDefault(); } } }
Jason Lattimer
My Blog - Follow me on Twitter - LinkedIn- Proposed as answer by JLattimerMVP, Moderator Friday, March 29, 2013 4:00 AM
- Edited by JLattimerMVP, Moderator Friday, March 29, 2013 4:00 AM
- Marked as answer by KennyChan6996 Tuesday, April 16, 2013 8:57 AM
Friday, March 29, 2013 4:00 AMModerator -
Jason's reply is right, but the code can be shortened (also to avoid errors by date&time representation):
function OnSave(executionObj) { var fieldDate = Xrm.Page.getAttribute("scheduledend").getValue(); if (fieldDate != null) { var hour = fieldDate.getHours(); if (hour < 8 || hour > 18) { alert("Invalid time"); executionObj.getEventArgs().preventDefault(); } } else { // stop to save also if the field is empty alert("Invalid time"); executionObj.getEventArgs().preventDefault(); } }
My blog: www.crmanswers.net
- Edited by Guido PreiteMVP Friday, March 29, 2013 8:35 AM
- Proposed as answer by ViN.k.S Tuesday, April 2, 2013 10:44 AM
Friday, March 29, 2013 8:14 AM