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
bharath kumar 52bharath kumar 52 

Setting static variables in aura enabled methods

Hi All,

I am working on a requirement and i am facing an issue where "mapAdjIdToAdj" map is filled by a query in getOpportunitySize function and i can see the values too in that function using debug, but the map doesn't have any values when i use debug to get its values in getAdjustmentList function. I have also declared the map public and static so that it is accessible by all functions of the class .

public static map<id,opportunity_adjustment__c> mapAdjIdToAdj = new map<id,opportunity_adjustment__c> ();
Can someone please tell me what's wrong?

public class OppAdjPocController1 {
public static list<opportunityAdjustment> adjustmentList = new list<opportunityAdjustment>();
public static boolean isWon;
public static map<id,opportunity_adjustment__c> mapAdjIdToAdj = new map<id,opportunity_adjustment__c> ();

    @AuraEnabled
    public static list<opportunity_roc__c> getOpportunitySize(Id OpportunityId){
      
        list<opportunity_roc__c> oppSizeList = new list<opportunity_roc__c>([select id,name,opportunity__c,unit_price__c,Quantities__c from opportunity_roc__c where opportunity__c=:OpportunityId]);
        return oppSizeList;
    }

    
    @AuraEnabled
    public static list<opportunityAdjustment> getAdjustmentList(Id OpportunityId){
        list<opportunity_roc__c> oppSizeList = new list<opportunity_roc__c>([select id,Name,Opportunity__c,Opportunity__r.StageName,Unit_price__c,Quantities__c from opportunity_roc__c where opportunity__c=:OpportunityId]);
		list<opportunity_adjustment__c> oppAdjList = new list<opportunity_adjustment__c>([select id,Opportunity_Size__c,Adjusted_Price__c,Adjusted_Quantity__c,Reason_for_adjustment__c,latest_version__c from opportunity_adjustment__c where opportunity__c=:OpportunityId and latest_version__c=true]);
        map<id,opportunity_adjustment__c> mapOppSizeIdToAdj = new map<id,opportunity_adjustment__c> ();
        
        if(!oppAdjList.isEmpty()){
			for(opportunity_adjustment__c oAdj : oppAdjList){
                mapOppSizeIdToAdj.put(oAdj.Opportunity_Size__c,oAdj);
                //mapOppSizeIdToAdj.put(oAdj.Opportunity_Size__c,new list<opportunity_adjustment__c>{oAdj});
				mapAdjIdToAdj.put(oAdj.Id,oAdj);//this works for 1 to many
			}
		
		}
        	system.debug('mapAdjIdToAdj>>>>>>>'+mapAdjIdToAdj.values());//getting values here
         for(opportunity_roc__c os : oppSizeList){
            string adjReason;
            Decimal adjAmount=0;
            Integer adjQuantity=0;
            
            if(mapOppSizeIdToAdj.get(os.Id)!=null){
                adjAmount=mapOppSizeIdToAdj.get(os.Id).Adjusted_Price__c;
				adjQuantity=Integer.valueof(mapOppSizeIdToAdj.get(os.Id).Adjusted_Quantity__c);
				adjReason=mapOppSizeIdToAdj.get(os.Id).Reason_for_adjustment__c;             
            }
            else{
               adjAmount=0; 
               adjQuantity=0;
               adjReason='';
            }

             adjustmentList.add(new opportunityAdjustment(os.name,os.unit_price__c,os.quantities__c,adjReason,adjAmount,adjQuantity,os.opportunity__c,os.opportunity__r.StageName,os.Id));
           
           
        }

           return adjustmentList;
        
    }
    
    
    
    @AuraEnabled
    public static string createOpportunityAdjustment(list<opportunityAdjustment> oppAdjlist){
       	string message='';
        list<Opportunity_Adjustment__c> adjustmentListToInsert = new list<Opportunity_Adjustment__c>();
        list<Opportunity_Adjustment__c> previousAdjListToUpdate = new list<Opportunity_Adjustment__c>();
        try{
            
            Opportunity_Adjustment__c oppAdj;
            if(oppAdjlist[0].oppStageName!='3 - Won'){
                throw new OppAdjPocControllerException('You cannot update adjustments until the opportunity stage is  3 - Won');
            }else{
                for(opportunityAdjustment oa : oppAdjlist){
                    oppAdj = new Opportunity_Adjustment__c();
                    oppAdj.Adjusted_Price__c = oa.adjustmentAmount;
                    oppAdj.Adjusted_Quantity__c = oa.adjustmentQuantity;
                    oppAdj.Reason_for_adjustment__c = oa.adjustmentReason;
                    oppAdj.Adjustment_amount__c = oa.adjustmentAmount * oa.adjustmentQuantity;
                    oppAdj.Opportunity_Size__c = oa.oppSizeId;
                    oppAdj.Opportunity__c = oa.oppId;
                    oppAdj.Opportunity_Size_ExtId__c = oa.oppSizeId;
                    oppAdj.Latest_version__c = True;
                    if(oppAdj.Adjusted_Price__c>0 || oppAdj.Adjusted_Quantity__c>0){
                       
                    adjustmentListToInsert.add(oppAdj);
                    } 
                }
               
                for(Opportunity_Adjustment__c prevAdj : mapAdjIdToAdj.values()){
                    //getting null in the iterable list mapAdjIdToAdj.values() despite being set 
					
                    Opportunity_Adjustment__c oa = new Opportunity_Adjustment__c();
                    oa.Id=prevAdj.Id;
                    if(prevAdj.Latest_Version__c==true){
                        oa.Latest_Version__c=false;
                    }
                       previousAdjListToUpdate.add(oa); 
                }
                if(previousAdjListToUpdate!=null){
                update previousAdjListToUpdate;
                }
                
            }  //else - Stage check
                if(adjustmentListToInsert!=null){
                    
                   insert adjustmentListToInsert;

                }
                
        }    
        catch(Exception e){
            message=e.getMessage();
            System.debug('Error '+e.getMessage());
            System.debug('Error line'+e.getLineNumber());
        }
        return message;
    }


    class opportunityAdjustment{
 		
        @AuraEnabled
        public string osiName{get;set;}
        @AuraEnabled
        public Decimal unitPrice{get;set;}
        @AuraEnabled
        public Decimal quantity{get;set;} 
        @AuraEnabled
        public string adjustmentReason{get;set;}
        @AuraEnabled
        public Integer adjustmentQuantity{get;set;} 
        @AuraEnabled
        public Decimal adjustmentAmount{get;set;}
        @AuraEnabled
        public string oppId{get;set;}
        @AuraEnabled
        public string oppStageName{get;set;} 
        @AuraEnabled
        public string oppSizeId{get;set;} 
        
        public opportunityAdjustment(){
            
        }
         		
        public opportunityAdjustment(string name,Decimal osUnitPrice, Decimal osQty, String adjReason, Decimal adjAmount,Integer adjQty, Id oppId,String oppStageName, Id oppSizeId){
        osiName=name;
        unitPrice=osUnitPrice;
        quantity=osQty;   
        adjustmentReason=adjReason;
        adjustmentAmount=adjAmount;
        adjustmentQuantity=adjQty;
        this.oppStageName=oppStageName;
        this.oppId=oppId;
        this.oppSizeId=oppSizeId;
    	}
    }


}
ShirishaShirisha (Salesforce Developers) 
Hi Bharath,

Greetings!

Unfortunately,it is very difficult for us to debug the above code,however you can try the lightning inspector by using the debugger to check,why the map becoming as null when you try to debug.

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/inspector_use.htm

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri