-2

I Have a lightning component from which I pass data to an apex class. I return the object. Why don't I get an update on my object neither a response in my lightning component.

Lightning

updatePicklist : function(component,event,helper){ 
    var selectedValues = $('[id$=picklist]').select2("val");
    var field = component.get("v.fieldName");
    var object = component.get("v.objectName");
    var getCurrentRecord = component.get("v.recordId"); 
    var action = component.get("c.updateRecord");
    action.setParams({
        "objId": getCurrentRecord,
        "objNew": object,
        "selection" : selectedValues,
        "fieldName" : field
    });
    action.setCallback(this, function(response) {
        if (response.getState() == "SUCCESS") {
            alert('record Updated Successfully-->' + JSON.stringify(response.getReturnValue()));   
            //Fire the refresh view event to update detail view
            $A.get('e.force:refreshView').fire();
        }
    });
    $A.enqueueAction(action);
}

Apex

@AuraEnabled
public static sObject updateRecord(string objNew, id objId , string selection, string fieldName){
    sObject sObj = Schema.getGlobalDescribe().get(objNew).newSObject(objId) ;
    sObj.put(fieldName , selection) ;
    update sObj ;
    return sObj;
}

Logging
16:22:13.0 (161658)|CODE_UNIT_STARTED|[EXTERNAL]|Aura
16:22:13.0 (1468788)|CODE_UNIT_STARTED|[EXTERNAL]|01p1l0000008UC9|select2TestCtrl.updateRecord(String, Id, String, String)
16:22:13.0 (1708031)|SYSTEM_MODE_ENTER|false
16:22:13.0 (1828648)|USER_DEBUG|[19]|DEBUG|null null null null
16:22:13.0 (14355318)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Class.select2TestCtrl.updateRecord: line 20, column 1
16:22:13.0 (14381632)|FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object

Class.select2TestCtrl.updateRecord: line 20, column 1
16:22:13.0 (14398622)|CODE_UNIT_FINISHED|select2TestCtrl.updateRecord(String, Id, String, String)
16:22:13.0 (18207207)|CODE_UNIT_FINISHED|Aura
16:22:13.0 (19271021)|EXECUTION_FINISHED
5
  • mind further elaborating on ' I don't get an update on my object neither a response ' ? your refresh event is fired? is your apex method invoked? is your alert fired? even if it were, and your apex method invoked, the refresh wont guarantee an updated view of the record since the action enters a queue and the update can take place after you refresh your view
    – glls
    Commented Aug 22, 2018 at 14:03
  • Can you turn on logging and see if there is an error on the server? Also wrap your javascript function with a try/catch. Sometimes lightning won't display syntax errors in the console Commented Aug 22, 2018 at 14:03
  • @Programatic I turned on logging, good idea. but cant figure out why my debug returns null see my update
    – Thomas
    Commented Aug 22, 2018 at 14:36
  • @glls My apex method is invoked, my alert is not fired, id updated the thread with some logging
    – Thomas
    Commented Aug 22, 2018 at 14:44
  • did you debug the "response.getState()"? Is it "SUCCESS"?
    – Anurag
    Commented Aug 22, 2018 at 14:48

3 Answers 3

3

Why don't I get an update on my object ?

-> null null null null

Cant get create a record if your apex controlLer does not receive any values.

Why dont I get a response in my lightning component?

Your response.getState() is NOT Success.

you aren't handling anything other than success, which is in part why you dont get feedback in your lightning controller.

If you check Calling a Server-Side Action, you'll notice that there arer other response states being handled in order to know what is going on in your controller such as INCOMPLETE or ERROR

        else if (state === "INCOMPLETE") {
            // do something
        }
        else if (state === "ERROR") {
            var errors = response.getError();
            if (errors) {
                if (errors[0] && errors[0].message) {
                    console.log("Error message: " + 
                             errors[0].message);
                }
            } else {
                console.log("Unknown error");
            }
        }

As a best practice, you might want to incldue those in your code for future reference.

In production - You will most likely want to display a toast message for when a server response fails, in order to notify the user.

The lightning:notificationsLibrary can provide the neccessary support for doing so: you will need to include:

<lightning:notificationsLibrary aura:id="notifLib"/>

in your component markup to be able to display a Toast, and in your state validation logic invoke the action to display it alongside your error message:

 component.find('notifLib').showToast({
        "title": "Notif library Success!",
        "message": "The record has been updated successfully."
    });
3
  • I added this with some alerts. I get an alert on an incomplete state so I still dont have a clue
    – Thomas
    Commented Aug 22, 2018 at 15:43
  • well, the reason you werent getting a response, was because of the lack of error handling, why the object was not created, because of the NullPointerException. I would recommend opening a new post if you are unsure why the NullPointerException occurs and how to pass the parameters to your apex controller.
    – glls
    Commented Aug 22, 2018 at 15:49
  • Best practice would certainly not be console.log though, it would be to fire a toast message event.
    – Adrian Larson
    Commented Aug 22, 2018 at 18:53
0

Also the apex class should have a try catch so the error is handled correctly in your lightning helper as glls mentioned. Something like catch(exception e) throw new AuraHandledException(e.getMessage());

0

In the apex class the selection is defined as string, this should be list

public static sObject updateRecord(string objNew, id objId , string selection, string fieldName)

must be

public static sObject updateRecord(string objNew, id objId , List<string> selection, string fieldName)

Then convert list to string like this

string picklistsel = '';
        for(string sel : selection){
            picklistsel += sel +';';
        }
sObject sObj = Schema.getGlobalDescribe().get(objNew).newSObject(objId) ;
        sObj.put(fieldName , picklistsel) ;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .