You need to sign in to do that
Don't have an account?
How to Query RecordTypes of Custom Objects in a Managed package
I ran into the following issue when testing code in a manage package. A test needed to get the Id's of the RecordTypes associated with different records in a custom object.
The sample code in SF is this:
List<RecordType> rtypes = [SELECT Name, id FROM RecordType WHERE sObjectType='Account' and isActive=true];
of course this works because Accont is a native object. But the following code fails in a managed package
List<RecordType> rtypes = [SELECT Name, id FROM RecordType WHERE sObjectType='MyObject__c' and isActive=true]; Map<String, String> accRecTypes = new Map<String, String>(); for(RecordType rt: rtypes){ accRecTypes.put(rt.name, rt.id); }
This fails because the custom object is appended with the Namespace so the sObjectType value should be MyNameSpace__MyObject__c. This is pain if you're moving from Dev, to Staging Orgs, Beta etc as you need to change the query each time. The solution is simple - leave out the sObjectType param in the Where clause and then get the types you want. The number of RecordTypes is generally small so its not a big deal.
List<RecordType> rtypes = [SELECT Name, id FROM RecordType WHERE isActive=true];
//now get the types you want
string MyType1Id = accRecTypes.get('My Record Type 1'); string MyType2Id = accRecTypes.get('My Record Type 2');
Hi
As you specified , you could have append the package namespace before the Sobject API name in the recordtype query.
Instead of querying and iterating over the loop to put that in the map for our later usage.
There is one more smart to fetch the record type id with out using soql query .
Here is the syntax:
Schema.Sobjecttype.Account.getRecordTypeInfosByName().get('RecordTypeLabel').getRecordTypeId();
Kindly let me know incase you are still facing the issue regarding this.
Regards
Mariappan Perumal