+ Start a Discussion

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

I'm looking for something like this:






Have a look at this page from the docs:




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.


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);





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


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.