0

So Im making an Apex class that I want to push to production that will change the record type of an object, to a different record type.

Here is the class below:

public class removeclassy {
List<Campaign> camplist = [SELECT Name,id FROM Campaign WHERE RecordTypeid = '01215000001YqO9AAK'];
 public void DestroyCampClassy()
    {
        for(Campaign camp : camplist)
    {
        camp.RecordTypeId ='01215000001DWqoAAG';
        if(camp.RecordTypeId == '01215000001DWqoAAG')
        {
            System.debug('record type changed on this record: '+ camp.Name);
            update camp;
        }
    }
        System.debug('Loop finished');
    }
}

And here is the Test method that i want to pair with it.

@IsTest static void testCamp()
    {
        Campaign camp = new Campaign();
        camp.Name = 'test';
        camp.RecordTypeId = '01215000001YqO9AAK';
        Test.startTest();
        insert camp;
        Test.stopTest();
        removeclassy rem = new removeclassy();
        rem.DestroyCampClassy();
        system.assertEquals('01215000001DWqoAAG', camp.RecordTypeId);
        
    }

The problem that I am encountering is that the test object that I create doesn't get its record type changed when I call the class. The class itself does an SOQL query that should get all the objects with that objects specific record type in order to change it to another. But for some reason the DestroyCampClassy method does not change this. Any thoughts?

3
  • It is possible that recordtype ids are different in production and sandbox. Can you conform that those are same? It is advised to use an record type api name, and get an ID based on that
    – kurunve
    Commented Jan 12, 2021 at 17:35
  • Side note: You are performing DML inside of a loop. That's bad (DML and queries should practically never be run in a loop, as that's a good way to run into governor limits). You should either use another list to store records you want to update, or just update camplist;. Using an implementation of the Unit of Work pattern (like fflib_SObjectUnitOfWork, trailhead module here) is another option, but don't feel a need to drop everything and start using that now.
    – Derek F
    Commented Jan 12, 2021 at 18:16
  • @kurunve They are the same but out of curiosity, is it possible to test for recordtype Ids that are in production but not in sandbox? Commented Jan 13, 2021 at 16:41

1 Answer 1

1

As I explain in depth in this answer, the state of the record in memory has nothing to do with the state of the record in the database. When you perform an operation in a trigger, and you're testing this in your class, you need to remember to query the records back afterwards to check for changes.

    Campaign camp = new Campaign();
    camp.Name = 'test';
    camp.RecordTypeId = '01215000001YqO9AAK';
    Test.startTest();
    insert camp;
    Test.stopTest();
    removeclassy rem = new removeclassy();
    rem.DestroyCampClassy();
    camp = [SELECT RecordTypeId FROM Campaign WHERE Id = :camp.Id];
    system.assertEquals('01215000001DWqoAAG', camp.RecordTypeId);
    

P.S. You should use the DescribeSObjectResult class to get the Record Type Id dynamically by name instead of hard-coding ID values. This will cause you trouble in the future.

1
  • This worked, thanks! Commented Jan 13, 2021 at 16:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .