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
Marc FriedmanMarc Friedman 

Ways To Optimize This Code?

Hi All,
 
The following code works fine, but I was wondering if anyone had any suggestions about ways it can be optimized.
 
Relevant objects / relationships:
  • Employer_Subgroup__c is a child object of Accounts
  • Reward_Activity__c is a standalone object
  • Employer_Group_Reward_Activity__c is a child of Accounts with a Lookup to Reward_Activity__c
  • Employer_Subgroup_Reward_Activity__c is a standalone object with Lookups to Employer_Subgroup__c and Reward_Activity__c

Thanks,

Marc

Code:

global class PopulateSubgroups {

 WebService static Integer populateSubgroups(List<String> args, String groupId) {
  
  // Set the return variable to -1 in case the functional fails
  Integer result = -1;
  
  //Get a list of all Subgroups for the Group
  Employer_Subgroup__c[] subgroups = [select id from Employer_Subgroup__c where Group_Name__c = :groupId];
  
  // Return 0 if there are no Subgroups for the Group
  if (subgroups.size() == 0) {
   result = 0;
  
  } else {
   result = 1;
   
   // Create the array of Employer Subgroup Reward Activities to upsert
   Employer_Subgroup_Reward_Activity__c[] esras = new Employer_Subgroup_Reward_Activity__c[]{};
   
   // Get the Reward Amount and Reward Activity IDs for the selected Employer Group Reward Activities
   Employer_Group_Reward_Activity__c[] egras = [select Amount__c, Reward_Activity__c from Employer_Group_Reward_Activity__c where id in :args];
   
   // Loop through each Employer Group Reward Activity to see if it is already associated with any of the Subgroups
   for (Integer i = 0; i < egras.size(); i++) {
    
    // Loop through each Subgroup
    for (Integer j = 0; j < subgroups.size(); j++) {
     Integer k = [select count() from Employer_Subgroup_Reward_Activity__c where Reward_Activity__c = :egras[i].Reward_Activity__c and Employer_Subgroup__c = :subgroups[j].id];
     
     // If the same association exists more than once, ask User to notify the administrator (there is code to prevent this from happening, but is a good check just in case)
     if (k > 1) {
      result = -2;
     
     // If the association exists, populate the Employer Subgroup Reward Activity record and update the Amount
     } else if (k == 1) {
      Employer_Subgroup_Reward_Activity__c esra = [select id from Employer_Subgroup_Reward_Activity__c where Reward_Activity__c = :egras[i].Reward_Activity__c and Employer_Subgroup__c = :subgroups[j].id];
      esra.Amount__c = egras[i].Amount__c;
      esras.add(esra);
     
     // If the association doesn't exist, create it
     } else if (k == 0) {
      Employer_Subgroup_Reward_Activity__c esra = new Employer_Subgroup_Reward_Activity__c();
      esra.Reward_Activity__c = egras[i].Reward_Activity__c;
      esra.Employer_Subgroup__c = subgroups[j].id;
      esra.Amount__c = egras[i].Amount__c;
      esras.add(esra);
     }
     // End If...else if...
    }
   }
   // Upsert the Employer Subgroup Reward Activities
   try {
    upsert esras;
   } catch (System.DmlException e) {
    System.debug(e.getMessage());
    result = -1;
   }
  }
  
  return(result);
 }
}