Domanda

I have a custom object into which I want to add a new item and assign ownership of it to a user, which has been input via user input. The record type does not default to the appropriate value, based on ownership, so I need to return the default record type for the specified user, so I can create my item correctly.

I can get the default record type for the object easily enough, but how do I get it for any user?

È stato utile?

Soluzione

The default record type is only defined at the profile (and therefore user) level, not for the whole org. So, whichever user is describing the object will see their own defaultRecordTypeMapping defined for their Profile. For example, if User A executes this code in Apex:

for (Schema.RecordTypeInfo rtInfo : Account.SObjectType.getDescribe().getRecordTypeInfos()) {
  if (rtInfo.isDefaultRecordTypeMapping()) {
    System.debug(rtInfo);
  }
}

The default record type for User A will be printed. If User B were to run the same code and was assigned to a different profile with a different default record type, that user's default record type would be printed.

The same is true through the API with describeSObject() for SOAP or /services/data/v27.0/sobjects/:object/describe for REST.

Altri suggerimenti

You could get recordId by developer name, see release info

Try to use this instead

Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Master');
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top