0

Updated:

Apex Code:

 public class MyAccountsController {

@auraEnabled
 public Static List<Account> getAccounts(Id accountID){
    List<Account> accountList = [Select Id,Name,Account_text__c, 
          AccountNumber,Website,Type,Industry,AnnualRevenue,Rating 
          FROM Account WHere id =: accountID];
    return accountList;
}


@auraEnabled
public static Map<String,String> saveAccount(List<Account> accountList1){

    Map<String,String> resultMap = new Map<String,String>();
    try{
        system.debug('saveAccount:'+accountList1);
        update accountList1;
        resultMap.put('status','success');
        resultMap.put('message','Data saved successfully');
    }
    catch(Exception e){
        resultMap.put('status','error');
        resultMap.put('message',e.getMessage());           
    }
    return resultMap;
 }

}

The Apex method is not been called. I check debug statements and also Console logs.

Controller:

({
  saveAccount : function(component, event, helper) {
    var btn = event.getSource();
    var name = btn.get('v.name');
    if(name=='save'){
        helper.saveAccounts(component, event, helper); 
    }
  }
})

Helper:

In helper class the saveAccount method is been called and im setting the Params. After that the setCallbck() method is not been called. I could see till the console log before the callback function. Is it because im using the setCallback twice in the process.??

 ({
    fetchAccounts : function(component, event, helper) 
      {


        var action = component.get("c.getAccounts");
        var accountId = component.get("v.recordId");
        //var toastEvent = $A.get('e.force:showToast');
        action.setParams({
            accountID : accountId
         });

        Console.log('test');         // I could see upto this
        action.setCallback(this, function(response){
        var state = response.getState();


        if(state ==='SUCCESS'){
            var accountList = response.getReturnValue();
            component.set("v.accountList",accountList);
        }
        else{
            alert('Error in getting data');
        }
    });
    $A.enqueueAction(action);
},

saveAccounts : function(component, event, helper){
    var accountList = component.get("v.accountList");
    console.log(accountList);
    var saveAction = component.get("c.saveAccount");
    var toastEvent = $A.get('e.force:showToast');
    saveAction.setParams({              
        accountList : accountList 
    });

    saveAction.setCallback(this, function(response){  // Not working
        var state = response.getState();
        var btn = event.getSource();
        console.log('test');
        if(state === 'SUCCESS'){
            console.log('test Success');
            var dataMap=response.getReturnValue();
            if(dataMap.status == 'success'){
                toastEvent.setParams({
                    'title' : 'Success!',
                    'type' : 'success',
                    'mode' : 'dismissable',
                    'message' : dataMap.message
                });
                toastEvent.fire();
            }
            else if(dataMap.status =='error'){
                toastEvent.setParams({
                    'title' : 'Error!',
                    'type' : 'error',
                    'mode' : 'dismissable',
                    'message' : dataMap.message
                });
                toastEvent.fire();
            }
        }
        else{
            alert('Error in getting data');
        }
      });
      console.log('last');    // I can see this log.
      $A.enqueueAction(saveAction);
    }
 })
3
  • did you check apex debug logs whether apex method getting called or not? Jun 4, 2018 at 12:34
  • setCallback() function use is to execute after the queue of actions returns ,so only you seeing last console log. Can you edit a question with apex class method so its easy to debug might be you missing something there.
    – vignesh
    Jun 4, 2018 at 12:44
  • please add backend code in the question or probably component code too, It will give the full picture Jun 4, 2018 at 13:24

4 Answers 4

1

I think this has something to do with the way you have defined your helper method. For the method fetchAccounts you have passed in the arguments as(component, event, helper) to which I believe that you should not pass the parameter helper. Please make the changes to (component, event) in the method definition and from where the method is being called i.e. your controller.

0

please make sure that returned javascript object from the server has a status variable and the values for it must be "success" or "error" case sensitive.

If your response does not contain a status variable with "success" or "error" then toast is not displayed

0

Server params should be called with the exact same names from server to client so you need the apex method definition to have the param name accountList (without the 1):

public static Map<String,String> saveAccount(List<Account> accountList1){

Then you can set your param on your client with the exact same name:

saveAction.setParams({              
    accountList : accountList 
});

As long as the param type is the same (you have defined the type in your markup which you have not shared with us), This should work

0

I got the answer and it worked, but don't know the reason.

Changed Apex method name from "saveAccount : function(component, event, helper)" to "saveAccount1 : function(component, event, helper)".

Also changed the variable name of "var saveAction = component.get("c.saveAccount");" to "var saveAction1 = component.get("c.saveAccount1");"

I think this is because the naming may have problem with some standard names.

You must log in to answer this question.

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