I have a custom setting where I store Id's for my dock records. (Dock__c)

In my test class after "creating" the custom setting and my other records, how can I reference the custom setting to match my dock record? How can I set an ID of the dock record in my custom setting for the test class?

static void testDocks(){
    shipment__c shipment = DataStorage.insertShipment('Shipment1');
    insert shipment;
    shipment.0f_package__c =0;
    update shipment;
    Package__c p = DataStorage.makePackage('p1');
    insert p1;

   LocationSets__c ls = new LocationSets__c
   LocationSets.Name ='ls test';
   LocationSets.Dock1__c =(it is a text field to hold the ID of the 
   actual dock1__c); 
   insert ls;
    p1.Status__c = 'Ready to Ship';
    p1.Shipment__c = shipment.id;
    p1.Dock__c = LocationSets.dock1__c;
    update p1;
    shipment = [SELECT ID, Status__c FROM shipment__c WHERE ID = :shipment.id];
        System.assertEquals('Ready', shipment.Status__c);

1 Answer 1


Custom Settings are treated as data (as opposed to metadata) in test context. That means you create and update them just like you do with data in your unit tests and @testSetup methods, and your changes are rolled back at the conclusion of the test - they don't affect your real Custom Settings.

This is distinct from Custom Metadata records, which you can't change in test context; for those records, you see your org's real data, because they are considered metadata.

What this boils down to is you create you Custom Setting record just like any other sObject. You'd create some Dock__c

Dock__c d = new Dock__c(Name = 'Wharf', // or 'Worf' or 'Whorf' 
                        Location = 'Test Location',
                        ... // etc
insert d;

and then populate its Id in your Custom Settings.

LocationSets.Dock1__c = d.Id;
// populate other fields on LocationSets
insert LocationSets
  • Actually I had written the code wrong, p1.Dock__c should = LocationSets.DOck1__c as there is no var.dock1__c.
    – J.koh
    Aug 27, 2018 at 2:43
  • I've updated my answer, but the principle is the same.
    – David Reed
    Aug 27, 2018 at 2:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .