4

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 ?

  • 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 '15 at 12:15
7

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

| improve this answer | |
  • Thanks for the answer but how can i add apex-mdapi to salesforce. i am using a Sandbox. – ihssan Aug 14 '15 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. – Amit Bangad Aug 14 '15 at 12:34
0
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;     
}}
| improve this answer | |

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.