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
JohnDuraiJohnDurai 

LWC Error when wrapper class return type is null

Hi - I facing error on one of my components when in controller class wrapper method returned the value as null. If the wrapper has some records then my component is working fine, can someone help me on debugging this?

For Example: my component is related list component, if that related list has records then the component is working fine if the record is null then i facing the error. Below is my code.

Controller Class:
Wrapper  Method:
public with sharing class FinancialAccountListControllerRefresh {
    @AuraEnabled(cacheable=true)
    public static FinancialAccountsWrapper getDescribeFinancialAccounts(String recordId, String recordtypesForFinancialAccounts, String fieldSetForFinancialAccount, String fieldSetForFinancialHolding,Boolean includeInActive,Boolean fullView){
            FinancialAccountsWrapper finAccountWrapper = new FinancialAccountsWrapper();
            finAccountWrapper.financialAccounts=getFinancialAccounts(recordId,recordtypesForFinancialAccounts,fieldSetForFinancialAccount,fieldSetForFinancialHolding,includeInActive,fullView);
            finAccountWrapper.fieldDescribe= getFieldDescribe(fieldSetForFinancialAccount,fieldSetForFinancialHolding);
            finAccountWrapper.financialAccountsCount=getFinancialAccountsCount(recordId,recordtypesForFinancialAccounts,fieldSetForFinancialAccount,fieldSetForFinancialHolding,includeInActive,fullView);
            system.debug('result: ' +finAccountWrapper);    
            return finAccountWrapper; 
    }


wire Method:
  @wire (getDescribeFinancialAccounts, { 
        recordId: '$recordId',
        recordtypesForFinancialAccounts: '$recordtypesForFinancialAccounts',
        fieldSetForFinancialAccount: '$fieldSetForFinancialAccount',
        fieldSetForFinancialHolding: '$fieldSetForFinancialHolding',
        includeInActive:'$includeInActive',
        fullView:'$fullView'
 }) 
    wiredFinancialAccount(result) {
        console.log(result);
        this.refreshaccount= result;
        this.financialAccounts = [];
        if (result.error) {
            this.handleError(result.error);
        } else if (result.data) {
            this.loadFinancialAccountAccess();
            //New Code
            //this.loadFinancialAccountDefaultRecordTypeId();
            var fieldData = JSON.parse(JSON.stringify(result.data));
            if(result.data.fieldDescribe){
            fieldData.fieldDescribe = JSON.parse(result.data.fieldDescribe);
            var obj = JSON.parse(result.data.fieldDescribe);
            this.financialAccountFields = obj.financialAccountFields;
            this.financialHoldingFields = obj.financialHoldingFields;
            //this.loadFinancialAccountsCount();
            //this.loadFinancialAccounts();
            }
if(result.data.financialAccounts){
            this.financialAccounts = this.formatData(result.data.financialAccounts, this.financialAccountFields);
                
                if(this.financialAccounts.length > 0){
                    this.hasAccounts = true;
                } 
                if (result.data.financialAccountsCount==0) {
                    this.countForDisplay = result.data.financialAccountsCount; 
                } 
                else {
                this.countForDisplay = result.data.financialAccountsCount;
                    if(this.countForDisplay > MAX_RESULTS && this.fullView==false){
                    this.countForDisplay = MAX_RESULTS + '+';
                    }
                }       
            }
        }
    }; 
 loadFinancialAccountAccess(){       
        isCreateFinancialAccountAccess({
          recordId: this.recordId
        })
        .then(result => {
            this.showNewButton = result;  
            this.error = undefined;  
        })
        .catch(error => {
            this.error = error;
        });
    }
PriyaPriya (Salesforce Developers) 

Hi John,

Can you please check the variable for financialAccountsCount first and if it is not 0 then check financialAccounts variable. Something like below snippet :- 
if(result.data.financialAccountsCount !=0){
    if(result.data.financialAccounts){
        this.financialAccounts = this.formatData(result.data.financialAccounts, this.financialAccountFields);
        if(this.financialAccounts.length > 0){
            this.hasAccounts = true;
        } 
        this.countForDisplay = result.data.financialAccountsCount;
        if(this.countForDisplay > MAX_RESULTS && this.fullView==false){
            this.countForDisplay = MAX_RESULTS + '+';
        }
    }else if (result.data.financialAccountsCount==0) {
        this.countForDisplay = result.data.financialAccountsCount; 
    }

 

If you still get any error, please post the screenshot of it.

Please mark as Best Answer so that it can help others in the future.

Regards,

Priya Ranjan