2

I can not return a custom Apex class from the server side to the client side.

Here is the class:

public class QuizDataNode {
    @AuraEnabled
    public SObject sob {get; set;}

    @AuraEnabled
    public QuizDataNode parent {get; private set;}
    @AuraEnabled
    public List<QuizDataNode> children {get; private set;}

    public QuizDataNode(SObject sob) {
        this.sob = sob;
        children = new List<QuizDataNode>();
    }

    public void addChild(QuizDataNode child) {
        children.add(child);
        child.parent = this;
    }
}

Here is the function which is returning the class instance:

@AuraEnabled
    public static QuizDataNode retrieveQuizInfo(String interactionId) {
        QuizDataNode res = new QuizDataNode(null);

        List<zced__Assessment__c> assessmentAndQuestions = [
            SELECT Id, (SELECT Id FROM zced__Assessment_Questions__r)
            FROM zced__Assessment__c
            WHERE zced__Interaction__c = :interactionId
        ];

        System.debug('assessmentAndQuestions: ' + assessmentAndQuestions);

        Set<String> questions = new Set<String>();
        Map<String, List<String>> assessmentToQuestions = new Map<String, List<String>>();
        for(zced__Assessment__c assessment: assessmentAndQuestions) {
            assessmentToQuestions.put(assessment.Id, new List<String>());
            for(zced__Assessment_Question__c question: assessment.zced__Assessment_Questions__r) {
                assessmentToQuestions.get(assessment.Id).add(question.Id);
                questions.add(question.Id);
            }
        }

        System.debug('questions: ' + questions);
        System.debug('assessmentToQuestions: ' + assessmentToQuestions);

        Map<Id, zced__Assessment_Question__c> questionToAnswers = new Map<Id, zced__Assessment_Question__c> ([
            SELECT zced__Question_text__c, zced__Question_Instruction__c,
            (
                SELECT zced__Answer_Text__c, zced__Is_Correct__c, zced__Answer_Points__c
                FROM zced__Assessment_Answers__r
            )
            FROM zced__Assessment_Question__c
            WHERE Id IN :questions
        ]);

        System.debug('questionToAnswers: ' + questionToAnswers);

        for(zced__Assessment__c e: [
            SELECT Id
            FROM zced__Assessment__c
            WHERE zced__Interaction__c = :interactionId
        ]) {
            QuizDataNode qdn = new QuizDataNode(new zced__Assessment__c(Id = e.Id));
            for(String f: assessmentToQuestions.get(e.Id)) {
                qdn.addChild(new QuizDataNode(questionToAnswers.get(f)));
            }
            res.addChild(qdn);
        }

        System.debug('res: ' + res);

        return res;
    }

Here is the error I am getting on the client side.

message:

Communication error, please retry or reload the page

stack:

Error: Communication error, please retry or reload the page at U.z.oh (https://zeddev-dev-ed.lightning.force.com/auraFW/javascript/M8mitrSnSoFe6Y855DnNgg/aura_prod.js:587:435) at Object.onXHRReceived (https://zeddev-dev-ed.lightning.force.com/libraries/instrumentation/idleDetector/idleDetector.js:2:265) at Object.TA.Ak (https://zeddev-dev-ed.lightning.force.com/auraFW/javascript/M8mitrSnSoFe6Y855DnNgg/aura_prod.js:240:255) at UG.xp (https://zeddev-dev-ed.lightning.force.com/auraFW/javascript/M8mitrSnSoFe6Y855DnNgg/aura_prod.js:835:421) at Object.TA.Ak (https://zeddev-dev-ed.lightning.force.com/auraFW/javascript/M8mitrSnSoFe6Y855DnNgg/aura_prod.js:240:255) at TA.start (https://zeddev-dev-ed.lightning.force.com/auraFW/javascript/M8mitrSnSoFe6Y855DnNgg/aura_prod.js:239:313) at XMLHttpRequest.e (https://zeddev-dev-ed.lightning.force.com/auraFW/javascript/M8mitrSnSoFe6Y855DnNgg/aura_prod.js:577:103)

Here are the related questions: 1, 2.

There are no errors in the log in Developer Console and the System.debug output is as I expect it.

When I replaced the function code with the following, no error was caught and System.debug logs remained correct :

@AuraEnabled
    public static QuizDataNode retrieveQuizInfo(String interactionId) {
        QuizDataNode res = new QuizDataNode(null);
        try{

        List<zced__Assessment__c> assessmentAndQuestions = [
            SELECT Id, (SELECT Id FROM zced__Assessment_Questions__r)
            FROM zced__Assessment__c
            WHERE zced__Interaction__c = :interactionId
        ];

        System.debug('assessmentAndQuestions: ' + assessmentAndQuestions);

        Set<String> questions = new Set<String>();
        Map<String, List<String>> assessmentToQuestions = new Map<String, List<String>>();
        for(zced__Assessment__c assessment: assessmentAndQuestions) {
            assessmentToQuestions.put(assessment.Id, new List<String>());
            for(zced__Assessment_Question__c question: assessment.zced__Assessment_Questions__r) {
                assessmentToQuestions.get(assessment.Id).add(question.Id);
                questions.add(question.Id);
            }
        }

        System.debug('questions: ' + questions);
        System.debug('assessmentToQuestions: ' + assessmentToQuestions);

        Map<Id, zced__Assessment_Question__c> questionToAnswers = new Map<Id, zced__Assessment_Question__c> ([
            SELECT zced__Question_text__c, zced__Question_Instruction__c,
            (
                SELECT zced__Answer_Text__c, zced__Is_Correct__c, zced__Answer_Points__c
                FROM zced__Assessment_Answers__r
            )
            FROM zced__Assessment_Question__c
            WHERE Id IN :questions
        ]);

        System.debug('questionToAnswers: ' + questionToAnswers);

        for(zced__Assessment__c e: [
            SELECT Id
            FROM zced__Assessment__c
            WHERE zced__Interaction__c = :interactionId
        ]) {
            QuizDataNode qdn = new QuizDataNode(new zced__Assessment__c(Id = e.Id));
            for(String f: assessmentToQuestions.get(e.Id)) {
                qdn.addChild(new QuizDataNode(questionToAnswers.get(f)));
            }
            res.addChild(qdn);
        }

        System.debug('res: ' + res);

         }
        catch (Exception e) {
            System.debug(e);
        }

        return res;
    }

Here is the output of System.debug calls:

15:20:23.20 (28726380)|USER_DEBUG|[350]|DEBUG|assessmentAndQuestions: (zced__Assessment__c:{Id=a031r00000VnN99AAF}, zced__Assessment__c:{Id=a031r00000VoWMqAAN})

15:20:23.20 (30114132)|USER_DEBUG|[362]|DEBUG|questions: {a041r00000ZoDZ8AAN}

15:20:23.20 (30210168)|USER_DEBUG|[363]|DEBUG|assessmentToQuestions: {a031r00000VnN99AAF=(a041r00000ZoDZ8AAN), a031r00000VoWMqAAN=()}

15:20:23.20 (35403476)|USER_DEBUG|[375]|DEBUG|questionToAnswers: {a041r00000ZoDZ8AAN=zced__Assessment_Question__c:{zced__Question_text__c=gnfgn, zced__Question_Instruction__c=zgnfgn, Id=a041r00000ZoDZ8AAN}}

15:20:23.20 (39762297)|USER_DEBUG|[389]|DEBUG|res: QuizDataNode:[children=(QuizDataNode:[children=(QuizDataNode:[children=(), parent=(already output), sob=zced__Assessment_Question__c:{zced__Question_text__c=gnfgn, zced__Question_Instruction__c=zgnfgn, Id=a041r00000ZoDZ8AAN}]), parent=(already output), sob=zced__Assessment__c:{Id=a031r00000VnN99AAF}], QuizDataNode:[children=(), parent=(already output), sob=zced__Assessment__c:{Id=a031r00000VoWMqAAN}]), parent=null, sob=null]

  • Can you try to put your code in try-catch block and then see the debug logs in the catch block. I think there could be a null pointer exception in your code. – Naval Sharma Jul 16 '18 at 14:14
3

It looks like you ran into a JSON serialization error. The hint here is the (already output) debug statement. This means that JSON is going to infinitely recurse through the structure until it reaches a maximum depth and then fail. JSON, the protocol, cannot handle recursive structures. You should exclude the "parent" option and calculate that on the client after receiving the data.


Besides reading the protocol, you can open up your favorite JavaScript environment (e.g. a Developer Console or NodeJS) and try the following:

var x = {};
x.parent = x;
JSON.stringify(x);

You'll get a Circular Reference error. Internally, this is the same thing that's happening when Lightning tries to process your recursive data structure.

  • Oh! It seems clear for me now. So, the deletion of the parent field should tackle the issue I hope. Thank you. – iloveseven Jul 16 '18 at 14:43

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.