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
Timmy AhluwaliaTimmy Ahluwalia 

Use list in the other @AuraEanbled Method

Hi,
i am generating a list of Id in one method and i want to use that list in the other method to fetch data.
My program was working but all of sudden i am getting message as Attempt to de-reference a null object.
I found that the list is not asseccible in other methods.
Timmy AhluwaliaTimmy Ahluwalia
  • public class DealerSummaryController {
     
        @AuraEnabled
        static List<Rep_Code__c> repCodeN { get; set; }
        @AuraEnabled
        public static List<Rep_Code__c> AUMCodeN { get;  set; }
        public static List<Account> Accounts { get; private set; }
        public static List<Contact> Contacts { get; private set; }
        public static Set<ID> AccountSet { get; private set; }
        public Set<ID> IDsToAdd { get; private set; }
        public static String MyACCRT {get; set;}
        public static String RecordName {get; set;}
        @AuraEnabled
        public static Id accountId {get;set;}
        
        @AuraEnabled(cacheable=true)
        public Static list<Rep_Code__c>  Codes(Id accountId){
            
            system.debug(accountId);
            Set<ID> AccountSet = new Set<ID>();
            Set<ID> IDsToAdd= new Set<ID>();
            if(accountId != null){
                Account a = [SELECT Name,RecordType.Name FROM Account WHERE Id =:accountId];
                
                MyACCRT = a.RecordType.Name;
                RecordName= a.Name;
                system.debug(MyACCRT);
                system.debug(RecordName);
                
                if(a.RecordTypeId==Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Dealer').getRecordTypeId()){
                    repcodeN = [SELECT id from Rep_code__c WHERE Dealer_Branch__c= :accountid];
                    AumcodeN= [SELECT id FROM Rep_code__c WHERE Active__c =:true AND Dealer_Branch__c= :accountid];
                } 
              		          
                system.debug('R'+repcodeN);
                system.debug('A'+AumcodeN);
            }
            return repcodeN;
        }
        //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        //Tabel 1. YMList
        //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        @AuraEnabled(cacheable=true)
        public Static string Ymlist(){ 
            system.debug(accountId);
            list<Rep_Code__c> repcodes= new list<Rep_Code__c>();
    //Programe failing on the below line
            for(Rep_Code__c c : repcodeN){
                repcodes.add(c);
                 system.debug('c'+c);
            }

     
Timmy AhluwaliaTimmy Ahluwalia
Program failing on line 47
Deepali KulshresthaDeepali Kulshrestha
Hi Timmy,

I've gone through your code and you cannot retain the state of aura component methods,that why you didn't receiving the list of (repCodeN),
Hence you can just set your (repCodeN) List to the aura attribute on setCallback method in jsHelper,and then when the (YmList) method is calling
pass the (repConN) List to the method.

1.
Lightning apex -->
 
@AuraEnabled
public List<Rep_Code__c> repCodeN { get; set; }

@AuraEnabled
public Static list<Rep_Code__c>  Codes(Id accountId){
    system.debug(accountId);
    Set<ID> AccountSet = new Set<ID>();
    Set<ID> IDsToAdd= new Set<ID>();
    if(accountId != null){
        Account a = [SELECT Name,RecordType.Name FROM Account WHERE Id =:accountId];

        MyACCRT = a.RecordType.Name;
        RecordName= a.Name;
        system.debug(MyACCRT);
        system.debug(RecordName);

        if(a.RecordTypeId==Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Dealer').getRecordTypeId()){
            repcodeN = [SELECT id from Rep_code__c WHERE Dealer_Branch__c= :accountid];
            AumcodeN= [SELECT id FROM Rep_code__c WHERE Active__c =:true AND Dealer_Branch__c= :accountid];
        }

        system.debug('R'+repcodeN);
        system.debug('A'+AumcodeN);
    }
    return repcodeN;
}


2.JS Helper---->
 
YourJsHelper : function(c,e,h) 
{
    
    var action=c.get("c.Codes");
    
    action.setParams({
    accountId:passaccountId
    });
    
    
    action.setCallback(this,function(res)
                       {
                           if(res.getState()==='SUCCESS')
                           {
                               component.set("v.attributeNameForStoreRepConN",res.getReturnValue());

                                }
                        }
                    }


3.Then get that component value (i.e.attributeNameForStoreRepConN)
4.Pass to the method to Ymlist.


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com            
                                                
Timmy AhluwaliaTimmy Ahluwalia
Thanks Deepali, This programe i wrote last year an it was working as a charm, business raised ticket two days back that its not loading.
No idea what is wrong.