1

Having in issue on my program running the setCallback. I have this one below which is working 100%:

.js

alert("Saving Comments!");       
        var saveCommentsAction = component.get("c.saveComments");
        saveCommentsAction.setParams({
            "comments" : comments
        });
        saveCommentsAction.setCallback(this, function(saveCommentsResponse) {
            var state = saveCommentsResponse.getState();
            console.log("callback response: "+state);
            if (state === "SUCCESS") {
                alert("Save Successful!");
                secData = saveCommentsResponse.getReturnValue();
                console.log(secData);
                component.set("v.SectionData",secData);
                component.set("v.sectionComments",secData.Comments__r); 
            } else {
                alert("Save failed due to the following error: " + saveCommentsResponse.getError()[0].message);
            }
        });
        $A.enqueueAction(saveCommentsAction);

.apxc

@AuraEnabled
    public static Audit_Section__c saveComments(List<SObject> comments) {
        system.debug('about to update: ***');
         List<Audit_Comment__c> commentsToUpdate = new List<Audit_Comment__c>();
        for (sObject commentObject : comments) {
            system.debug('asigning: ***');
            Audit_Comment__c comment = new Audit_Comment__c();
            comment.Id = commentObject.Id;
            comment.Comment__c = String.valueOf(commentObject.get('Comment__c'));
            comment.Audit_Section__c = String.valueOf(commentObject.get('Audit_Section__c'));
            commentsToUpdate.add(comment);
        }
        upsert commentsToUpdate;

        system.debug('**********'+commentsToUpdate[0].Audit_Section__c);
        Audit_Section__c section = xxx;
        return section;

    }

The Above code is working 100% and followed that principle to create the below but the below refuses to work.. Just does not get to calling the server side controller.

.js

var commentToDel;
        var btnClicked = event.getSource();
        var comments = component.get("v.sectionComments");        
        var thisCommentId = btnClicked.get("v.class");
        console.log("about to delete comment id: "+thisCommentId);
        for (var i in comments)
        {
            if (comments[i].Id == thisCommentId)
            {
                console.log("im equal: "+comments[i].Id);
                commentToDel = comments[i];                
            }
        }

var deleteCommentAction = component.get("c.deleteComment");
        deleteCommentAction.setParams({
            "comment" : commentToDel
        });
        console.log("Set Params +");
        deleteCommentAction.setCallback(this, function(Response) {
            Console.log("Begin callback");
            var state1 = Response.getState();
            console.log("callback response: "+state1);
            if (state1 === "SUCCESS") {

                alert("delete Successful!");
            } else {
                alert("Save failed due to the following error: " + Response.getError()[0].message);
            }
        });
        console.log("About to enqueue");
        $A.enqueueAction(deleteCommentAction);

.apxc

@AuraEnabled
    public static void deleteComment(Audit_Comment__c comment) {
        system.debug('************** Deleting');
        DELETE  [SELECT Id,Name FROM Audit_Comment__c WHERE Id = :comment.Id];             
    }

Console Out

about to delete comment id: a0D0Y000001qt19UAA
:37 im equal: a0D0Y000001qt19UAA
:46 Set Params +
:58 About to enqueue

Output of window.setTimeout(function(){console.log(deleteCommentAction‌​.getError())}, 5000);

 Objet:
    action: null
    >JP
    message: "Action failed: c:Audit_Section_Comments$controller$deleteComment [Cannot read property 'getSource' of undefined]"
    name:"TypeError"

My browser Log gets to "Set Params +" and "about to enqueue" But never enters the Callback as it doesnt display the response as well as my Dev Console Logs have no record of the action.

Please if this question is vague or needs more info just let me know, I am still new to this Form. Thanks in Advance.

1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Adrian Larson
    Mar 15, 2017 at 12:36

2 Answers 2

6

According to this post, "a javascript method name in a component controller can never be the same name in an apex lightning controller":

Enqueued action not executed in Lightning Component

Just need to change the name of your javascript method to be different to the apex method

0
1

Might it simply be because of this line:

Console.log("Begin callback");

Console.log() should be console.log() -- case sensitive. Console.log() will throw an exception (Console object does not exist) and the rest of the code in your callback will not be executed.

Also, is the comment you pass to the Apex controller the actual comment you want to delete? In that case you can simply do:

delete comment;

... instead of executing a query to find the comment you already have.

Are the saveComments and deleteComment methods in the same Apex controller? Make sure the Apex controller is public or global.

I would also look at the commentToDel object before setParams() to make sure it's in the right shape:

console.log(commentToDel);
deleteCommentAction.setParams({
    "comment" : commentToDel
});
3
  • 1
    That would have been an issue if it got there, made the change but still does exactly the same. does not enter that callback function. tested by changing another console.log(); to Console.log(); gives straight error of [Console is not defined]
    – MrD
    Mar 15, 2017 at 13:47
  • Edited the answer with a suggestion for your Apex controller Mar 15, 2017 at 14:09
  • 1
    Didnt think one could delete so easily :) Thanks for that. Although still does not work. I dont even get apex controller log in my dev logs. Never gets there :(
    – MrD
    Mar 15, 2017 at 14:15

You must log in to answer this question.

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