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
Abhishek Sharma 527Abhishek Sharma 527 

Wrapper class apex

Hello Developers, I have apex class which consists of 2 queries and I want to merge it inside wrapper class.
here's my class
public class NonBillableReport {

    @AuraEnabled
    public static List<Event> getNonBillable(){
    List<Event> nonBill = [Select Subject, Id, Facilities__c, Services__c, Appointment_Status__c, OwnerId, WhoId,
             ActivityDateTime, ActivityDate, WhatId, Facilities__r.Address_1__c,Facilities__r.City__c, Facilities__r.Name,Facilities__r.Country__c, Sup_Provider__c,Sup_Provider__r.Name, Owner.FirstName, Owner.MiddleName, Owner.LastName,
EndDateTime, Facilities__r.State__c,
Sup_Provider__r.Provider_Role__c FROM Event WHERE IsRecurrence = false AND Appointment_Status__c = 'Non-Billable' LIMIT 100];
 return nonBill;

 List<Session> SessionRecords = [Select Case_Number__c,Case_Number__r.Client_Insurance__c,      Case_Number__r.Total_Amounts__c,Case_Number__r.Client_Insurance__r.Insurance_Type__c,Case_Number__r.Client_Insurance__r.Insured_ID__c,       Client_Name__c,Case_Number__r.Patient_Customer__r.Client_ID__c,Case_Number__r.Patient_Customer__r.DOB__c,Insurance_Name__c,     Case_Number__r.InsuranceType__c,Case_Number__r.Claim_ID__c,Case_Number__r.Insurance__r.Payer_Id__c,Case_Number__r.Insurance__r.Name,From_DOS__c,Case_Number__r.Location_Name__c,Case_Number__r.Facility_Type__c,Case_Number__r.Provider_Name__c,Case_Number__r.Provider__c,   Case_Number__r.Authorization_Number__c, Authorization_Number__c,Case_Number__r.CaseNumber , Procedure_Code__c,Modifier1__r.Name,Modifier2__r.Name,Modifier3__r.Name,Modifier4__r.Name ,Unit_Count__c, Case_Number__r.Billed_Amount__c,       Practice_Fee__c,Case_Number__r.Status,Case_Number__r.First_Bill_Date__c,Case_Number__r.Last_Bill_Date__c, ICD_Code__c,     	NDC_Code__c,NDC_Unit_Price__c,Unit_of_Measure__c,Case_Number__r.Unit__c,Case_Number__r.FacilitiesV3__c, 
Audit_Status__c,lastModifiedBy.Name, lastModifiedDate,PracticeFee__c FROM Case_Line_Item__c limit 10];
     }
}
Please help me or suggest if something is incorrect
 
Best Answer chosen by Abhishek Sharma 527
Prateek Prasoon 25Prateek Prasoon 25
Sure, here's the updated code with the merged queries inside a wrapper class:
public class NonBillableReport {
    public class NonBillableWrapper {
        @AuraEnabled public Event event { get; set; }
        @AuraEnabled public Session session { get; set; }
        
        public NonBillableWrapper(Event event, Session session) {
            this.event = event;
            this.session = session;
        }
    }
    
    @AuraEnabled
    public static List<NonBillableWrapper> getNonBillable() {
        List<NonBillableWrapper> nonBillableList = new List<NonBillableWrapper>();
        
        List<Event> eventList = [SELECT Subject, Id, Facilities__c, Services__c, Appointment_Status__c, OwnerId, WhoId,
                                  ActivityDateTime, ActivityDate, WhatId, Facilities__r.Address_1__c,Facilities__r.City__c, 
                                  Facilities__r.Name,Facilities__r.Country__c, Sup_Provider__c,Sup_Provider__r.Name, Owner.FirstName, 
                                  Owner.MiddleName, Owner.LastName, EndDateTime, Facilities__r.State__c,
                                  Sup_Provider__r.Provider_Role__c 
                                  FROM Event 
                                  WHERE IsRecurrence = false AND Appointment_Status__c = 'Non-Billable' 
                                  LIMIT 100];
        
        List<Session> sessionList = [SELECT Case_Number__c, Case_Number__r.Client_Insurance__c, Case_Number__r.Total_Amounts__c,
                                      Case_Number__r.Client_Insurance__r.Insurance_Type__c,Case_Number__r.Client_Insurance__r.Insured_ID__c,       
                                      Client_Name__c, Case_Number__r.Patient_Customer__r.Client_ID__c, Case_Number__r.Patient_Customer__r.DOB__c,
                                      Insurance_Name__c, Case_Number__r.InsuranceType__c, Case_Number__r.Claim_ID__c, Case_Number__r.Insurance__r.Payer_Id__c,
                                      Case_Number__r.Insurance__r.Name, From_DOS__c, Case_Number__r.Location_Name__c, Case_Number__r.Facility_Type__c,
                                      Case_Number__r.Provider_Name__c, Case_Number__r.Provider__c, Case_Number__r.Authorization_Number__c, 
                                      Authorization_Number__c, Case_Number__r.CaseNumber , Procedure_Code__c,Modifier1__r.Name,Modifier2__r.Name,
                                      Modifier3__r.Name, Modifier4__r.Name ,Unit_Count__c, Case_Number__r.Billed_Amount__c, Practice_Fee__c,
                                      Case_Number__r.Status, Case_Number__r.First_Bill_Date__c, Case_Number__r.Last_Bill_Date__c, ICD_Code__c,
                                      NDC_Code__c, NDC_Unit_Price__c, Unit_of_Measure__c, Case_Number__r.Unit__c, Case_Number__r.FacilitiesV3__c, 
                                      Audit_Status__c, lastModifiedBy.Name, lastModifiedDate, PracticeFee__c 
                                      FROM Case_Line_Item__c 
                                      LIMIT 10];
        
        // Merge Event and Session records into wrapper class
        for (Event event : eventList) {
            for (Session session : sessionList) {
                if (event.WhatId == session.Case_Number__c) {
                    nonBillableList.add(new NonBillableWrapper(event, session));
                    break;
                }
            }
        }
        
        return nonBillableList;
    }
}

If you find this answer helpful, Please mark it as the best answer.

All Answers

GulshanRajGulshanRaj
While creating a wrapper class consider the following points:
  • Avoid using unnecessary fields in SOQL query and same is applicable for wrapper class.
  • Build a wrapper class with all valid error handlers.
use this link as a reference for creating a wrapper class: https://www.sfdcpoint.com/salesforce/wrapper-class-in-lwc/


Best Regards
Gulshan Raj 
Prateek Prasoon 25Prateek Prasoon 25
Sure, here's the updated code with the merged queries inside a wrapper class:
public class NonBillableReport {
    public class NonBillableWrapper {
        @AuraEnabled public Event event { get; set; }
        @AuraEnabled public Session session { get; set; }
        
        public NonBillableWrapper(Event event, Session session) {
            this.event = event;
            this.session = session;
        }
    }
    
    @AuraEnabled
    public static List<NonBillableWrapper> getNonBillable() {
        List<NonBillableWrapper> nonBillableList = new List<NonBillableWrapper>();
        
        List<Event> eventList = [SELECT Subject, Id, Facilities__c, Services__c, Appointment_Status__c, OwnerId, WhoId,
                                  ActivityDateTime, ActivityDate, WhatId, Facilities__r.Address_1__c,Facilities__r.City__c, 
                                  Facilities__r.Name,Facilities__r.Country__c, Sup_Provider__c,Sup_Provider__r.Name, Owner.FirstName, 
                                  Owner.MiddleName, Owner.LastName, EndDateTime, Facilities__r.State__c,
                                  Sup_Provider__r.Provider_Role__c 
                                  FROM Event 
                                  WHERE IsRecurrence = false AND Appointment_Status__c = 'Non-Billable' 
                                  LIMIT 100];
        
        List<Session> sessionList = [SELECT Case_Number__c, Case_Number__r.Client_Insurance__c, Case_Number__r.Total_Amounts__c,
                                      Case_Number__r.Client_Insurance__r.Insurance_Type__c,Case_Number__r.Client_Insurance__r.Insured_ID__c,       
                                      Client_Name__c, Case_Number__r.Patient_Customer__r.Client_ID__c, Case_Number__r.Patient_Customer__r.DOB__c,
                                      Insurance_Name__c, Case_Number__r.InsuranceType__c, Case_Number__r.Claim_ID__c, Case_Number__r.Insurance__r.Payer_Id__c,
                                      Case_Number__r.Insurance__r.Name, From_DOS__c, Case_Number__r.Location_Name__c, Case_Number__r.Facility_Type__c,
                                      Case_Number__r.Provider_Name__c, Case_Number__r.Provider__c, Case_Number__r.Authorization_Number__c, 
                                      Authorization_Number__c, Case_Number__r.CaseNumber , Procedure_Code__c,Modifier1__r.Name,Modifier2__r.Name,
                                      Modifier3__r.Name, Modifier4__r.Name ,Unit_Count__c, Case_Number__r.Billed_Amount__c, Practice_Fee__c,
                                      Case_Number__r.Status, Case_Number__r.First_Bill_Date__c, Case_Number__r.Last_Bill_Date__c, ICD_Code__c,
                                      NDC_Code__c, NDC_Unit_Price__c, Unit_of_Measure__c, Case_Number__r.Unit__c, Case_Number__r.FacilitiesV3__c, 
                                      Audit_Status__c, lastModifiedBy.Name, lastModifiedDate, PracticeFee__c 
                                      FROM Case_Line_Item__c 
                                      LIMIT 10];
        
        // Merge Event and Session records into wrapper class
        for (Event event : eventList) {
            for (Session session : sessionList) {
                if (event.WhatId == session.Case_Number__c) {
                    nonBillableList.add(new NonBillableWrapper(event, session));
                    break;
                }
            }
        }
        
        return nonBillableList;
    }
}

If you find this answer helpful, Please mark it as the best answer.
This was selected as the best answer
Abhishek Sharma 527Abhishek Sharma 527
Thank you very much Prateek.