0

In a test class, I want to update Account information depending on the record type. The information to update comes from the Order object. I have created a test account with the according record type. In the test factory, the Account creation looks like this:

    public static Account createNewsletterRecipientAccount(){
       RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE 
          Name = 'Newsletter' and SObjectType = 'Account'];
       Account mockAccount = new Account();
       mockAccount.FirstName = 'Hans';
       mockAccount.AccountNumber = '1';
       mockAccount.LastName = 'Meier';
       mockAccount.EXT_Id__c = 'testExtId';
       mockAccount.RecordType = personAccountRecordType;
       mockAccount.RecordType.Name = 'Newsletter';
       return mockAccount;
}

In the test, I call the class as follows:

Account account = MockUpFactory.createNewsletterRecipientAccount();
insert account;

When I do a debug here, I do get the Record Type 'Newsletter' for the Account. Then I create the Orders for the test:

list <Order> listOrders = new list <Order>();
for(Integer i=0; i<10; i++){
  Order order = new Order(); 
  order.AccountId = account.Id;
  order.EffectiveDate = Date.today().addDays(-i);
  listOrders.add(order);
}
insert listOrders;

When I do a debug here for listOrders[0].Account.RecordTypeId, I get null as a result. How would I have to write the code so that I get a proper result for the RecordTypeId?

1

My assumption you need the record type Id for the Newsletter. And while creating an account you need to tag recordtypeid field

Check this:

Id recordTypeId =  [SELECT Id FROM RecordType WHERE 
          DeveloperName = 'Newsletter' and SObjectType = 'Account' LIMIT 1].id;

While creating a test account mention

mockAccount.RecordTypeId = recordTypeId;
  • Yes, I replaced these two lines: mockAccount.RecordType = personAccountRecordType; mockAccount.RecordType.Name = 'Newsletter'; with this line: mockAccount.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Newsletter Empfänger').getRecordTypeId(); THIS WORKED! – user40501 Aug 14 '19 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.