[SalesForce] Status 500, Internal server error when calling Apex from LWC

I'm getting a status 500, internal error when calling an Apex function from my LWC. I'm able to provoke the error whenever the function, which I'm calling, is passes a parameter.

The following code WORKS:

// MyApexClass.cls //
public with sharing class MyApexClass {

    @AuraEnabled
    public static String myCoolFunction() {
        System.debug('I am working');

        // Do stuff
        return 'my cool string';
    }
}
// myLWC.js
import { LightningElement, api, wire } from 'lwc';
import myCoolFunction from '@salesforce/apex/MyApexClass.myCoolFunction';

export default class MyLWComponent extends LightningElement {

    clickHandlerFunction() {
        myCoolFunction(); // DOES NOT return an error.
    }
}

As mentioned, the above code works, but the second I try and parse a paramater (tried with integer and string) to myCoolFunction, I get an error. The following code doesn't work.

// MyApexClass.cls //
public with sharing class MyApexClass {

    @AuraEnabled
    public static String myCoolFunction(String myParam) {
        System.debug('I am working: ' + myParam);

        // Do stuff
        return 'my cool string';
    }
}
// myLWC.js
import { LightningElement, api, wire } from 'lwc';
import myCoolFunction from '@salesforce/apex/MyApexClass.myCoolFunction';

export default class MyLWComponent extends LightningElement {

    clickHandlerFunction() {
        myCoolFunction('param'); // DOES return an error.
    }
}

The error is as follows:

{
    status: 500,
    ok: false,
    headers: {},
    body: {
        stack: "stack: "Error: An internal server error has occurred
        Error ID: 881448938-2861492 (-1335892898)    
        at V.B.ik (https://static.lightning.force.com/cs84/auraFW/javascript/kHqYrsGCjDhXliyGcYtIfA/aura_prod.js:639:433)
        at Object.onXHRReceived (https://a-m--sanddev9.lightning.force.com/libraries/instrumentation/idleDetector/idleDetector.js:2:276)    
        at Object.iC.nl (https://static.lightning.force.com/cs84/auraFW/javascript/kHqYrsGCjDhXliyGcYtIfA/aura_prod.js:287:255)    
        at eval (https://a-m--sanddev9.lightning.force.com/libraries/one/s1MetricsServicePlugins/requestIdTransport.js:4:133)↵    at Object.iC.nl (https://static.lightning.force.com/cs84/auraFW/javascript/kHqYrsGCjDhXliyGcYtIfA/aura_prod.js:287:255)
        at tI.rr (https://static.lightning.force.com/cs84/auraFW/javascript/kHqYrsGCjDhXliyGcYtIfA/aura_prod.js:915:467)        
        at Object.iC.nl (https://static.lightning.force.com/cs84/auraFW/javascript/kHqYrsGCjDhXliyGcYtIfA/aura_prod.js:287:255)
        at iC.start (https://static.lightning.force.com/cs84/auraFW/javascript/kHqYrsGCjDhXliyGcYtIfA/aura_prod.js:286:313)
        at V.B.sr (https://static.lightning.force.com/cs84/auraFW/javascript/kHqYrsGCjDhXliyGcYtIfA/aura_prod.js:638:161)
        at XMLHttpRequest.e (https://static.lightning.force.com/cs84/auraFW/javascript/kHqYrsGCjDhXliyGcYtIfA/aura_prod.js:629:103)",

        message: "An internal server error has occurred↵Error ID: 881448938-2861492 (-1335892898)"
    }
}

I initially thought that it was because I was developing from the local dev server, but the I get the same error when I deploy the LWC and Apex class to the actual sandbox and test from there…

Any help would be greatly appreciated since it feels like it isn't my code which is breaking…

Best Answer

You are suppose to call the method like

   myCoolFunction({myParam:'param'}).then(result=>{
            console.log(JSON.stringify(result));
        }).catch(error=>{
            console.log(error);
        })
    }

with the name of parameter not just the value

Related Topic