Skip to main content
Hi all,

I have been extensively using Custom Metadata Types in our org for a while now for the many obvious benefits over Custom Settings, but have run into an issue when it comes to instancing sandboxes.

One of our Custom Metadata Types contains endpoint information and transaction keys for external systems. When we forget to change these settings in a newly refreshed sandbox, we can end up with some problems due to requests from test data being sent to the production server instead of the development server.

I have tried to automate a fix to this with a SandboxPostCopy script. Custom Settings are easily updated in this way, but Custom Metadata Types not so much. I have some code that, theoretically, should do what I need. I have confirmed that the code works by running it anonymously without the sandbox context condition (though other parts of my script use the same condition and work fine). It seems that, inexplicably, metadata deployments will simply not run from a SandboxPostCopy script.

The code below is the bit that runs the metadata deployment. Some values are sanitized, but other than that, this is exactly what I have - and I can confirm that the script is running on sandbox creation because I also have it doing some Custom Settings updates successfully.

 

Metadata.CustomMetadata ecasConnection = new Metadata.CustomMetadata();

ecasConnection.fullName = 'Connection_Setting__mdt.ECAS';

ecasConnection.label = 'ECAS';

if (context.sandboxName().equalsIgnoreCase('UATFull'))

{

Metadata.CustomMetadataValue endpointRoot = new Metadata.CustomMetadataValue();

endpointRoot.field = 'Endpoint_Root__c';

endpointRoot.value = '.../ECAS/salesforce';

ecasConnection.values.add(endpointRoot);

}

else

{

Metadata.CustomMetadataValue transactionKey = new Metadata.CustomMetadataValue();

transactionKey.field = 'Transaction_Key__c';

transactionKey.value = 'void';

ecasConnection.values.add(transactionKey);

}

Metadata.DeployContainer metadataContainer = new Metadata.DeployContainer();

metadataContainer.addMetadata(ecasConnection);

if (!Test.isRunningTest())

Metadata.Operations.enqueueDeployment(metadataContainer, null);

What it should be doing is switch our UAT sandboxes endpoint to point to our UAT server on refresh, or void the transaction key in any other sandbox, causing any requests to be rejected outright.

So, the question is, what am I missing? Can you intentionally not do a metadata deployment out of a SandboxPostCopy script? I can think of some other workarounds, such as having additional metadata records and extra logic in my code to grab a different record depending on the instance, but i'd rather introduce as little uneeded complexity as I can. I could also revert to using custom settings instead, but that seems like a step backwards to something that is largely inferior in 'most' situations.

Thanks!

-Brian
5 个回答
0/9000