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
sfdcIssfdcIs 

return sObject method - record type question

I am buldiing a test class with sObjects instantiated inside a test factory.  I have another accountTest class that passes parameters to the test class which successfully created my account with buildTestAccountPostive().  However, in my negative test, I want to be able to update this same account with and set the record type to null. First of all, will SFDC let me update a account.recordtypeId = null and is there a way I can insert an Account succesfully without any recordtypeID?

 

Second question if yes, when I try to reference the newAccount1 with  Account negAccountRec =[select id, recordtypeid from Account where ID IN: newAccount1];     i keep getting newAccount1 variable does not exist.  Notice Account buildTestAccountPostive() is not static

 

public Account buildTestAccountPostive(Integer i)
{
Account newAccount1 = new Account();
newAccount1.Name = 'Test' + i;
//RT is hard-coded temporarily ill update to use a collection instead later
newAccount1.recordtypeid= '012f0000000ChWYAA0';
return newAccount1;
}

 

public static Account updateTestAccountPositivetoNegative(Integer i)
{
Account negAccountRec =[select id, recordtypeid from Account where ID IN: newAccount1];
negAccountRec.recordtypeid = null;
return negAccountRec;
}

crop1645crop1645

sfdcls:

 

1. The reason that newAccount1 is not found is because your second method doesn't declare it.  These routines could be much simpler such as the following:

 

public Account buildTestAccount(Integer i, Boolean isPosTest) {
  return new Account(Name = 'Test' + i,
                     recordTypeId = isPosTest ? '012f0000000ChWYAA0' : null);

 2. I believe that recordTypeId = null is the MasterRecordType for the SObject. It will only be settable if the SObject has at least one custom recordTYpe defined.

 

3. And, avoid hard coded recordTYpeIds -- use a SOQL Select to fetch the IDs and reference using the developerName field.  

 

BTW - I highly recommend the SFDC Workbench tool as a vehicle for trying out SOQL and inspecting your org - it is a good way to learn more about the SFDC 'system-y' SObjects. Also the API Reference Guide has full details on each SObject.