3

I have recently found a way to create a Static Resource via the Metadata API - see this article.

So, I was curious if it was possible to not only create a static resource, but to either:

  • Edit the static resource directly
  • Delete and recreate the static resource

Use Case

The use case is for a testing framework I have been working on. I am implementing the following article here but, I would like to have a simple VisualForce interface to create and edit my instructions rather that editing the JSON string directly in Eclipse (mainly because I could make a mistake).

So essentially, I'd like to:

  • Retrieve the JSON string from within the static resource and deserialize it.
  • Once I am done altering it, serialize it and update the static resource.

If this is doable, I'm curious how it could be done. Any help would be most appreciated.

4

The metadata api is perhaps more than needed here. I would suggest using the ajax tookit to accomplish this from the platform.

<script>
    sforce.connection.query(
        "SELECT Id, Body FROM StaticResource WHERE Name = 'TheOneIWantToEdit'", 
        { onSuccess : handleUpdate });


    function handleUpdate(result) {
        result = result.getArray('records');
        result[0].Body += 'alert("Hello World!");';
        sforce.connection.update(result);
    }
</script>
9

This is most certainly possible...

Create Static Resource

    MetadataService.MetadataPort service = createService();     
    MetadataService.StaticResource staticResource = new MetadataService.StaticResource();
    staticResource.fullName = 'test';
    staticResource.contentType = 'text';
    staticResource.cacheControl = 'public';
    staticResource.content = EncodingUtil.base64Encode(Blob.valueOf('Static stuff'));
    MetadataService.AsyncResult[] results = service.create(new List<MetadataService.Metadata> { staticResource });

Update Static Resource

    MetadataService.MetadataPort service = createService();     
    MetadataService.StaticResource staticResource = new MetadataService.StaticResource();
    staticResource.fullName = 'test';
    staticResource.contentType = 'text';
    staticResource.cacheControl = 'public';
    staticResource.content = EncodingUtil.base64Encode(Blob.valueOf('Static stuff Changed'));
    MetadataService.UpdateMetadata ut = new MetadataService.UpdateMetadata();
    ut.currentName='test';
    ut.metadata= staticResource;
    MetadataService.AsyncResult[] results = service.updateMetadata(new List<MetadataService.updateMetadata> {ut});  

Delete Static Resource

    MetadataService.MetadataPort service = createService();     
    MetadataService.StaticResource staticResource = new MetadataService.StaticResource();
    staticResource.fullName = 'test';
    staticResource.contentType = 'text';
    staticResource.cacheControl = 'public';
    MetadataService.AsyncResult[] results = service.deleteMetadata(new List<MetadataService.Metadata> { staticResource });      

Please take care to review the use of the AsyncResult here. From Spring'14 onwards (in a few weeks), there will be some new operations on the Metadata API which will make doing this kind of thing much easier without having to worry about the AsyncResult.

4
  • I cannot wait to incorporate this is the coming months - when I have a chance to pursue this. This will definitely make my testing a tad easier once my idea comes to fruition. Feb 26 '14 at 15:52
  • Andrew Fawcett- This is awesome but which library or package are you using to implement this. I don't you can use the MetaData API natively. Thanks!
    – user5428
    Feb 28 '14 at 18:34
  • It is this one, github.com/financialforcedev/apex-mdapi, it is an Apex wrapper around the Salesforce Metadata Web Service API. Feb 28 '14 at 22:57
  • In case it saves anyone else some time, I just tried upsertMetadata using the current MetadataService class on an existing static resource and got "Error: message=That Static Resource name is already in use, please choose a different one. statusCode=DUPLICATE_DEVELOPER_NAME fields=(Name)" - not sure why. So I guess I'll need to use delete/insert or upsert/update instead.
    – Keith C
    Jun 3 '15 at 11:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.