2

I am getting an invalid conversion error when trying to access an element from an array but don't understand why..

Class

public with sharing class WspController {

    @AuraEnabled
    public static WSP_Politic__c saveWSP (Integer[] answers)
        System.debug('answers--->'+answers[1]); //Error
    }
}

Controller

score: function(component, event, helper) {
    var answerArr = [1, 2, 1, 3, 5, 2, -1, -3, 6];
    component.set("v.answers", answerArr);
}
scoreSet: function(component, event, helper) {
    var action = component.get("c.saveWSP");
    action.setParams({
        "answers": component.get("v.answers")
    });
    action.setCallback(this, function(response) {
        var state = response.getState();
        console.log('State-->' + state);
    });
    $A.enqueueAction(action);
}

Component

  <aura:component access="global" controller="WspController">
    <aura:attribute name="answers" type="Integer[]" />
  </aura:component>
4
  • What have you defined the "answers" component as? I wonder if it's because you have negative numbers in your array? (I thought integers by default would be signed, but perhaps they are unsigned by default and if negative, become decimals) Aug 27 '17 at 22:18
  • I'm investigating Aug 27 '17 at 22:18
  • Updated the code..
    – Brav
    Aug 27 '17 at 22:24
  • There is no "integer" type in Javascript, just Number, which is analogous to double in Apex. I wonder if the framework doesn't allow the conversion because it would be similar to an implicit conversion from double to integer, which is illegal in Apex. Aug 27 '17 at 23:55
6

Ok, I've tested.

If you change your method signature to

public static WSP_Politic__c saveWSP (Decimal[] answers)

It will fix the error (which I assumed you knew). However, I was unable to get it to change by removing negative values.

It looks like it sends all numbers as Decimals, even if you specified the aura:component as an Integer.

Your other option if you really really want it to be an integer is either serialize the dataload as a string and JSON decode it or typecast it inside your Apex method.

There is no real downside to it being a Decimal in Apex, however, so you may as well just change the method signature.

4
  • To be pedantic, there is a downsize to using Decimal, that being it uses 36 bytes of memory per value compared to Integer's 4. However, you're right that it doesn't matter in most trivial applications you'd use Deciml for, but it is something to be aware of if you're doing something crazy like uploading a binary file in Decimal format.
    – sfdcfox
    Aug 27 '17 at 23:27
  • Wow that is a lot more memory. I'll keep that in mind if I ever run into heap issues Aug 27 '17 at 23:30
  • 1
    Yeah, Decimals can hold very large and very small numbers, so they should only be used in small doses, like when you need to calculate taxes accurately and stuff. Try not to keep large lists of them in memory any more than necessary.
    – sfdcfox
    Aug 27 '17 at 23:37
  • Thanks for the help. Decimals worked.. I did consider putting decimals but hadn't tried it as using Integer made sense to me..
    – Brav
    Aug 28 '17 at 9:25
4

The aura framework is sending numbers as decimals at runtime but your Apex method is assuming those are integers.

It can be corrected by updating the signature and then working with the Decimal type, or you can convert to Integer if that's what's necessary.

public with sharing class WspController {

    @AuraEnabled
    public static WSP_Politic__c saveWSP (Decimal[] answers)
        System.debug('answers--->'+answers[1]); //Error
    }

    /*
    @AuraEnabled
    public static WSP_Politic__c saveWSP (Decimal[] answers)
        System.debug('answers--->' + answers[1].intValue()));
    }
    */
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.