+ Start a Discussion
Ertyq MrskErtyq Mrsk 

Call Apex Method Having Different Query Results in Salesforce LWC

I have an existing apex class method which contains three different queries. This method is originally being called by an apex trigger on before insert to handle bulk insert when using Data Loader or any ETL tools.

But I would also like to make values appear onload when I view the Salesforce LWC page.

Upon research, usual scenarios appearing are those which only involves a single query, then called via the wire service or imperatively. Is it possible to make a reference to the results of those three different queries through a single call? Or do I need to create separate method for each? The idea is that upon calling the map of results, I will be controlling the rest on javascript file.

Meanwhile, below are the current code I have:

CustomObjectController:
public with sharing class CustomObjectController {

    
    @AuraEnabled
    public static void copyRateItemValues(List<Custom_Object__c> cRecords) {
    
        List<Custom_Object__c> cList = new List<Custom_Object__c>();
        
    
        Map<Id, Rate_Item__c> rateItemMapA = new Map<Id, Rate_Item__c> ([SELECT Id, Name, Rate__c, 
                                                Rate_Amount__c, Start_Date__c, End_Date__c
                                                FROM Rate_Item__c WHERE Rate__r.Name = 'Rate A']);
                                                                                 

        Map<Id, Rate_Item__c> rateItemMapB = new Map<Id, Rate_Item__c> ([SELECT Id, Name, Rate__c, 
                                                Rate_Amount__c, Start_Date__c, End_Date__c
                                                FROM Rate_Item__c WHERE Rate__r.Name = 'Rate B']);

        Map<Id, Rate_Item__c> rateItemMapC = new Map<Id, Rate_Item__c> ([SELECT Id, Name, Rate__c, 
                                                Rate_Amount__c, Start_Date__c, End_Date__c
                                                FROM Rate_Item__c WHERE Rate__r.Name = 'Rate C']);

          
        if(cRecords.size() > 0) {
    
            for(Custom_Object__c cObj: cRecords) { 
            
                Date monthStartDate;  
                Integer firstMonth = 1;
                String yearStart;
                String yearEnd;
                Integer intYearStart = 0;
                Integer intYearEnd = 0;
                String firstMonthDate;
                
                yearStart = cObj.Fiscal_Year__c.right(4);   
                yearEnd = cObj.Fiscal_Year__c.right(4);
                
                intYearStart = Integer.ValueOf(yearStart); 
                intYearEnd = Integer.ValueOf(yearEnd) + 1;
                
                firstMonthDate = '1' + '/' + String.ValueOf(firstMonth) + '/' + String.ValueOf(intYearStart);
                
                
                if(cObj.Picklist__c == 'value 1') {
                
                    monthStartDate = Date.parse(firstMonthDate);
                
                }    
                
                for(Rate_Item__c rItemMapA : rateItemMapA.values()) {
                
                    if(rItemMapA.Start_Date__c == monthStartDate) {
                        cObj.Rate__c = rItemMapA.Rate_Amount__c;         
                    }    
                              
                }

                for(Rate_Item__c rItemMapB : rateItemMapB.values()) {

                    if(rItemMapB.Start_Date__c == monthStartDate) {   
                        cObj.Rate_Sales__c = rItemMapB.Rate_Amount__c; 
                    }   

                }

                for(Rate_Item__c rItemMapC : rateItemMapC.values()) {

                    if(rItemMapC.Start_Date__c == monthStartDate) {  
                        cObj.Rate_Service__c = exRateMap3.Rate_Amount__c;     
                    }   

                }
    
            }    
            
        }
        
    }
      
}