3

How do I query default values for a record type?

I'm unsure of which namespaces to use in my apex for record type default values aswell.

7
  • 1
    Are you looking for the default record type for a specific profile?
    – David Reed
    Jul 13, 2018 at 12:25
  • You might want to take a look at the Schema namespace. Particularly the DescribeFieldResult class. I don't think you can do what you want, though. Jul 13, 2018 at 14:07
  • 1
    The metadata API is one way of getting what you're looking for, esp the MDAPI. Otherwise, realistically, you're looking at burning a SOQL query to actually get what you're looking for. This question has been asked a number of times and currently there is NO good way to provided by the platform to easily get picklist values by record type without a query of some kind.
    – crmprogdev
    Jul 13, 2018 at 14:21
  • @DavidReed I'm looking for the picklist fields that have a default value given a record type. So I only want results to show me a value if there is a default.
    – rna7
    Jul 16, 2018 at 9:42
  • @RenatoOliveira I think you are right, its not possible.
    – rna7
    Jul 16, 2018 at 9:43

3 Answers 3

5

If you know the record type ID, you can initialize a record with default picklist values intact, like so:

Account a = (Account)Account.sObjectType.newSObject(recordTypeId,true);

If you don't know the record type ID yet, you can review the map:

Map<String,RecordTypeInfo> infos = Account.sObjectType.getDescribe()
    .getRecordTypeInfosByDeveloperName();

And use .isDefaultRecordTypeMapping() to check if each returned RT is the default for this user.

1
  • This one Saved me.
    – bharath
    May 18, 2022 at 12:54
0

To provide more a concrete answer to the additional questions made in comments, you cannot query the metadata API directly from Apex for record types unless you're querying for Custom Metadata or using the apex-mdapi. The latter is the approach I would recommend.

There are built-in queries in the mdapi for record types. You'll need to download the repository and play with it. Once you do, you'll begin to understand how it can be used to obtain what you're looking for; either by object or by record type and namespace as well. I can't provide you with a specific recommendation on exactly how to approach obtaining the results you're looking for without more details.

Once you've played with the mdapi, come back and ask more specific questions should you discover you can't get the results you need.

0

Although not efficient, you could create a dummy record and query its record type after it is created.

Contact cont = new Contact(LastName = 'TestLast');
insert cont;
Id defaultRtId = [SELECT RecordTypeId FROM Contact WHERE Id = :cont.Id].RecordTypeId;

Record Type defaults vary base on user profiles, so you could use System.runAs() to test a specific user and their profile.

You must log in to answer this question.

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