locked
Restrict Access to Form from a time 3 days before the Date on Form RRS feed

  • Question

  • I have a form that contains a date called Date1.

    I want to restrict access to that form from 16:30pm 3 days before the Date1.

    Anyone know how to do this ?

    Wednesday, July 23, 2014 8:18 AM

Answers

  •   
    Hi JmcCon,

    I didn't test the below code but JavaScript should be something like below:

    You can use the below code to show alert and close the form.

    var date = Xrm.Page.getAttribute("Date field").getValue();

    var currentDate = new Date();
    var time = currentDate.getHours() + ":" + currentDate.getMinutes();

    if((currentDate>(date.getDate()-3)) && time>16.30)
    {
    // if using CRM 2013.
    Xrm.Utility.alertDialog("Sorry you are not allowed to view the form");

    // if using CRM 2011.
    alert("Sorry you are not allowed to view the form");

    Xrm.Page.ui.close();
    }

    If any unsaved information is found, it may prompts the user to save the changes prior to closing it.

    If you don't want to show the prompt to the user you need to set submitMode to "never"
    Below is the code snippet.
    var attributes = Xrm.Page.data.entity.attributes.get();

    for (var i in attributes) {

    attributes[i].setSubmitMode("never");

    }

    Xrm.Page.ui.close();

    Or you can directly use window.close() but it is unsupported.                                                                                                                                                                                                                                                                                                                                                                                   

    • Proposed as answer by Kalim Khan Wednesday, July 23, 2014 5:16 PM
    • Edited by Kalim Khan Wednesday, July 23, 2014 5:16 PM
    • Marked as answer by JMcCon Tuesday, August 5, 2014 8:26 AM
    Wednesday, July 23, 2014 5:16 PM
  • Hi,

    Try below condition,

    if((datef-currentDate)/(1000*60*60)<=3) && currentDate.getHours()>=16 && currentDate.getMinutes()>=30 && !(UserHasRole("Administrator"))

    HTH!

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful" 

    • Marked as answer by JMcCon Tuesday, August 5, 2014 8:26 AM
    Friday, July 25, 2014 5:26 PM

All replies

  • Hi,

    You can write a javascript code which will fire on load of the form and will check if the current date is greater than (Date1 - 3days) and current time is greater than 16.30 then show a pop message to user and close the form.

    HTH!

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"  



    • Edited by DynaDeveloper Wednesday, July 23, 2014 8:36 AM
    • Proposed as answer by Kalim Khan Wednesday, July 23, 2014 5:14 PM
    Wednesday, July 23, 2014 8:33 AM
  • Thanks for the reply.

    But would you have any code that would do this ?

    Wednesday, July 23, 2014 9:41 AM
  •   
    Hi JmcCon,

    I didn't test the below code but JavaScript should be something like below:

    You can use the below code to show alert and close the form.

    var date = Xrm.Page.getAttribute("Date field").getValue();

    var currentDate = new Date();
    var time = currentDate.getHours() + ":" + currentDate.getMinutes();

    if((currentDate>(date.getDate()-3)) && time>16.30)
    {
    // if using CRM 2013.
    Xrm.Utility.alertDialog("Sorry you are not allowed to view the form");

    // if using CRM 2011.
    alert("Sorry you are not allowed to view the form");

    Xrm.Page.ui.close();
    }

    If any unsaved information is found, it may prompts the user to save the changes prior to closing it.

    If you don't want to show the prompt to the user you need to set submitMode to "never"
    Below is the code snippet.
    var attributes = Xrm.Page.data.entity.attributes.get();

    for (var i in attributes) {

    attributes[i].setSubmitMode("never");

    }

    Xrm.Page.ui.close();

    Or you can directly use window.close() but it is unsupported.                                                                                                                                                                                                                                                                                                                                                                                   

    • Proposed as answer by Kalim Khan Wednesday, July 23, 2014 5:16 PM
    • Edited by Kalim Khan Wednesday, July 23, 2014 5:16 PM
    • Marked as answer by JMcCon Tuesday, August 5, 2014 8:26 AM
    Wednesday, July 23, 2014 5:16 PM
  • I have the following code created but it doesn't disable the form or any of the fields.

    Anyone know whats wrong?

    // formdisable 
    
    function DisableFormAfterDate() {
    
    var datef = Xrm.Page.getAttribute("meetingdate").getValue();
     
    var currentDate = new Date();
    var time = currentDate.getHours() + ":" + currentDate.getMinutes();
     
    if((currentDate>(datef.getDate()-3)) && time>16.30 && !(UserHasRole("Administrator")))
     {
     
     formdisable(disablestatus);
    
     }
    }
    
    
    function formdisable(disablestatus) 
    
    { 
    var allAttributes = Xrm.Page.data.entity.attributes.get();
    
    
     for (var i in allAttributes) { 
    
    	var myAttributes = Xrm.Page.data.entity.attributes.get(allAttributes[i].getName()); 
    	var myName = myattribute.getName();           
    	Xrm.Page.getControl(myname).setDisabled(disablestatus);  
    
    } 
    
    } 


    • Edited by JMcCon Friday, July 25, 2014 3:23 PM
    Friday, July 25, 2014 3:22 PM
  • Hi,

    Try below condition,

    if((datef-currentDate)/(1000*60*60)<=3) && currentDate.getHours()>=16 && currentDate.getMinutes()>=30 && !(UserHasRole("Administrator"))

    HTH!

    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful" 

    • Marked as answer by JMcCon Tuesday, August 5, 2014 8:26 AM
    Friday, July 25, 2014 5:26 PM