Internal Salesforce.com Error — when I pass a Javascript value to Apex

Is there some Salesforce equivalent of https://jsfiddle.net/ ? Here’s the files involved. I’ve pared them down to a simple case.

TestApp.app
<aura:application >
    <c:cltTest />
</aura:application>

cltTest.cmp
<aura:component controller="svrTestController"
        implements="flexipage:availableForAllPageTypes">
    <aura:handler name="init" value="{!this}" action="{!c.initComponent}"/> 
</aura:component>

svrTestController.apxc
public with sharing class svrTestController {
    @AuraEnabled
    public static Integer doTest(String stringArgument, Integer integerArgument) {
        system.debug('stringArgument:');
        system.debug(stringArgument);
        system.debug('integerArgument:');
        system.debug(integerArgument);
        try {
            system.debug('stringArgument == \'cat\':');
            system.debug(stringArgument == 'cat');
            system.debug('integerArgument == 10:');  // line 11
            system.debug(integerArgument == 10);     // line 12
        } catch (Exception e) {
            system.debug('svrTestController caught exception.');
        } finally {
            system.debug('svrTestController is complete');
        }
        return 10;
        // If you don't return something, Salesforce thinks it's a constructor, 
        // and objects that constructors can't be static.
    }
}

cltTestController.js
({
    initComponent : function(cmp, event, helper) {
        helper.cltDoTest(cmp);
    }
})

cltTestHelper.js
({
    cltDoTest : function(cmp) {
        var action = cmp.get('c.doTest');
        action.setParams({
            stringArgument: 'cat', 
            integerArgument: 12
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === 'SUCCESS') {
                console.log('Succeeded');
            } else {
                console.log('Failed');
            }
        });
        $A.enqueueAction(action);
    }
})

When you run this, the log reads in part:

STATEMENT_EXECUTE      [11]|
USER_DEBUG             [11]|DEBUG|integerArgument == 10:
STATEMENT_EXECUTE      [12]|
FATAL_ERROR            Internal Salesforce.com Error

The helper calls the server-side controller with a Javascript number of 12. The server-side controller interprets it as an Apex number. And the mechanism blows.

(I’ve just realized that there are no integers on the Javascript side. They are all “numbers”, floating point quantities. There’s part of the problem.)

We suspect the answer is to tell the Apex side what to expect. Something involving <apex:attribute>?

Answer

You’ll want to go log a case. It appears that the internal state of the parameter is screwed up. Using parseInt in the helper does not fix the problem. Interestingly, if you use Integer.valueOf in your apex code, the problem goes away:

system.debug(Integer.valueOf(integerArgument) == 10);

Alternatively, use the Decimal type. Our limited ability to view the underlying data will probably make it impossible for us “outsiders” to fix the problem. I’ve suggested two workarounds here, but R&D really needs to know about this so they can issue a Known Issue and get it fixed.


Edit: Doing anything at all with the parameter that involves an operator causes the error. For example, I tried sanitizing by adding 0:

Integer integerArgument2 = integerArgument+0;

Same result; it crashed when I tried to add 0. You have to use Integer.valueOf before it starts working like a normal Integer should.

Attribution
Source : Link , Question Author : user2171796 , Answer Author : sfdcfox

Leave a Comment