• Bill5
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies

When setting a instance variables value from within a batch context the value is not retained when testing.

 

Example code:

 

public class FinishLogic {
    public Integer val { get; set; }
}

 

global class UserBatch implements Database.Batchable<sObject>, Database.Stateful {
    
    private final FinishLogic fl;
    
    public UserBatch(FinishLogic fl) {
        this.fl = fl;
    }
    
    global Iterable<sObject> start(Database.BatchableContext bc) {
        return [SELECT Id FROM User LIMIT 1];
    }
    
    global void execute(Database.BatchableContext bc, List<User> scope) {
        //do some stuff
    }
    
    global void finish(Database.BatchableContext bc) {
        fl.val = 5;
    }
}

 

@isTest
public class TestUserBatch {
    
    static testMethod void shouldReturnFive() {
        final FinishLogic fl = new FinishLogic();
        final UserBatch ub = new UserBatch(fl);
        
        Test.StartTest();
        Database.executeBatch(ub);
        Test.StopTest();
        
        System.assertEquals(5, fl.val);
    }
}

 

The test should pass as the value should be 5 but instead returns null. This is a simple example for demonstration purposes, but this behavor is important to test the correct functionality of a batch. 

If I add the line "System.assert(false, 'executing finish);" to the batches finish method the test does correctly fail with the error, therefore the method is being called. Does anybody know why this wouldn't work, or is this a known bug?

 

Thanks

 

  • November 20, 2012
  • Like
  • 0

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables_considerations.htm


According to the above link a before delete trigger "can update an original object using an update DML operation nd the updates are saved before the object is deleted, so if the object is undeleted, the updates become visible". However, the contents of the trigger.old collection are read-only and trigger.new is only available in insert and update triggers. Therefore how can a objects value be modified as part of the trigger before delete?

 

I would like to be able to chage the value of one of the fields on an object when it is deleted.

 

 

Thanks

Bill

  • November 01, 2012
  • Like
  • 0

When setting a instance variables value from within a batch context the value is not retained when testing.

 

Example code:

 

public class FinishLogic {
    public Integer val { get; set; }
}

 

global class UserBatch implements Database.Batchable<sObject>, Database.Stateful {
    
    private final FinishLogic fl;
    
    public UserBatch(FinishLogic fl) {
        this.fl = fl;
    }
    
    global Iterable<sObject> start(Database.BatchableContext bc) {
        return [SELECT Id FROM User LIMIT 1];
    }
    
    global void execute(Database.BatchableContext bc, List<User> scope) {
        //do some stuff
    }
    
    global void finish(Database.BatchableContext bc) {
        fl.val = 5;
    }
}

 

@isTest
public class TestUserBatch {
    
    static testMethod void shouldReturnFive() {
        final FinishLogic fl = new FinishLogic();
        final UserBatch ub = new UserBatch(fl);
        
        Test.StartTest();
        Database.executeBatch(ub);
        Test.StopTest();
        
        System.assertEquals(5, fl.val);
    }
}

 

The test should pass as the value should be 5 but instead returns null. This is a simple example for demonstration purposes, but this behavor is important to test the correct functionality of a batch. 

If I add the line "System.assert(false, 'executing finish);" to the batches finish method the test does correctly fail with the error, therefore the method is being called. Does anybody know why this wouldn't work, or is this a known bug?

 

Thanks

 

  • November 20, 2012
  • Like
  • 0