1

I am trying to automate the set up of some of my lower sandboxes.

In my SandboxPostCopy class, I'm using the FinancialForce MetadataAPI wrapper MetadataService to query and upsert a certificate. (Update the prod cert to be the development cert).

In a dev sandbox where the CA-Signed certificate has already been uploaded, I have downloaded the Certificate (.CRT) and saved it as a static resource into the other lower environment.

I can query the cert via the metadata api and get the record and can update the master label and can even update the Certificate.Content to be the string export of the content of the existing Certificate.Content, but when I attempt to update the content to the exportedCRT.body resource I get a: "Couldn't find a certificate chain within the uploaded content" error response.

Is what I'm trying to do not possible? Any recommendations on how to automate this process?

Code:

StaticResource exportedCRT = [SELECT id, Body FROM StaticResource WHERE name = 'exportedCRT'];
system.debug(exportedCRT.body);

MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();
MetadataService.Certificate myExistingProdCert = (MetadataService.Certificate)service.readMetadata('Certificate', new String[] {'myExistingProdCert'}).getRecords()[0];
system.debug(myExistingProdCert.content);

myExistingProdCert.content = String.valueOf(exportedCRT.body);

myExistingProdCert.masterLabel = 'convertedToDevCert';

List <MetadataService.UpsertResult> results = service.upsertMetadata(new MetadataService.Metadata[] {myExistingProdCert});

system.debug(results);

Salesforce Certificate Metadata API Documentation: https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_certificate.htm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.