[SalesForce] Lightning component action responds with internal server error

I'm trying to create my first production lightning component and I'm running into an internal server error when trying to call an action from the apex controller of one of my components.

The premise is simple:
Component 1 is a text field where the user types a search string. When changed, the field fires a custom change event.
Component 2 receives the event, uses the content to generate parameters for a server side method. The server converts the parameters into a query and returns the results minus any exact matches. When a result is selected, an event is fired.
Component 1 responds to the new event setting the search string to be an exact match of the selected suggestion. This obviously re-fires the change event. Which is fine

The issue is that when trying to call the server method I get an internal server error. I can't seem to get the server to return more information about the error (which makes sense for these errors).

Component 2:

<aura:component controller="SearchSuggestionsController">
    <aura:attribute name="SObjectType" type="String" required="true"
                description="The object to run queries against" />
    <aura:attribute name="Field" type="String" required="true"
                description="The field to perform the search on"/>
    <aura:attribute name="ResultSize" type="Integer" default="5"
                description="The number of results that will be displayed"/>

    <aura:handler event="c:SearchFieldChanged"
              action="{!c.handleChangeEvent}"/>
</aura:component>

Component 2 js controller:

({
    handleChangeEvent : function(component, event, helper) {
        var searchString = event.getParam("value");
        var SObjectType = component.get('v.SObjectType');
        var field = component.get('v.Field');
        var resultSize = component.get('v.ResultSize');
        var action = component.get('c.getSearchSuggestions');
        var params = {
            "searchText" : searchString, 
            "SObjectType" : SObjectType, 
            "fieldName" : field, 
            "resultSize" : resultSize
        };

        console.log(params);

        action.setCallback(this, helper.showSuggestions);
        action.setParams(params);

        $A.enqueueAction(action);
    }
})

Component 2 Helper:

({
    showSuggestions : function(response) {
        var state = response.getState();

        if (state === "SUCCESS") {
            var result = response.getReturnValue();
            console.log(result);
        } else if (state === "ERROR" ) {
            var errors = response.getError();

            if (errors) {
                if (errors[0] && errors[0].message) {
                    console.log("Error Message: " + errors[0].message);
                }
            }
        }
    }
})

Apex Controller:

public class SearchSuggestionsController {
    @AuraEnabled
    public static List<String> getSearchSuggestions(
        String searchText, String SObjectType, String fieldName, Integer resultSize
    ) {
        Set<String> result = new Set<String>();

        searchText = String.escapeSingleQuotes(searchText);
        SObjectType = String.escapeSingleQuotes(SObjectType);
        fieldName = String.escapeSingleQuotes(fieldName);

        String query = 'SELECT ' + fieldName +
            ' FROM ' + SObjectType +
            ' WHERE ' + fieldName + ' LIKE \'%' + searchText + '%\'' +
            ' AND ' + fieldName + ' != \'' + searchText + '\'' +
            ' LIMIT ' + string.valueOf(resultSize);

        for (SObject so : Database.query(query)) {
            result.add(String.valueOf(so.get(fieldName)));
        }

        // Aura enabled methods cannot return sets
        return new List<String>(result);
    }
}

The error appears in the browser developer console.

I should also add that testing the apex method in execute anonymous shows that it works as expected so the error has to have something to do with how I'm setting params and or calling the action, I just don't know what it is.

Best Answer

I had a thought to check the apex logs in the dev console and there are quite a few errors related to type conversion when deserializing the JSON params.

so... Check the dev console seems to be the answer here.

UPDATE:

My Specific issue was the integer parameter. JSON deserialized it as a string. Changing the param to a string and handling type conversions myself resolved the issue.

Related Topic