7

Assume we have this code in the controller or helper to invoke a APEX method:

action = cmp.get("c.TestUwe");
action.setParams( { params :  ['777','888'] } );
action.setCallback( null, function(callbackResult) {
    if(callbackResult.getState()=='SUCCESS') {
        var result = callbackResult.getReturnValue();
        console.log('RESULT', result  );
    }
    if(callbackResult.getState()=='ERROR') {
        console.log('ERROR', callbackResult.getError() ); 
    }
});
$A.enqueueAction( action );

In Apex we have one input param of List<Object>:

@AuraEnabled public static Object TestUwe(List<Object> params) {
    system.debug( params );
    return null;
}   

All good: this works fine!

Now if I change the type to just Object

@AuraEnabled public static Object TestUwe(Object params) 

It does not work anymore:

An internal server error has occurred↵Error ID: 1556564141-71620 (-2109904195)↵↵org.auraframework.throwable.AuraExecutionException: apex://elfLC_WorkOrderLineItemEditor: java.lang.UnsupportedOperationException↵ at ...

Other example is: we keep List<Object> in APEX but provide this as params:

action.setParams( { params :  ['777','888', { test : 'abc' }] } );

An internal server error has occurred↵Error ID: 1556564141-71801 (119852647)↵↵org.auraframework.throwable.AuraExecutionException: apex://elfLC_WorkOrderLineItemEditor: common.apex.runtime.impl.ExecutionException: Salesforce System Error: 1556564141-71800 (-1664928234) (-1664928234)↵ at ...

So not all possible types works as expected.

I've read these chapters but can't find any documentation on which types are supported and which not. Where does Salesforce specify EXACTLY what is allowed and what not?

I found some posts where people serialize the parameters, e.g.:

Why is this necessary at all? Why can't the framework just do the serialization of anything on it's own and in APEX we just use the typeless Object and take care on proper handling from there dynamically?

But the main question is:

Where do I find the official documentation on which types are allowed and which are forbidden? It should be stated at a prominent place here in order to save us a lot of trial-and-error time.

  • Interested to see if you get an official answer on this. Nothing except string seems to work reliably, see also this: salesforce.stackexchange.com/questions/108355/… – Aidan Sep 7 '17 at 14:08
  • @Aidan missing docs on this would by very bad, because it's wasting a lot of developers time for nothing... I always think: it's me not reading the right place.... – Uwe Heim Sep 7 '17 at 14:18
  • I'd assumed that they meant the parameter types to work the same way as the return types, which they list here: developer.salesforce.com/docs/atlas.en-us.lightning.meta/… But, as in by previous link, Integers don't work correctly. And I've found that collections of collections don't work right as a return value. – Aidan Sep 7 '17 at 14:24
  • I cant check it now but will propose to try for: { params : ['777','888'] } - Map<String, List<string>> – patryk Oct 4 '17 at 11:28
2

From my experience, the best practice for passing parameter from Lightning component to Apex class is to use JSON.stringify() on anything that is more complicated than single string (every list, map, object, map of lists, ...). You can then deserialize/parse it in apex, which is safer then managing it in javascript.

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.