3

In a test class, I need to have 2 contacts with different record types.

Contact Record Types: Client_Only and Candidate.

Account Contact is Client only, and Applicant is Candidate.

Line 8 is throwing the exception:

Id con = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('C‌​lient_Only').getReco‌​rdTypeId(); 

Full Test:

@isTest
public class AP109_JobBoardApplication_TEST
{
    static testMethod void newJobBoardApplication_TEST(){
        User u1 = Utils_Methods.UserTest();
        Account a1 = TestUtils.createAccount('compte test');

        Id con = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Client_Only').getRecordTypeId();
        Contact c1 = TestUtils.createContact(new Contact (accountId=a1.Id,Lastname = 'Payne', Salutation = 'Mr.', Firstname = 'Max', Title = 'Cop', recordtypeid=con));

        Id can = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Candidate').getRecordTypeId();
        Contact c2 = TestUtils.createContact(new Contact (Lastname = 'Smith', 
                                                                          Salutation = 'Mr.', 
                                                                          Firstname = 'Will', 
                                                                          Title = 'Gen', 
                                                                          Email = 'abc@axyz.com',
                                                                          TR1__Candidate_Status__c = 'Prospect',
                                                                          Nationality__c = 'Australia',
                                                                          TR1__Primary_Background__c = 'Internal audit',
                                                                          WorkPermitforForeigners__c = 'Confirmed', recordtypeid=can));

        insert c1;
        insert c2;
        a1.OwnerId = u1.Id;


        TR1__Job__c job = new TR1__Job__c(  
                                            TR1__Account__c = a1.id,
                                            Name = 'Accounting Test Job',
                                            TR1__Contact__c = c1.id,
                                            Chatter_Team__c = 'BAT Users',
                                            TR1__Salary_High__c = 100.20,
                                            TR1__Fee_Percentage__c = 10000.00,
                                            TR1__Division__c = 'Advisory',
                                            TR1__Level__c = 'Executive',
                                            TR1__Primary_Background__c = 'Internal audit',
                                            TR1__Primary_Matching_Requirement__c = 'Sample Test',
                                            TR1__Estimated_Start_Date__c = System.today(),
                                            TR1__State_Area__c = 'Denmark',
                                            TR1__Regional_Area__c = 'Midtjylland',
                                            TR1__City__c = 'TestCity',
                                            TR1__External_Job_Title__c = 'Software Engineer',
                                            TR1__Status__c = 'Open',
                                            TR1__Open_Date__c = System.Today(),
                                            TR1__Management_Experience__c='2',
                                            PostingStartDate__c = System.Today(),
                                            ExpirationDate__c = System.Today(),
                                            TR1__Client_Description__c = 'Posted Client Description'
                                        );
        insert job;

        TR1__Job_Board_Application__c jobBoardApplication = new TR1__Job_Board_Application__c(
                                                                                              TR1__Job__c = job.Id,
                                                                                              TR1__Applicant__c=c2.Id
                                                                                              );
        database.insert(jobBoardApplication);

        jobBoardApplication.TR1__Applicant__c=c2 .Id;
        try{
        database.update(jobBoardApplication);}catch(exception e){}

    }  

}
7
  • Which line throws the null pointer?
    – Adrian Larson
    Jun 26, 2017 at 15:53
  • @AdrianLarson Line no. 8; Id con = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Client_Only').getRecordTypeId(); Jun 26, 2017 at 15:56
  • 2
    You're using getRecordTypeInfosByName, Client_Only looks like the developer name to me
    – EricSSH
    Jun 26, 2017 at 16:00
  • I changed the Client_Only to its label, 'Client', and now a validation rule is blocking it. That Account. Contact which is c1 should be of client record type only Jun 26, 2017 at 16:06
  • @RichaUpadhyay In other words, problem solved. A validation rule blocking data insertion is a completely different issue, and should be addressed via a separate question.
    – Adrian Larson
    Jun 26, 2017 at 16:13

4 Answers 4

6

The map returned by getRecordTypeInfosByName maps by Name, not DeveloperName. The value 'Client_Only' seems highly likely to be the latter, as this field does not allow spaces. If you want to retrieve this RecordType, you must specify the proper Name value, likely 'Client Only'.

I find it preferable to use unique fields, so I would just query by DeveloperName:

RecordType clientOnlyRecordType = [
    SELECT Id FROM RecordType WHERE DeveloperName = 'Client_Only'
];
Id recordTypeId = (clientOnlyRecordType != null) ? clientOnlyRecordType.Id : null;

For completeness I should mention that since this idea was delivered, there is no longer a need to query to retrieve RecordType by DevleloperName. The DescribeSObjectResult class now has a getRecordTypeInfosByDeveloperName() method.

getRecordTypeInfosByDeveloperName()
Returns a map that matches developer names to their associated record type. The current user is not required to have access to a record type to see it in this map.

You would use it as follows:

Id recordTypeId = SObjectType.Contact.getRecordTypeInfosByDeveloperName()
    .get('Client_Only').getRecordTypeId();
0
3
Id con = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Client_Only').getRecordTypeId();

If recordtype name is incorrect then Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Client_Only') will be null and hence accessing getRecordTypeId() will throw expection.

0

Schema.SObjectType.Contact.getRecordTypeInfosByName() returns map with labels as keys, not with the developer name, I believe this is the problem, as you might have Client Only as label of record type.

So try it like,

Id con = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('C‌​lient Only').getReco‌​rdTypeId();
0

try with

Id recordTypeId =   Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Wholesale_Partner').getRecordTypeId();
2
  • 3
    I don't see any indication that the poster has a record type called Wholesale_Partner. Can you clarify how this answers the question?
    – David Reed
    Apr 23, 2019 at 15:59
  • 1
    Welcome to SFSE! Please take the tour and read How to Answer. Right now, your answer is what we call a "code dump". It's almost all code, and no explanation. While your answer does appear correct (aside from the Wholesale_Partner thing already mentioned), this answer would be improved and useful to a wider audience if you explain yourself. What's different about your approach compared to the others presented so far? When did this approach become possible? What was the initial problem, and how does this solve it? You can edit your answer to include additional information.
    – Derek F
    Apr 23, 2019 at 16:38

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.