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
Jacob Elliott 1Jacob Elliott 1 

Return a list to be used in another method

Hi all, I'm a new Apex developer and I am trying to return a list in one method and reference it in another method. 

The bottom method "checkFieldChanges" is getting a list of fields that might have changed and I want to return that and reference it in the top method "resaveAccount" so that I can pass it to a batch class. Any help is greatly appreciated!

Top
public with sharing class CCIAllegianceAccountChanges {
    public static void resaveAccount(Account oldAcct, Account newAcct) {
        String acctType = newAcct.Account_Record_Type_Hidden__c;
        List <String> fieldChanged = new List<String>();
        boolean isACO = (oldAcct.V12C__Market__c == 'MHFN' || oldAcct.V12C__Market__c == 'PHQC') ? true : false;
        boolean isGroup = (acctType == 'Provider Group Account' || acctType == 'Ancillary Group Account') ? true : false;
        boolean isLocation = (acctType == 'Location' || acctType == 'Ancillary Location') ? true : false;

        if (isGroup && isACO) {

            if (fieldChanged.size() > 0) {
                //Query Locations
                try {
                    //Assigns the Parent ID to a variable for use in the query, queries for all locations, saves them in a list, and passes to batch class
                    String query = 'SELECT Id, Name, BillingStreet, BillingCity, BillingCountry, BillingState, BillingPostalCode, V12C__PR_Practice_Tax_ID_Number__c, Prac_TIN_Legal_Name__c, Child_Address_Override__c, Fee_Schedule__r.Name, Fee_Schedule_Effective_From_Date__c, V12C__PR_Practice_NPI_Number__c FROM Account WHERE Child_Address_Override__c = false AND (RecordType.DeveloperName = \'Location\' OR RecordType.Name = \'Ancillary Location\') AND ParentId =\'' + newAcct.Id + '\'';
                    List<Account> locations = Database.query(query);
                    System.debug('--------------List Size=' + locations.size());
                    Id batchInstanceId = Database.executeBatch(new CCIAllegianceLocationUpdates(locations, oldAcct, newAcct, String.join(fieldChanged, ',')), 100);

                    String queryCon = 'SELECT Id, Name, Allegiance_Status1__c, Last_Changed_Date__c, V12C__Effective_From_Date__c, V12C__Effective_Through_Date__c FROM Contact WHERE RecordType.DeveloperName = \'Provider\' AND Primary_Practicing_Location_Lookup__r.Child_Address_Override__c = false AND AccountId = \'' + newAcct.Id + '\'';
                    List<Contact> contacts = Database.query(queryCon);
                    System.debug('-------------- List Size=' + contacts.size());
                    Id batchInstanceIdCon = Database.executeBatch(new CCIAllegianceContactUpdates(contacts, oldAcct, newAcct, String.join(fieldChanged, ',')), 100);

                } catch (exception e) {
                    System.debug('Error Message: ' + e);
                }
            }
        } else if (isLocation && IsACO) {

            if (fieldChanged.size() > 0) {
                try {
                    String query = 'SELECT Id, Name, Last_Changed_Date__c, Allegiance_Status1__c, V12C__Effective_From_Date__c, V12C__Effective_Through_Date__c FROM Contact WHERE RecordType.DeveloperName = \'Provider\' AND Primary_Practicing_Location_Lookup__c = \'' + newAcct.Id + '\'';
                    List<Contact> contacts = Database.query(query);
                    System.debug('-------------- List Size=' + contacts.size());
                    Id batchInstanceId = Database.executeBatch(new CCIAllegianceContactUpdates(contacts, oldAcct, newAcct, String.join(fieldChanged, ',')), 100);

                    //CCIUpdateThisLocation.resaveLocation(oldAcct, newAcct, String.join(fieldChanged, ','));
                } catch (exception e) {
                    System.debug('Error Message: ' + e);
                }
            }
        }
    }

Bottom
public static List <String> checkFieldChanges(Account oldAcct, Account newAcct){
        List <String> fieldChanged = new List<String>();
        String acctType = newAcct.Account_Record_Type_Hidden__c;
        boolean isACO = (oldAcct.V12C__Market__c == 'MHFN' || oldAcct.V12C__Market__c == 'PHQC') ? true : false;
        boolean isGroup = (acctType == 'Provider Group Account' || acctType == 'Ancillary Group Account') ? true : false;
        boolean isLocation = (acctType == 'Location' || acctType == 'Ancillary Location') ? true : false;
        boolean billingChanged = (oldAcct.BillingStreet != newAcct.BillingStreet || oldAcct.BillingState != newAcct.BillingState || oldAcct.BillingCity != newAcct.BillingCity || oldAcct.BillingPostalCode != newAcct.BillingPostalCode || oldAcct.BillingCountry != newAcct.BillingCountry) ? true : false;
        boolean tinChanged = (oldAcct.PRAC_TIN_LEGAL_NAME__c != newAcct.PRAC_TIN_LEGAL_NAME__c || oldAcct.V12C__PR_Practice_Tax_ID_Number__c != newAcct.V12C__PR_Practice_Tax_ID_Number__c) ? true : false;
        boolean feeChanged = (oldAcct.Fee_Schedule__c != newAcct.Fee_Schedule__c) ? true : false;
        boolean NameChanged = (oldAcct.Name != newAcct.Name) ? true : false;

        if (billingChanged) {
            fieldChanged.add('Billing Address Changed');
        } else if (tinChanged) {
            fieldChanged.add('TIN Changed');
        }

        if(isLocation && isACO) {
         if (feeChanged) {
            fieldChanged.add('Fee Schedule Changed');
        } else if (nameChanged) {
            fieldChanged.add('Location Name Changed');
        }
        }

        return fieldChanged;
    }
}

 
Ravi Dutt SharmaRavi Dutt Sharma
To call a static method, you just need to reference it using ClassName.methodName