Hardcoded IDs in code
Why is this an issue?
Salesforce record IDs are unique to each org. An ID that references a record in your development sandbox will not point to the same record (or any record at all) in production or another sandbox. Code containing hardcoded IDs will fail or behave unexpectedly when deployed to a different environment.
This issue commonly affects:
References to specific users, profiles, or permission sets
Queue IDs for case or lead assignment
Record type IDs
Custom metadata or custom setting records
Examples
Example of incorrect code in Apex:
public void assignToQueue(Case c) {
c.OwnerId = '00G5f000004XYZABC'; // Hardcoded queue ID
update c;
}
Example of correct code: Query for the record dynamically using a stable identifier like DeveloperName.
public void assignToQueue(Case c) {
Group queue = [SELECT Id FROM Group WHERE Type = 'Queue' AND DeveloperName = 'Support_Queue' LIMIT 1];
c.OwnerId = queue.Id;
update c;
}
Example of incorrect code in a SOQL query:
List<Account> accounts = [SELECT Id FROM Account WHERE RecordTypeId = '012000000000ABC'];
Example of correct code: Use Schema.SObjectType to retrieve IDs at runtime.
Id recordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName()
.get('Business_Account').getRecordTypeId();
List<Account> accounts = [SELECT Id FROM Account WHERE RecordTypeId = :recordTypeId];
Example of incorrect code in Aura JavaScript:
({
doInit: function(component) {
var userId = '0055f000003XYZABC'; // Hardcoded user ID
component.set('v.assignedTo', userId);
}
})
Example of correct code: Pass dynamic values from Apex or use Lightning Data Service.
({
doInit: function(component) {
var userId = $A.get('$SObjectType.CurrentUser.Id');
component.set('v.assignedTo', userId);
}
})
How can I fix violations?
Replace hardcoded IDs with one of these approaches:
Query by stable API identifiers: e.g. RecordType.DeveloperName, PermissionSet.Name, Group.DeveloperName (queues/public groups), and Profile.Name (profiles).
Use Schema methods: For record type IDs, use
Schema.SObjectType.<Object>.getRecordTypeInfosByDeveloperName().
Resources
