Search code examples
javascriptdynamics-crm

CRM 2016 not liking Javascript using if-else


Can anyone tell me what CRM would hate about this onload function I've created?

It's telling me Form_OnLoad is not defined. Looks defined to me. It's enabled in my form onload, published, etc.

Thank you.

function Form_OnLoad() {
    //Calculates total commission for AE1

    // Products + Services
    if (Xrm.Page.getAttribute("new_commissionproductae1").getValue() !== null) && (Xrm.Page.getAttribute("new_commissionserviceae1").getValue() !== null) && (Xrm.Page.getAttribute("new_commissionseligible").getValue() == "Yes") {
        var comm1 = (Xrm.Page.getAttribute("new_commissionproductae1").getValue() + Xrm.Page.getAttribute("new_commissionserviceae1").getValue());
        Xrm.Page.getAttribute("new_commissiontotalae1").setValue(comm1);   
    } else if {
    // Products only
    (Xrm.Page.getAttribute("new_commissionproductae1").getValue() !== null) && (Xrm.Page.getAttribute("new_commissionseligible").getValue() == "Yes") {
        Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionproductae1").getValue());  
    } else if {
    // Services only
    (Xrm.Page.getAttribute("new_commissionserviceae1").getValue() !== null) && (Xrm.Page.getAttribute("new_commissionseligible").getValue() == "Yes") {
        Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionserviceae1").getValue());  
    } else {
    // Net Sales 
    (Xrm.Page.getAttribute("new_commissionnetae1").getValue() !== null) && (Xrm.Page.getAttribute("new_commissionseligible").getValue() == "Yes") {
        Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionserviceae1").getValue());  
    }
}

Solution

  • I believe that's because your JavaScript is not correct. Try to use following code instead of yours:

    function Form_OnLoad() { //Calculates total commission for AE1
    
        // Products + Services
        if (Xrm.Page.getAttribute("new_commissionproductae1").getValue() !== null && 
            Xrm.Page.getAttribute("new_commissionserviceae1").getValue() !== null && 
            Xrm.Page.getAttribute("new_commissionseligible").getValue() === "Yes") {
            var comm1 = Xrm.Page.getAttribute("new_commissionproductae1").getValue() + Xrm.Page.getAttribute("new_commissionserviceae1").getValue();
            Xrm.Page.getAttribute("new_commissiontotalae1").setValue(comm1);   
        } else if (Xrm.Page.getAttribute("new_commissionproductae1").getValue() !== null && 
            Xrm.Page.getAttribute("new_commissionseligible").getValue() === "Yes") {
            Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionproductae1").getValue());  
        } else if (Xrm.Page.getAttribute("new_commissionserviceae1").getValue() !== null &&
            Xrm.Page.getAttribute("new_commissionseligible").getValue() === "Yes") {
            Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionserviceae1").getValue());  
        } else if (Xrm.Page.getAttribute("new_commissionnetae1").getValue() !== null &&
            Xrm.Page.getAttribute("new_commissionseligible").getValue() === "Yes") {
            Xrm.Page.getAttribute("new_commissiontotalae1").setValue(Xrm.Page.getAttribute("new_commissionserviceae1").getValue());  
        }
    }