6

Is there a way to create a StaticResource within a Apex code.

this the code i am doing:

        StaticResource sr = new StaticResource();
        sr.Body = file;
        sr.Name = 'testLicence';
        sr.ContentType = 'zip';
        sr.CacheControl = 'Public';

        sr.create();

but i am getting this error:

Method does not exist or incorrect signature: [StaticResource].create

and when i consulted the documentation in this link: StaticResource Documentation

it says that the create() method is supported.

What's wrong with my code ?

1
  • It would be insert sr; from Apex code but that will not work as direct modification from Apex code is explicitly disallowed. That leaves you with having to make a web service call as Amit describes. Be very sure that this is really necessary before choosing to do it; you will be dragging a lot of code into your org.
    – Keith C
    Aug 14, 2015 at 12:15

2 Answers 2

10

You will have to go with MetaData API. Something as below -

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

Remember that each create call corresponds to a callout. Here is a brilliant repository on Metadata API that can help you out.

https://github.com/financialforcedev/apex-mdapi

3
  • Thanks for the answer but how can i add apex-mdapi to salesforce. i am using a Sandbox.
    – ihssan
    Aug 14, 2015 at 12:30
  • Either download zip and deply to your sandbox or manually copy the classes from SRC and paste. it works for me in my DEV org. Aug 14, 2015 at 12:34
  • @AmitBangad Do you know if it is possible to create a static resource with multiple files as content? With the above method you can create a Static Resource with a single file as content. I can't figure out how to write the content of multiple files into a single static resource with the MetadataService (or anything else) from Apex.
    – lemming
    Jun 28, 2021 at 9:26
2
public class CreateStaticResource{
public static void createStatic(){
    MetadataService.MetadataPort service = createService();     
    MetadataService.StaticResource staticResource = new MetadataService.StaticResource();
    staticResource.fullName = 'MyResource';
    staticResource.contentType = 'text';
    staticResource.cacheControl = 'public';
    staticResource.content = EncodingUtil.base64Encode(Blob.valueOf('Static stuff'));
    MetadataService.SaveResult[] results = service.createMetadata(new List<MetadataService.Metadata> { staticResource });

}
private static MetadataService.MetadataPort createService(){ 
    MetadataService.MetadataPort service = new MetadataService.MetadataPort();
    service.SessionHeader = new MetadataService.SessionHeader_element();
    service.SessionHeader.sessionId = UserInfo.getSessionId();
    return service;     
}}

You must log in to answer this question.

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