0

I am creating a lightning component that takes input and creates a new record with it. As soon as I type in the first input text field the following error occurs:

TypeError: Error in $A.getCallback() [d is null] throws at https://cunning-bear-335045-dev-ed.lightning.force.com/auraFW/javascript/pX1ganOCAMJgvd2N5wQKGQ/aura_prod.js:231:178 D.set()@https://cunning-bear-335045-dev-ed.lightning.force.com/auraFW/javascript/pX1ganOCAMJgvd2N5wQKGQ/aura_prod.js:455:371 H.prototype.set()@https://cunning-bear-335045-dev-ed.lightning.force.com/auraFW/javascript/pX1ganOCAMJgvd2N5wQKGQ/aura_prod.js:290:246 XI.prototype.set()@https://cunning-bear-335045-dev-ed.lightning.force.com/auraFW/javascript/pX1ganOCAMJgvd2N5wQKGQ/aura_prod.js:238:292 D.set()@https://cunning-bear-335045-dev-ed.lightning.force.com/auraFW/javascript/pX1ganOCAMJgvd2N5wQKGQ/aura_prod.js:455:399 H.prototype.set()@https://cunning-bear-335045-dev-ed.lightning.force.com/auraFW/javascript/pX1ganOCAMJgvd2N5wQKGQ/aura_prod.js:290:246 onNodeValueChange()@https://cunning-bear-335045-dev-ed.lightning.force.com/components/lightning/input.js:12:156 f.addChangeEvent/<()@https://cunning-bear-335045-dev-ed.lightning.force.com/libraries/lightning/domLibrary/dom.js:17:83 z()@https://cunning-bear-335045-dev-ed.lightning.force.com/libraries/lightning/domLibrary/dom.js:2:427

Component:

<aura:component controller="ExamBankCloner2" implements="force:lightningQuickAction,flexipage:availableForRecordHome,force:hasRecordId" >

    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

    <aura:attribute name="ExamToClone" type="Exam__c"/>
    <aura:attribute name="NewExam" type="Exam__c"/>

    <!-- Display an editing form -->
    <div class="Record Details">
        <lightning:card iconName="action:edit" title="Cloned Exam">
            <div class="slds-p-horizontal--small">

                <lightning:input label="Name" value="{!v.NewExam.Name}" required="true"/>
                <br/>

                <lightning:input label="Number of Minutes" value="{!v.NewExam.Number_of_Minutes__c}" required="true"/>
                <br/>

                <aura:if isTrue="{!v.ExamToClone.Graded__c}">
                    <lightning:input label="Graded" type="checkbox" value="{!v.NewExam.Graded__c}" checked="true"/>
                <aura:set attribute="else">
                    <lightning:input label="Graded" type="checkbox" value="{!v.NewExam.Graded__c}" checked="false"/>
                </aura:set>
                </aura:if>
                <br/>

                <aura:if isTrue="{!v.ExamToClone.Show_One_Question_At_A_Time__c}">
                    <lightning:input label="Show One Question at a Time" type="checkbox" value="{!v.NewExam.Show_One_Question_At_A_Time__c}" checked="true"/>
                <aura:set attribute="else">
                    <lightning:input label="Show One Question at a Time" type="checkbox" value="{!v.NewExam.Show_One_Question_At_A_Time__c}" checked="false"/>
                </aura:set>
                </aura:if>
                <br/>

                <aura:if isTrue="{!v.ExamToClone.Show_Score_Upon_Completion__c}">
                    <lightning:input label="Show Score Upon Completion" type="checkbox" value="{!v.NewExam.Show_Score_Upon_Completion__c}" checked="true"/>
                <aura:set attribute="else">
                    <lightning:input label="Show Score Upon Completion" type="checkbox" value="{!v.NewExam.Show_Score_Upon_Completion__c}" checked="false"/> 
                </aura:set>
                </aura:if>
                <br/>

                <aura:if isTrue="{!v.ExamToClone.Randomize_Question_Order__c}">
                    <lightning:input label="Randomize Question Order" type="checkbox" value="{!v.NewExam.Randomize_Question_Order__c}" checked="true"/>
                <aura:set attribute="else">
                    <lightning:input label="Randomize Question Order" type="checkbox" value="{!v.NewExam.Randomize_Question_Order__c}" checked="false"/> 
                </aura:set>
                </aura:if>
                <br/>

                <lightning:button variant="brand" label="Clone" iconName="action:new_campaign" onclick="{!c.createNewExam}" />

            </div>
        </lightning:card>
    </div>

</aura:component>

Apex controller:

public class ExamBankCloner2 {

    @AuraEnabled
    public static Exam__c loadExam(Id examID) {
        List<Exam__c> exam = [Select Name,Number_of_Minutes__c, Graded__c, Show_One_Question_at_a_Time__c, Show_Score_Upon_Completion__c, Randomize_Question_Order__c, Status__c From Exam__c Where ID = :examID];
        System.debug('existing exam size: ' + exam.size());
        return exam[0];        
    }

    public static void saveExam(Exam__c newExam, Id oldExam) {
        insert newExam;

        List<Exam_Bank__c> oldexambanks = [Select Exam__c,Question_Bank__c,Num_Questions__c From Exam_Bank__c where Exam__c = :oldExam];
        List<Exam_Bank__c> newexambanks = new List<Exam_Bank__c>();
        System.debug(oldexambanks);

        for(Exam_Bank__c oeb : oldexambanks){
            Exam_Bank__c neb = new Exam_Bank__c();
            neb.Exam__c = newExam.ID;
            neb.Question_Bank__c = oeb.Question_Bank__c;
            neb.Num_Questions__c = oeb.Num_Questions__c;
            newexambanks.add(neb);
            System.debug(neb.Exam__c);
        }

        insert newexambanks;
        System.debug(oldexambanks.size()); 
        System.debug(newexambanks.size());
    }

}

JavaScript controller:

({
    doInit : function(component, event, helper) {
        var loadExamID = component.get("v.recordId");
        var action = component.get("c.loadExam");

        action.setParams({
            examID : loadExamID
        });

        action.setCallback(this, function(response) {
             if (response.getState() === "SUCCESS") {

               let existingExam = response.getReturnValue();
                 console.log("EXSISTING EXAM:" + existingExam);
                 component.set("v.ExamToClone",existingExam);

             } else if (response.getState() === "ERROR") {
               $A.log("Errors", response.getError());
             }
        });

        $A.enqueueAction(action);

    },

    createNewExam : function(component, event, helper) {
        let save = component.get("c.saveExam");

        save.setParams({
            newExam : component.get("v.NewExam"),
            oldExam : component.get("v.recordId")
        });

        save.setCallback(this,function(response){
            var state = response.getState();
            if(state == "SUCCESS"){

                //display success message
                var resultsToast = $A.get("e.force:showToast");
                resultsToast.setParams({
                    "title": "Saved",
                    "message": "The record was SAVED!"
                });

                resultsToast.fire();

            }  else if (state === "INCOMPLETE") {
                // do nothing

            } 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");
                }
            }
        });



    }
})

This is all I have...

  • 1
    Define attributes like this: <aura:attribute name="ExamToClone" type="sObject” default=”{'sobjectType': 'Exam__c'}”/> <aura:attribute name="NewExam" type="sObject' default=”{'sobjectType': 'Exam__c'}"/> and also add other fields if you want to set default value. – itzmukeshy7 Dec 10 '17 at 4:04
  • Do you have to define default values for all fields ? – SallyRothroat Dec 11 '17 at 0:55
  • Also I read in the docs that they recomend using type Map to avoid 'serialization issues with the server' ? – SallyRothroat Dec 11 '17 at 0:56
0

Defining default values and changing the type to 'sObject' has stopped that strange d is null error.

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.