locked
CRM 2011 - Javascript adding more than 2 fields together RRS feed

  • Question

  • Hi,

    I'm having some trouble trying to add more than 2 values together on the same entity form using jscript. I'm wanting to add 7 decimal fields together and put them into a totalvalue field ideally adding onchange events. I can get it to work with 2 values but whenever I try any more the javascript doesn't fire. I've listed by code below, any assistance would be greatly appreciated.

    function calculatetotalsavings()
    {
    var val1 = Xrm.Page.getAttribute("value1").getValue();
    var val2 = Xrm.Page.getAttribute("value2").getValue();
    var val3 = Xrm.Page.getAttribute("value3").getValue();
    var val4 = Xrm.Page.getAttribute("value4").getValue();
    var val5 = Xrm.Page.getAttribute("value5").getValue();
    var val6 = Xrm.Page.getAttribute("value6").getValue();
    var val7 = Xrm.Page.getAttribute("value7").getValue();

    if(val1==null)return;
    if(val2==null)return;
    if(val3==null)return;
    if(val4==null)return;
    if(val5==null)return;
    if(val6==null)return;
    if(val7==null)return;

    var result = val1 + val2 + val3 + val4 + val5 + val6 + val7;
    Xrm.Page.getAttribute("total value").setValue(result);
    }

    Regards


    Paul

    Thursday, May 8, 2014 1:27 PM

Answers

  • might be one of the variable is null, so its hitting the return statement and doing nothing. 

    For testing replace 'return' with alert for each value and see if its alerting with all.

    e.g. 

    if(val1==null)alert(val1); 
    if(val2==null)alert(val2);
    if(val3==null)alert(val3);
    if(val4==null)alert(val4);
    if(val5==null)alert(val5);
    if(val6==null)alert(val6);
    if(val7==null)alert(val5);



    • Edited by jattscorpion Thursday, May 8, 2014 1:36 PM
    • Proposed as answer by Guido PreiteMVP Thursday, May 8, 2014 1:55 PM
    • Marked as answer by samrr1875 Thursday, May 8, 2014 3:40 PM
    Thursday, May 8, 2014 1:32 PM

All replies

  • might be one of the variable is null, so its hitting the return statement and doing nothing. 

    For testing replace 'return' with alert for each value and see if its alerting with all.

    e.g. 

    if(val1==null)alert(val1); 
    if(val2==null)alert(val2);
    if(val3==null)alert(val3);
    if(val4==null)alert(val4);
    if(val5==null)alert(val5);
    if(val6==null)alert(val6);
    if(val7==null)alert(val5);



    • Edited by jattscorpion Thursday, May 8, 2014 1:36 PM
    • Proposed as answer by Guido PreiteMVP Thursday, May 8, 2014 1:55 PM
    • Marked as answer by samrr1875 Thursday, May 8, 2014 3:40 PM
    Thursday, May 8, 2014 1:32 PM
  • Try this and make sure the total field should be Decimal Type 

    If it works Please Mark it Answered to Me 

    function OnChange() {
        var AllAttributes = ['value1',
                            'value2',
                            'value3',
                            'value4',
                            'value5',
                            'value6',
                            'value7' ];
    
        for (var fields in AllAttributes) {
            var result = Xrm.Page.ui.controls.get(AllAttributes[fields]).getValue();
            resul += result;
        }
        Xrm.Page.getAttribute("total_value").setValue(result);
    }

    Thanks


    SRKD0808

    Thursday, May 8, 2014 2:11 PM