I have a method in APEX class that does variety of checks via If else conditions with some SOQL statements. I created test data(Accounts, custom object records) to test the SOQL statements and it seems to go though fine. The place where my test class throws a null exception is when in my Apex code a new opportunity is created. For SOQL statements, I know we setup the test data before and then call the method that we need to test, but what about the case where my code is actually creating a new opportunity on real-time. To my knowledge, I don't need to setup anything for that, since it is a new record creation.

Here is the code inside my apex method that is causing trouble. It is fetching the record type id first that needs to be assigned to the new opportunity.

     Id OpportunityRecordTypeId = 

     Opportunity newopportunity = new Opportunity();
                                newopportunity.StageName= 'Prospecting';
                                newopportunity.RecordTypeID= OpportunityRecordTypeId;
                                newopportunity.AccountId= contact.AccountId;
                                newopportunity.Name='Final Test';

                                insert newopportunity;

It seems to throw the

System.NullPointerException: Attempt to de-reference a null object

error on the line where it fetches the record type id. How do I cater this my TEST class?


  • 1
    What is the value of marketing.Lead_Type? Is it null here? Is it really a record type name rather than a developer name? – David Reed May 28 '19 at 15:37
  • marketing.Lead_Type is actually a custom picklist. The record type of opportunity should be whatever the value of marketing.Lead_Type is. – Student May 28 '19 at 15:49
  • I think I have fixed it by providing the value for leadType picklist in my setup data.That error seems to have gone away, now I am getting some other issue. Thanks for the help. – Student May 28 '19 at 16:12

There are a couple things you should change about your retrieval of this record type.

  • Do not call getGlobalDescribe, as there is no need for dynamic retrieval of the DescribeSObjectResult for Opportunity.
  • Verify that you get a result before calling getRecordTypeId

What it should look like instead:

DescribeSObjectResult describe = SObjectType.Opportunity;
RecordTypeInfo info = describe.getRecordTypeInfosByName().get(marketing.Lead_Type);
Id recordTypeId = (info == null) ? null : info.getRecordTypeId();

If you are able to map by DeveloperName instead, that would also be preferable.

  • Looks like you beat me by a couple of min! – PartOfTheOhana May 28 '19 at 15:44

Check your RT name. I don't think it's really marketing.Lead_Type It's proably something else... Also, Lead_Type looks more like a Deveoper_Name rather than a Name (the underscore is the indicator).

If you're Using DeveloperName, do something like this:

Id oppRTId = Opportunity.sObjectType.getDescribe().getRecordTypeInfosByDeveloperName(DEVELOPER_NAME_OF_THE_RT).getRecordTypeId();

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.