• Akshat Tiwari 2
  • NEWBIE
  • 10 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
global class TeacherBonusClass implements database.Batchable<sObject>, Database.Stateful{
    
    
    global Integer recordsProcessed = 0;
    
    
    global static Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator('Select Name, Salary__c from Teacher__c');
    }
    
    global static void execute(Database.BatchableContext bc, List<Teacher__c> teaList){
        
        List<Teacher__c> teaNewList = new List<Teacher__c>();
        for(Teacher__c tea: teaList){
            if(tea.Salary__c< 300000){
                tea.Salary__c = tea.Salary__c + (0.002 * tea.Salary__c);
                teaNewList.add(tea);
                
            }
            recordsProcessed = recordsProcessed + 1;
        }
        insert teaNewList;
        
    }
    
    global static void finish(Database.BatchableContext bc){
        
        Messaging.SingleEmailMessage  mail = new Messaging.SingleEmailMessage();
        string[] toAddresses = new String[] {'akshattiwari489@gmail.com'};
        mail.setSubject('Salary Bonus Alert');
        mail.setToAddresses(toAddresses);
        mail.setPlainTextBody('Your Salary Has been Incremented  by 0.2% and your new salary is');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        
        
        
        
    }

}

I am trying to maintain states accross the transaction and I am getting error " Variable Does Not Exist: recordsProcessed " when I am tring to store values inside the recordsProcessed inside for loop. What modifications should I make in the code?
I am trying to update the AnnualRevenue of Account into Opportunity amount and it is working for update.....During testing I am getting error System Variable does not exist: Amount
Here is the code
@isTest
public class OppAmountUpdate_Test {
    @isTest
    public static void myMethod_Test(){
        
        Account acc = new Account();
        //acc.Id = '0015j00000dkZj0AAE';
        acc.Name = 'Aviral Dandge';
        acc.AnnualRevenue = 25000;
        insert acc;
        
        Opportunity opp1 = new Opportunity();
        opp1.Name = 'Dandge Pvt Ltd';
        opp1.CloseDate = system.today();
        opp1.StageName = 'Qualification';
        insert opp1;
        
        Test.startTest();
        acc.AnnualRevenue = 50000;
        update acc;
        Test.stopTest();
        
        /*Opportunity oppNew = new Opportunity();
        oppNew.AccountId = acc.Id;*/
        /*Account newAcc = [Select Name, Id, AnnualRevenue from account where Id =: acc.Id];
        system.assertEquals(25000, acc.AnnualRevenue);*/
        
        List<Opportunity> opp = [Select AccountId, id, Amount from Opportunity 
                           where AccountId =:acc.Id];
        
        
        system.assertEquals(50000, opp.Amount);
        
            
        
        
    }

}
What modifications can I make in the code?
 
global class TeacherBonusClass implements database.Batchable<sObject>, Database.Stateful{
    
    
    global Integer recordsProcessed = 0;
    
    
    global static Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator('Select Name, Salary__c from Teacher__c');
    }
    
    global static void execute(Database.BatchableContext bc, List<Teacher__c> teaList){
        
        List<Teacher__c> teaNewList = new List<Teacher__c>();
        for(Teacher__c tea: teaList){
            if(tea.Salary__c< 300000){
                tea.Salary__c = tea.Salary__c + (0.002 * tea.Salary__c);
                teaNewList.add(tea);
                
            }
            recordsProcessed = recordsProcessed + 1;
        }
        insert teaNewList;
        
    }
    
    global static void finish(Database.BatchableContext bc){
        
        Messaging.SingleEmailMessage  mail = new Messaging.SingleEmailMessage();
        string[] toAddresses = new String[] {'akshattiwari489@gmail.com'};
        mail.setSubject('Salary Bonus Alert');
        mail.setToAddresses(toAddresses);
        mail.setPlainTextBody('Your Salary Has been Incremented  by 0.2% and your new salary is');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        
        
        
        
    }

}

I am trying to maintain states accross the transaction and I am getting error " Variable Does Not Exist: recordsProcessed " when I am tring to store values inside the recordsProcessed inside for loop. What modifications should I make in the code?
I am trying to update the AnnualRevenue of Account into Opportunity amount and it is working for update.....During testing I am getting error System Variable does not exist: Amount
Here is the code
@isTest
public class OppAmountUpdate_Test {
    @isTest
    public static void myMethod_Test(){
        
        Account acc = new Account();
        //acc.Id = '0015j00000dkZj0AAE';
        acc.Name = 'Aviral Dandge';
        acc.AnnualRevenue = 25000;
        insert acc;
        
        Opportunity opp1 = new Opportunity();
        opp1.Name = 'Dandge Pvt Ltd';
        opp1.CloseDate = system.today();
        opp1.StageName = 'Qualification';
        insert opp1;
        
        Test.startTest();
        acc.AnnualRevenue = 50000;
        update acc;
        Test.stopTest();
        
        /*Opportunity oppNew = new Opportunity();
        oppNew.AccountId = acc.Id;*/
        /*Account newAcc = [Select Name, Id, AnnualRevenue from account where Id =: acc.Id];
        system.assertEquals(25000, acc.AnnualRevenue);*/
        
        List<Opportunity> opp = [Select AccountId, id, Amount from Opportunity 
                           where AccountId =:acc.Id];
        
        
        system.assertEquals(50000, opp.Amount);
        
            
        
        
    }

}
What modifications can I make in the code?