I have some Custom Settings and Named credentials configured since I make call outs to external systems. But every time I refresh any of my sandbox, I have to manually update the Custom Settings and Named Credentials. I tried to create a Sandbox template but couldn't find a way to exclude Named Credentials and Custom Settings there. Also, thought of writing an APEX class which I can execute after refresh but I think Custom Settings can be updated via APEX but since Named Credentials is a read only object, I didn't find this option much suitable as well. Could someone advise what is the best practice to handle this situation?

Thanks in advance!

  • See Mohith's answer - you use the Apex wrapper on the Metadata API (available in GitHub) and update the NamedCredential metadata resource. You don't use normal Apex DML, metadata API is done via callout
    – cropredy
    Commented Dec 17, 2017 at 6:11

1 Answer 1


One of the approaches I can think of is to Use Salesforce metadata API wrapper like one maintained by Financial Force to implement logic to update named credentials .

You will need to put the logic inside class implementing SandboxPostCopy interface

global class UpdateNamedCredential implements SandboxPostCopy {
   global void runApexClass(SandboxContext context) {
      System.debug('Hello Tester Pester ' + context.organizationId()
       + ' ' + context.sandboxId() + context.sandboxName());
      // Insert logic here to update named credential

  • 1
    Mohit - thanks for the details. I tried this approach as well but the challenge I am facing is how to update Named Credentials using APEX. You can reference Named Credentials in callouts but I am not aware of any apex code which can query and update the Named Credentials. Commented Dec 17, 2017 at 1:56
  • You will need to import financial force wrapper for metadata api and that would allow you to edit the named credential . Commented Dec 17, 2017 at 9:38
  • can you share sample code for reference? Commented Feb 15, 2023 at 20:43

You must log in to answer this question.

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