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
fgwarb_devfgwarb_dev 

Apex - How do you determine the default record type for a user?

I'm looking for something like this:

Schema.CurrentUser.Opportunity.getDefaultRecordType();

 

Thanks!

 

bob_buzzardbob_buzzard

Have a look at this page from the docs:

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_sobject_describe.htm

 

At the end there's a section on recordTypeInfos.  I think you can use the isDefaultRecordTypeMapping method.  Its not 100%  as the docs don't mention the current user, but I seem to recall I've read somewhere that this is the mechanism.

fgwarb_devfgwarb_dev

We're running in system mode, so I need some way to explictly associate the recordtype to the user.  Like on the Profile...

 

 

 

User u = [SELECT Id, ProfileId FROM User WHERE Id = :UserInfo.getId()];
ProfileRecordTypeTable prtt = [SELECT Id, RecordTypeId FROM ProfileRecordTypeTable WHERE sObjectType = 'Opportunity' AND IsDefault = TRUE];

system.debug('This is the RT i need to use for this user:'+prtt.RecordTypeId);

 

 

 

bob_buzzardbob_buzzard

I haven't found any way to get at that information programmatically I'm afraid.

bob_buzzardbob_buzzard

Mind you, I don't think that running in system mode should be a problem as long as your code is being executed as the logged in user (i.e. not scheduled apex or incoming web services).

Jhon Cortes GasparJhon Cortes Gaspar
Only for update:

For default you could use "Master".The default record type is defined at the profile level. So, whichever user is describing the object will see their own defaultRecordTypeMapping defined for their Profile. 

You could get recordId by developer name, see relase info https://releasenotes.docs.salesforce.com/en-us/summer18/release-notes/rn_apex_developer_name.htm

The if two different user with diferent profile run same code, the will see their own default record respectively.
 
Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Master');

 
@jpmaria@jpmaria
https://salesforce.stackexchange.com/questions/3311/find-out-the-default-record-type-of-any-object-in-apex