1

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 methods 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;     
                    }   

                }
    
            }    
            
        }
        
    }
      
}
3

Yeah, you can do that. You can make your code return a custom class with all three collections and then do your thing. On the apex side, it'd look like this:

public with sharing class CustomObjectController {

  public class MyContainerClass {
    @AuraEnabled
    Map<Id, Rate_Item__c> rateItemMapA {get;set;}
    @AuraEnabled
    Map<Id, Rate_Item__c> rateItemMapB {get;set;}
    @AuraEnabled
    Map<Id, Rate_Item__c> rateItemMapC {get;set;}
  }

  @AuraEnabled
  public static MyContainerClass copyRateItemValues(List<Custom_Object__c> cRecords) {
    //Do your queries

    MyContainerClassr returnClass = new MyContainerClass();
    returnClassrateItemMapA = rateItemMapA;
    returnClassrateItemMapC = rateItemMapB;
    returnClassrateItemMapB = rateItemMapC;

    return returnClass;
  }
}

In JS you will get back one object with three properties, and each property will contain the map you assigned inside.

PS: I may not have interpreted your use-case properly (for the three queries) but you can extrapolate this concept to fit you more precisely.

1
  • 1
    Thanks for providing me the idea. Will try to implement your approach and check if I can bind it well with javascript and html. – Ertyq Jul 6 '20 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.