简体   繁体   中英

crm 2011 javascript set date field to null

How would I clear a date field?

The following code doesn't seem to work:

    if (Xrm.Page.getAttribute("new_boolfield1").getValue() != null) {

       switch(Xrm.Page.getAttribute("new_boolfield1").getValue()) {

        case false:

          Xrm.Page.getAttribute("new_datefield1").setValue(null); // this doesn't work
          Xrm.Page.getAttribute("new_datefield1").setValue(); // this doesn't work either

        case true:

          Xrm.Page.getAttribute("new_datefield1").setValue(new Date()); 
        }
     }
Xrm.Page.getAttribute('new_datefield1').setValue();

Should work. Make sure that you're not setting your date field anywhere else and make sure to put break statements in

http://crmbusiness.wordpress.com/2012/05/17/crm-2011-javascript-to-set-the-current-date-and-time/

** EDIT ** It's your case statement

var b = false;
switch(b) {
  case false:
    console.log("false");
  case true:
    console.log("true");
}

Without a break statement this will print "false true"

var b = false;
switch(b) {
  case false:
    console.log("false");
    break;
  case true:
    console.log("true");
    break; // <- not really required but good practise
}

The right way to clear a field in CRM 2011 is to set its value to null

Xrm.Page.getAttribute("new_datefield1").setValue(null);

If your field still not cleared, can be for two reasons:

  1. you are setting the date somewhere else (as jasonscript suggested)

  2. your new_boolfield1 is not a boolean field ( two options inside CRM) but one optionset

You can easily test these conditions, the first check outside the switch, the second with an alert or a console.log

null is false so this is much cleaner

var xrmPage = Xrm.Page;
var date1   = xrmPage.getAttribute("new_datefield1");
var bValid  = xrmPage.getAttribute("new_boolfield1").getValue();
date1.setValue(bValid ? new Date() : null);

Voted up the answer above since it will also correct your code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM