简体   繁体   中英

Dynamics CRM 2011: javascript, 'null' is null not an object. error

I am using JavaScript on Dynamics CRM 2011. I am trying to create a new field by comparing two date fields. however, I think something is wrong with null value. cas error message shows up: 'null' is null not an object.

The following is the script. Please have a look, and let me know what you think.

function m1status() {

    var m1date = Xrm.Page.getAttribute("new_m1date").getValue()

    var today = Xrm.Page.getAttribute("new_todayis").getValue()

    if (m1date == null) {

        Xrm.Page.getAttribute("new_m1status").setValue('Not Booked');}

    else if (m1date.getTime() >= today.getTime()) {

        Xrm.Page.getAttribute("new_m1status").setValue('Booked');}

    else {

        Xrm.Page.getAttribute("new_m1status").setValue('Completed');}

    //Set the Submit Mode
    Xrm.Page.getAttribute("new_m1status").setSubmitMode("always");

}

Also most likely the default value is null. cas nothing is shown up.

Thanks so much.

There are some typos in your code, however you can rewrite your code in this way and fill the missing actions.

function m1status() {

    var m1date = Xrm.Page.getAttribute("new_m1date").getValue();
    var today = Xrm.Page.getAttribute("new_todayis").getValue();

    // you can have 4 cases
    // 1: both fields are null
    if (m1date == null && today == null) {
        Xrm.Page.getAttribute("new_m1status").setValue('Not Booked');
    }
    // 2: both fields are not null
    if (m1date != null && today != null) {

        if (m1date.getTime() >= today.getTime()) {
            Xrm.Page.getAttribute("new_m1status").setValue('Booked');
        }
        else {
            Xrm.Page.getAttribute("new_m1status").setValue('Completed');
        }
    }
    // 3: first field is not null, second field is null
    if (m1date != null && today == null) {
        // what need to do in this case?
    }
    // 4: first field is null, second field is not null
    if (m1date == null && today != null) {
        // what need to do in this case?
    }

    Xrm.Page.getAttribute("new_m1status").setSubmitMode("always");
}

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