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
Amr Ibrahim 42Amr Ibrahim 42 

variable does not exist error although it is used earlier in the code

 //For every account, step through all it's sale__c records and Sum up each field.  Save it to a local variable, then use variable to overwrite account's roll up field.  
            for(sale__c s : a.sales__r){
                //If a regular sale__c, roll up normal numbers
                if(!s.Mydentity_Sale__c){         NO ERROR
                    lastQuarter = lastQuarter + s.last_quarter__c;
                    lastQuarterPriorYear = lastQuarterPriorYear + s.Last_Quarter_Prior_Year__c;
                    TwoQuartersAgo = TwoQuartersAgo + s.X2_Quarters_Ago__c;
......
//Loyalty section
            //Calculate loyalty redemption value based off record types and revenue.  Only do this if it's not a Store.
            //Check if myDentity Sale
           if(s.Mydentity_Sale__c == True){  ERROR Variable does not exist
            if(a.recordtypeid != Store){
                
                    // Silver Pearl
                    if(MyIdentitylastQuarter > 499 && MyIdentitylastQuarter <= 1000) {
                        a.Mydentity_Loyalty_Redemption_Value__c = 1;
                        a.mydentity_Loyalty_Level__c = 'Silver Pearl';
 
Joe Harrison 6Joe Harrison 6
Hi Amr

It's hard to tell without the full code but is the loyalty section inside this loop?
for(sale__c s : a.sales__r)

If it isn't you won't be able to refer to s.Mydentity_Sale__c 
Amr Ibrahim 42Amr Ibrahim 42
Hi Joe, I am an admin trying to learn Apex still so here is the full code - my task is to add the loyalty level section for our other sister brand mydentity :

// 08Nov2019 CJ Simmons: Add fields Last_12_Months__c and Last_12_Months_Mydentity__c to calculations
// 02Dec2019 CJ Simmons: Corrected Loyalty level and Redemption amount calculations

public class AccountRollUp {
    public static void RollUp(list<Account> AccountList){
        //Get ID's of Account record types
        Id Store = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Store').getRecordTypeId();
        Id Other = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Other').getRecordTypeId();
        Id Salon = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Salon').getRecordTypeId();
        Id School = Schema.SObjectType.Account.getRecordTypeInfosByName().get('School').getRecordTypeId();
        
        //Get all account roll up fields, and their associated Sale__c records
        list<account> UpdateList = new list<account>();
        
        //step through each account and do calculation
        for(account a : [select last_quarter__c, Last_Quarter_Prior_Year__c,
                                                   X2_Quarters_Ago__c,Last_4_Quarters__c,
                                                   Last_12_Months__c,
                                                   This_Year_to_Date__c,Last_Year_to_Date__c,
                                                   Last_Quarter_Mydentity__c,
                                                   Last_Quarter_Prior_Year_Mydentity__c,
                                                   X2_Quarters_Ago_Mydentity__c,
                                                   Last_4_Quarters_Mydentity__c,
                                                   This_Year_to_Date_Mydentity__c,
                                                   Last_Year_to_Date_Mydentity__c,
                                                   Loyalty_Eligible__c,
                                                   recordtypeid,
                                                   Loyalty_Redemption_Value__c,
                                                   Mydentity_Loyalty_Redemption_Value__c,
                                                     mydentity_Loyalty_Level__c,
                                                   Last_quarter_Color__c,                                                  
                                                      (select Mydentity_Sale__c, last_quarter__c, Last_Quarter_Prior_Year__c,
                                                       X2_Quarters_Ago__c, Last_4_Quarters__c, Last_12_Months__c,
                                                       This_Year_to_Date__c, Last_Year_to_Date__c,Mydentity_Amount__c,color_amount__c
                                                       from sales__r) FROM account where status__c = 'Active' AND id in : AccountList]){
            decimal lastQuarter = 0;
            decimal lastQuarterPriorYear = 0;
            decimal TwoQuartersAgo = 0;
            decimal last4Quarters = 0;
            decimal last12Months = 0;
            decimal ThisYearToDate = 0;
            decimal LastYearToDate = 0;
            decimal MyIdentitylastQuarter = 0;
            decimal MyIdentitylastQuarterPriorYear = 0;
            decimal MyIdentityTwoQuartersAgo = 0;
            decimal MyIdentitylast4Quarters = 0;
            decimal MyIdentitylast12Months = 0;
            decimal MyIdentityThisYearToDate = 0;
            decimal MyIdentityLastYearToDate = 0;
            decimal ColorAmount = 0;
            decimal Loyalty;
            
            //For every account, step through all it's sale__c records and Sum up each field.  Save it to a local variable, then use variable to overwrite account's roll up field.  
            for(sale__c s : a.sales__r){
                //If a regular sale__c, roll up normal numbers
                if(!s.Mydentity_Sale__c){
                    lastQuarter = lastQuarter + s.last_quarter__c;
                    lastQuarterPriorYear = lastQuarterPriorYear + s.Last_Quarter_Prior_Year__c;
                    TwoQuartersAgo = TwoQuartersAgo + s.X2_Quarters_Ago__c;
                    last4Quarters = last4Quarters + s.Last_4_Quarters__c;
                    last12Months = last12Months + s.Last_12_Months__c;
                    ThisYearToDate = ThisYearToDate + s.This_Year_to_Date__c;
                    LastYearToDate = LastYearToDate + s.Last_Year_to_Date__c;
                    if(s.color_amount__c != Null && s.last_quarter__c > 0)
                        ColorAmount = ColorAmount + s.color_amount__c;
                //if mydentity, roll up mydentity numbers
                } else {
                    MyIdentitylastQuarter = MyIdentitylastQuarter + s.last_quarter__c;
                    MyIdentitylastQuarterPriorYear = MyIdentitylastQuarterPriorYear + s.Last_Quarter_Prior_Year__c;
                    MyIdentityTwoQuartersAgo = MyIdentityTwoQuartersAgo + s.X2_Quarters_Ago__c;
                    MyIdentitylast4Quarters = MyIdentitylast4Quarters + s.Last_4_Quarters__c;
                    MyIdentitylast12Months = MyIdentitylast12Months + s.Last_12_Months__c;
                    MyIdentityThisYearToDate = MyIdentityThisYearToDate + s.This_Year_to_Date__c;
                    MyIdentityLastYearToDate = MyIdentityLastYearToDate + s.Last_Year_to_Date__c;
                }
            }
            
            //Assign account's fields to the local sum from above.  
            a.Last_Quarter__c = lastQuarter;
            a.Last_Quarter_Prior_Year__c = lastQuarterPriorYear;
            a.X2_Quarters_Ago__c = TwoQuartersAgo;
            a.Last_4_Quarters__c = last4Quarters;
            a.Last_12_Months__c = last12Months;
            a.This_Year_to_Date__c = ThisYearToDate;
            a.Last_Year_to_Date__c = LastYearToDate;
            a.Last_Quarter_Mydentity__c = MyIdentitylastQuarter;
            a.Last_Quarter_Prior_Year_Mydentity__c = MyIdentitylastQuarterPriorYear;
            a.X2_Quarters_Ago_Mydentity__c = MyIdentityTwoQuartersAgo;
            a.Last_4_Quarters_Mydentity__c = MyIdentitylast4Quarters;
            a.Last_12_Months_Mydentity__c = MyIdentitylast12Months;
            a.This_Year_to_Date_Mydentity__c = MyIdentityThisYearToDate;
            a.Last_Year_to_Date_Mydentity__c = MyIdentityLastYearToDate;
            a.Last_Quarter_Color__c = ColorAmount;
            
            //Loyalty section
            //Calculate loyalty redemption value based off record types and revenue.  Only do this if it's not a Store.
            //Check if myDentity Sale
           if(s.Mydentity_Sale__c == TRUE) {
            if(a.recordtypeid != Store){
                
                    // Silver Pearl
                    if(MyIdentitylastQuarter > 499 && MyIdentitylastQuarter <= 1000) {
                        a.Mydentity_Loyalty_Redemption_Value__c = 1;
                        a.mydentity_Loyalty_Level__c = 'Silver Pearl';
                        
                    // Dusty Lavender   
                    } else if (MyIdentitylastQuarter > 1000 && MyIdentitylastQuarter <= 2500){
                        a.Mydentity_Loyalty_Redemption_Value__c = (MyIdentitylastQuarter * .05 );
                        a.mydentity_Loyalty_Level__c = ' Dusty Lavender';
                       
                    // Silver Smoke
                    } else if (MyIdentitylastQuarter > 2500 && MyIdentitylastQuarter <= 4000){
                        a.Mydentity_Loyalty_Redemption_Value__c = (MyIdentitylastQuarter * .07);
                        a.mydentity_Loyalty_Level__c = 'Silver Smoke';
                        
                    //Rose Gold    
                    } else if (MyIdentitylastQuarter > 4000){
                        a.Mydentity_Loyalty_Redemption_Value__c = (MyIdentitylastQuarter * .1);
                        a.mydentity_Loyalty_Level__c = 'Rose Gold';
                    //Unqualified    
                    } else {
                        a.Mydentity_Loyalty_Redemption_Value__c = 0;
                        a.mydentity_Loyalty_Level__c = 'Unqualified';
                             
                }
              }
            }
                else if(a.recordtypeid == School) {
                
                    //Bronze
                    if(lastQuarter >= 499 && lastQuarter <= 1500) {
                        a.Loyalty_Redemption_Value__c = (LastQuarter * .33);
                        a.Loyalty_Eligible__c = 'Bronze';

                    //Silver
                    } else if(lastQuarter > 1500 && lastQuarter <= 2500) {
                        a.Loyalty_Redemption_Value__c = (LastQuarter * .33) + (lastQuarter * .05);
                        a.Loyalty_Eligible__c = 'Silver';
                        
                    //Gold
                    } else if(lastQuarter > 2500 && lastQuarter <= 5000) {
                        a.Loyalty_Redemption_Value__c = (LastQuarter * .33) + (lastQuarter * .08);
                        a.Loyalty_Eligible__c = 'Gold';

                    //Diamond
                    } else if(lastquarter > 5000 && lastQuarter <= 10000) {
                        a.Loyalty_Redemption_Value__c = (LastQuarter * .33) + (lastQuarter * .10);
                        a.Loyalty_Eligible__c = 'Diamond';
                    
                    //Platinum
                    } else if(lastquarter > 10000) {
                        if(lastquarter > 20000) {
                            a.Loyalty_Redemption_Value__c = 8600;
                        } else {
                            a.Loyalty_Redemption_Value__c = (LastQuarter * .33) + (lastQuarter * .10);
                        }
                        a.Loyalty_Eligible__c = 'Platinum';
                    
                    //Unqualified
                    } else {
                        a.Loyalty_Redemption_Value__c = 0;
                        a.Loyalty_Eligible__c = 'Unqualified';
                    }
                        
                } else {
                
                    //Bronze
                    if(lastQuarter >= 1000 && lastQuarter <= 2500) {
                        a.Loyalty_Redemption_Value__c = (lastQuarter + ColorAmount) * .05;
                        a.Loyalty_Eligible__c = 'Bronze';
                
                    //Silver
                    } else if(lastQuarter > 2500 && lastQuarter <= 4000) {
                        a.Loyalty_Redemption_Value__c = (lastQuarter + ColorAmount) * .07;
                        a.Loyalty_Eligible__c = 'Silver';
                
                    //Gold
                    } else if (lastQuarter > 4000 && lastQuarter <= 5500) {
                        a.Loyalty_Redemption_Value__c = (lastQuarter + ColorAmount) * .08;
                        a.Loyalty_Eligible__c = 'Gold';
                    
                    //Diamond
                    } else if (lastquarter > 5500) {
                        a.Loyalty_Redemption_Value__c = (lastQuarter + ColorAmount) * .10;
                    a.Loyalty_Eligible__c = 'Diamond';
                
                    //Unqualified
                    } else {
                        a.Loyalty_Redemption_Value__c = 0;
                        a.Loyalty_Eligible__c = 'Unqualified';
                    }
                }
            
                                                                                                 
        
            UpdateList.add(a);
        
    }
       if(UpdateList.size() > 0){
            database.update(updateList, false);
        }
                                                    
    }      
  }
Amr Ibrahim 42Amr Ibrahim 42
So some background on the business process ; we have a Sale_c child object to the master standard Account object - this apex class is used to go into every sale record and calculate the quarterly sales and populate these fields on the account object and then the loyalty section checks the lastQuarter value and based on that calculates the loyalty eligibility (Bronze,Silver, or Gold) and then the loyalty redemption value as a percentage based of the sales amount - on the sale object there are regular fields for amounts and there are amount field for the other brand mydentity and there is a checkbox on the sale object which is checked when we upload our sales data 

your help will be highly highly appreciated - Thanks !!
Amr Ibrahim 42Amr Ibrahim 42
i added this part right above the line i had an error and now it's showing no errors - trying to schedule the apex classs to run but can't find it in the options of the scheduler