function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Cole PratoCole Prato 

Cannot read property 'Product_Version__c' of undefined Component Error

Hi,

I am getting this error: 
Uncaught Error in $A.getCallback() [Cannot read property 'Product_Version__c' of undefined]
Callback failed: apex://dynamicCardController/ACTION$getCaseDetails

I have a lightning component on the page that uses an Apex Controller to Grab Account Detials and Display on the Case Page.

I am only getting this error when the case does not have an account. I tired only to display the component when there is an account but still getting these errors.

End Goal is for me not to get the error on the page, since there would be no way to display the data without an account on the case.

Helper Code:
({
    getCaseDetails : function(cmp, event) {
        var recordId = cmp.get('v.recordId');
        var getCaseDetails = cmp.get('c.getCaseDetails');
        getCaseDetails.setParams({
            recordId : recordId
        })
        getCaseDetails.setCallback(cmp, function(response) {
            var state = response.getState();
            if (state === 'SUCCESS') {
                var results = response.getReturnValue();
                cmp.set('v.AccountId', results.AccountId);
                cmp.set('v.ProductVersion', results.Account.Product_Version__c);
                
            } else 
                console.log(response);
            
        })
        $A.enqueueAction(getCaseDetails);
    }
})

 
Best Answer chosen by Cole Prato
Maharajan CMaharajan C
Hi Cole,

I think the problem is ==> If there is no Account is related to the case then you are getting this error:

So just add the Account null check in your JS.

({
    doInit : function(cmp, event) {
        var recordId = cmp.get('v.recordId');
        var getCaseDetails = cmp.get('c.getCaseDetails');
        getCaseDetails.setParams({
            recordId : recordId
        })
        getCaseDetails.setCallback(cmp, function(response) {
            var state = response.getState();
            if (state === 'SUCCESS') {
                var results = response.getReturnValue();
                alert( ' ==> results' +  JSON.stringify(results) );
                if(results.AccountId != null )
                {
                  
  cmp.set('v.AccountId', results.AccountId);
                    cmp.set('v.ProductVersion', results.Account.Product_Version__c);
                }
               

            } else
                console.log(response);
           
        })
        $A.enqueueAction(getCaseDetails);
    }
})

Thanks,
Maharajan.C

All Answers

Cole PratoCole Prato
Controler:
public class dynamicCardController {
@AuraEnabled
    public static Case getCaseDetails(Id recordId) {
        Case c = [SELECT Id, AccountId, Account.Product_Version__c FROM Case WHERE Id =: recordId];
        return c;
    }
}
Maharajan CMaharajan C
Hi Cole,

I think the problem is ==> If there is no Account is related to the case then you are getting this error:

So just add the Account null check in your JS.

({
    doInit : function(cmp, event) {
        var recordId = cmp.get('v.recordId');
        var getCaseDetails = cmp.get('c.getCaseDetails');
        getCaseDetails.setParams({
            recordId : recordId
        })
        getCaseDetails.setCallback(cmp, function(response) {
            var state = response.getState();
            if (state === 'SUCCESS') {
                var results = response.getReturnValue();
                alert( ' ==> results' +  JSON.stringify(results) );
                if(results.AccountId != null )
                {
                  
  cmp.set('v.AccountId', results.AccountId);
                    cmp.set('v.ProductVersion', results.Account.Product_Version__c);
                }
               

            } else
                console.log(response);
           
        })
        $A.enqueueAction(getCaseDetails);
    }
})

Thanks,
Maharajan.C
This was selected as the best answer
Cole PratoCole Prato
Thank you! That worked. I was trying to do the same exact thing with the ProductVersion Variable, but now I understand why I couldnt do that and needed to do the null check using the AccountId