0

I want to call action1 server call after action server call gets over while doing enque. Since its not a best practice to call consecutively. I called the action1 inside action as shown below. Which is not executing as expected. My action1 has values which is dependent on action methods operation values. Which is not working. Please suggest the correct order.

  var classDetailJSON = JSON.stringify(obj);
    //Forming the json which accepts ILT Class details format
    classDetailJSON = '{'+'\"ClassDetails\": '+classDetailJSON+'}';
    console.log("***classDetailJSON***"+classDetailJSON);
    //Calling the server to assign the organization, location and etc in ILT Class & Session
    var action = cmp.get("c.postClass");
    action.setParams({ JSON : classDetailJSON});
    action.setCallback(this, function(response) {
        var state = response.getState();
        console.log(state);
         if (state === "SUCCESS") {
    // Creating Json format and assigning the grades for that student(Passing value to server side controller)
     var studentDetails = cmp.get("v.numberOfStudentsList");
             var ids=new Array();
             var jsonStr = '';
             for (var idx=0; idx<studentDetails.length; idx++) {
                 jsonStr += JSON.stringify(studentDetails[idx]);
                 if(idx != (studentDetails.length)-1)
                     jsonStr+=',';
             }
             console.log(jsonStr);
             jsonStr = '{'+'\"Students\": ['+jsonStr+'] } ';
             console.log(jsonStr);
             var action1 = cmp.get("c.addStudents");
             action1.setParams({ JSON : jsonStr});
             action1.setCallback(this, function(response) {
                var state = response.getState();
                console.log(state);
             });

             $A.enqueueAction(action1); // Action1 complete
         }
        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");
            }
        }

    });
             $A.enqueueAction(action); // Action Complete
4
  • Your code looks fine to me. Can you please elaborate on what is happening exactly and what are you expecting? Please edit the question.
    – ajinkyah
    Aug 30 '18 at 12:45
  • Based on "action" call to a method few records will be created for object1 and when "action1" call another method another set of records will be created for object2 in which a field in object 2 has the value of object 1 field. So I want to run "action", after creating records, assign that value to "action1's" created object records. this is not happening. Aug 30 '18 at 14:12
  • Bit Confusing, but hope you can understand my question Aug 30 '18 at 14:13
  • Can you confirm if the second action is being called by adding debug statements in your second apex method - addStudents? If it is not being called, there is something wrong in the Javascript code in the callback function for 'action'. Check for errors in browser console or add console.logs to check which lines are executed.
    – ajinkyah
    Aug 30 '18 at 16:04
1

I think part of the problem is that you are building your own JSON when you should just let the built in parser do it for you.

Plus, you should break out the nested call into it's own method. To do this, just save the state of this outside the anonymous function callback so you can call other methods in the helper object.

If you have your developer console open, you should be able to catch any errors that happen when you make the Apex server calls.

Here is roughly how I would define your functions - which at present are hard to read and likely contain errors:

saveClass : function(cmp){

  var self = this;
  //note the use of stringify - it can take an object or array
  var classDetailJSON = JSON.stringify({ClassDetails:obj});
  var action = cmp.get("c.postClass");
  action.setParams({ JSON : classDetailJSON});
  action.setCallback(this, function(response) {
    var state = response.getState();
    if (state === "SUCCESS") {
      //call the other handler method. There are many ways of doing this,
      //eg, using 'bind' or using Promises, but this is the simplest.
      self.retrieveStudentDetails(cmp)
    }
    else if (state === "ERROR") {
      var errors = response.getError();
      console.log('Error: ' + JSON.stringify(errors));
    }
  });
  $A.enqueueAction(action); // Action Complete
}


retrieveStudentDetails : function(cmp){

  var studentDetails = cmp.get("v.numberOfStudentsList");
  var jsonStr = JSON.stringify({Students:studentDetails});
  var action = cmp.get("c.addStudents");
  action.setParams({ JSON : jsonStr});
  action.setCallback(this, function(response) {
     var state = response.getState();
     console.log(state);
  });

  $A.enqueueAction(action); 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.