+ Start a Discussion
smriti sharan19smriti sharan19 

How to pass values from one method to another in Apex

I want to Pass a list named classList to another method within a class
Complete Requirement
Based on field values coming from case object after query, I want to create account and link the accountid to case. I have created two methods.
Method1 to validateCase
Method 2 to create account. 
I have created a List(caseList) at class level, iterated over list in method1(validateCase) and I want to get values of that in method2(createAccount).However, values of list is not coming in method 2. CaseList when debugging in method2 is not showing any result. 
public with sharing class AccountHandler  {
   
     public static List<Case> caseList = new List<Case>();
    
    @auraEnabled
    public static Boolean validateClaim(Map<String, String>claimIdMap, Map<String,String>claimDateMap) {
        Map<String, String> claimIdToClaimDateMap = new Map<String, String>();
        Boolean flag = false;
        DateTime datetimeinquiry;
        String convertToStr;
        String dateStr;
        Date dformat;
        Date d2format;
        
        Set<String> tempList = new Set<String>();
        tempList = claimIdMap.keySet();
        
        for(String s : tempList){
            claimIdToClaimDateMap.put(claimIdMap.get(s), claimDateMap.get(s));
        }
        System.debug('claimIdToClaimDateMap ' + claimIdToClaimDateMap);
        map<String,Date> myMap=new Map<String,Date>(); 
        caseList = [Select CaseNumber,Date_of_Injury__c,Insurer__c FROM Case where CaseNumber IN:claimIdToClaimDateMap.keySet()];
        system.debug('caseList'+caseList);
        for(Case c:caseList)
        { 
            if(c.Date_of_Injury__c!=null){
                date d = c.Date_of_Injury__c;
                myMap.put(c.CaseNumber, c.Date_of_Injury__c);
            }
        }
        for(String s : myMap.keySet()){
            dformat = Date.valueOf(claimIdToClaimDateMap.get(s));
            if(dformat == myMap.get(s)){
                flag = true;
            }else{
                flag = false;
                break;
            }
        }
        
        return flag;
    }

    
    @auraEnabled
    public static Account createAccount(account accrec,List<Case> caseList) {
        List<Case> casesToUpdate = new List<Case>();
        system.debug('inside createaccount');
        system.debug('accrec'+ accrec);
        system.debug('caseList' + caseList);
        try{
            //insert accrec;
            //caselist jo banayi h isme jo account field h usme accrec id update kardo 
            //for each - iterate on c - c.acc = accrec.id;
            //update caseList;
            //user u = createUser(); 
            //
            system.debug('caseList'+caseList);
            for (Case c: caseList) {
                system.debug('c: '+ c);
                c.Insurer__c = accrec.id;
            }
            update caseList;
            
        }
        catch(Exception ex) {
            System.debug('test'+ex.getMessage());
            throw new AuraHandledException(ex.getMessage());
        }
        
        return accrec;
    }
}

 
RituSharmaRituSharma
From where are you calling createAccount method?
smriti sharan19smriti sharan19
from LWC
 
smriti sharan19smriti sharan19
So firstly I am validating values coming from lwc in method1 and then if values are correct then again I am going to lwc and getting values for account and then creating account so both method are in sepeate threads because of which method2 is not getting caselist value
AbhishekAbhishek (Salesforce Developers) 
Check this once,

https://salesforce.stackexchange.com/questions/207977/pass-value-from-one-method-to-another

Another example,

https://developer.salesforce.com/forums/?id=906F0000000kCivIAE

It might help you.