Toggle Side Panel

  • Home
  • Articles
    • All Articles
    • Blogs
    • Videos
    • Infographics
  • Consultants
    • Salesforce Product Expertise
      • Top Salesforce ConsultantsTop Salesforce Consultants
      • Marketing Cloud ConsultantsMarketing Cloud Consultants
      • Service Cloud ConsultantsService Cloud Consultants
      • Experience Cloud ConsultantsExperience Cloud Consultants
      • Analytics Cloud ConsultantsAnalytics Cloud Consultants
    • Salesforce Industry Expertise
      • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
      • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
      • Health Cloud ConsultantsHealth Cloud Consultants
      • Commerce Cloud ConsultantsCommerce Cloud Consultants
      • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
    • Salesforce Experts by Location
      • USATop Salesforce Consultants in USA
      • IndiaTop Salesforce Consultants in India
      • AustraliaTop Salesforce Consultants in Australia
      • United KingdomTop Salesforce Consultants in UK
      • CanadaTop Salesforce Consultants in Canada
  • Webinars
  • Marketplace
  • Advertise With Us
  • Contact Us
  • Discussions
More options
    Sign in Sign up
    • Home
    • Articles
      • All Articles
      • Blogs
      • Videos
      • Infographics
    • Consultants
      • Salesforce Product Expertise
        • Top Salesforce ConsultantsTop Salesforce Consultants
        • Marketing Cloud ConsultantsMarketing Cloud Consultants
        • Service Cloud ConsultantsService Cloud Consultants
        • Experience Cloud ConsultantsExperience Cloud Consultants
        • Analytics Cloud ConsultantsAnalytics Cloud Consultants
      • Salesforce Industry Expertise
        • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
        • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
        • Health Cloud ConsultantsHealth Cloud Consultants
        • Commerce Cloud ConsultantsCommerce Cloud Consultants
        • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
      • Salesforce Experts by Location
        • USATop Salesforce Consultants in USA
        • IndiaTop Salesforce Consultants in India
        • AustraliaTop Salesforce Consultants in Australia
        • United KingdomTop Salesforce Consultants in UK
        • CanadaTop Salesforce Consultants in Canada
    • Webinars
    • Marketplace
    • Advertise With Us
    • Contact Us
    • Discussions
    Close search

    Activity › Forums › Salesforce® Discussions › Can I Insert a Static Resource using Apex Code in Salesforce?

    Tagged: DML Operation, Metadata WSDL, Salesforce Apex, Salesforce SOQL, Static Resource, Tooling WSDL

    • Salesforce® Discussions

      Can I Insert a Static Resource using Apex Code in Salesforce?

      Posted by Ajit on April 14, 2016 at 8:28 AM

      Can I Insert a static resource using apex code in Salesforce?

      Sourabh replied 9 years, 1 month ago 5 Members · 6 Replies
      • DML Operation
      • Metadata WSDL
      • Salesforce Apex
      • Salesforce SOQL
      • Static Resource
      • Tooling WSDL
    • 6 Replies
    • Himanshu

      Member
      April 14, 2016 at 8:29 AM

      Hi Ajit,

      No,Actually you can't perform DML operations on static resources so it is not possible, if you try the below code then it will throw an error that DML operations are not allowed on Static resources.

      string str;
      StaticResource st = new StaticResource();
      st.body=blob.toPdf(str);
      st.name='abcd';
      insert st;

    • Satyakam

      Member
      April 14, 2016 at 8:34 AM

      hii ajit,

      if you have uploaded any .zip file through static resource then you may use on vf page by following code:-

      <apex:page sidebar="false" showHeader="false" standardStyleSheets="false" >
      <apex:image url="{!URLFOR($Resource.CSSImage, 'images/bottom.gif')}" />
      </apex:page>

    • Abhinav

      Member
      April 14, 2016 at 8:35 AM

      But looks like we can insert the static resource through API. Because static resource is present in the metadata WSDL. So, i guess we can insert from API.

      Below are 2 static resources classes :-

      <xsd:complexType name="StaticResource">
      <xsd:complexContent>
      <xsd:extension base="tns:MetadataWithContent">
      <xsd:sequence>
      <xsd:element name="cacheControl" type="tns:StaticResourceCacheControl"/>
      <xsd:element name="contentType" type="xsd:string"/>
      <xsd:element name="description" minOccurs="0" type="xsd:string"/>
      </xsd:sequence>
      </xsd:extension>

       

      </xsd:complexContent>
      </xsd:complexType>
      <xsd:simpleType name="StaticResourceCacheControl">
      <xsd:restriction base="xsd:string">
      <xsd:enumeration value="Private"/>
      <xsd:enumeration value="Public"/>
      </xsd:restriction>
      </xsd:simpleType>

       

    • Abhinav

      Member
      April 14, 2016 at 8:37 AM

      It is also there in Tooling WSDL :-

      Please see the class below-

      <xsd:complexType name="StaticResource">
      <xsd:complexContent>
      <xsd:extension base="tns:sObject">
      <xsd:sequence>
      <xsd:element name="Body" minOccurs="0" type="xsd:base64Binary" nillable="true"/>
      <xsd:element name="BodyLength" minOccurs="0" type="xsd:int" nillable="true"/>
      <xsd:element name="CacheControl" minOccurs="0" type="xsd:string" nillable="true"/>
      <xsd:element name="ContentType" minOccurs="0" type="xsd:string" nillable="true"/>
      <xsd:element name="CreatedBy" minOccurs="0" type="tns:User" nillable="true"/>
      <xsd:element name="CreatedById" minOccurs="0" type="tns:ID" nillable="true"/>
      <xsd:element name="CreatedDate" minOccurs="0" type="xsd:dateTime" nillable="true"/>
      <xsd:element name="Description" minOccurs="0" type="xsd:string" nillable="true"/>
      <xsd:element name="LastModifiedBy" minOccurs="0" type="tns:User" nillable="true"/>
      <xsd:element name="LastModifiedById" minOccurs="0" type="tns:ID" nillable="true"/>
      <xsd:element name="LastModifiedDate" minOccurs="0" type="xsd:dateTime" nillable="true"/>
      <xsd:element name="Name" minOccurs="0" type="xsd:string" nillable="true"/>
      <xsd:element name="NamespacePrefix" minOccurs="0" type="xsd:string" nillable="true"/>
      <xsd:element name="SystemModstamp" minOccurs="0" type="xsd:dateTime" nillable="true"/>
      </xsd:sequence>
      </xsd:extension>
      </xsd:complexContent>
      </xsd:complexType>

    • Ajit

      Member
      April 14, 2016 at 8:39 AM

      Thanx Abhinav, but when i performed the insert operation in my code then it was throwing the error that DML not allowed.

    • Sourabh

      Member
      November 7, 2016 at 10:28 AM

      Hi Ajit,

      Metadata Api can help you out for creating static resource, Do someting like this:-

      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 });

      Thanks

    Log In to reply.

    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me

    application solution

    Popular Salesforce Blogs

    Back Up Metadata

    Back Up Metadata to Secure and Restore Your Customizations | Salesforce Guide

    Blog in Salesforce

    You can protect your organization’s customizations, like customized fields, custom Apex code (Everything you wrote in your console), page layouts, reports, and permission sets, by…

    API, AppExchange, AppExchange Packages, Augment Package, Back Up Metadata
    MOHIT Oct 15, 2020
    5,784  Views

    Breaking Down the Basics of Salesforce MuleSoft

    Blog in Salesforce, Salesforce Products

    We all know the feeling. You've got a to-do list, but somehow it never seems to get any shorter. Your IT team is no different.…

    Application Integration, Application Network, Applications, Automate Sales, Benefits of MuleSoft
    Apphienz Nov 22, 2022
    2,082  Views
    Dreamforce 2024: What can you look forward to?

    Dreamforce 2024: What Can You Look Forward To?

    Blog in Others

    Table of Contents   Dreamforce 2024 Learn the Hows and Whys of Trusted AI Be on the Cutting Edge of AI Navigate Future Trends Plan…

    Business Growth, Customer Engagement, Customer Satisfaction, Digital Initiatives, Digital Strategies
    Kizzy Consulting Jun 20, 2024
    851  Views
    Footer Forcetalks logo

    support@forcetalks.com

    • twitterx

    Quick Links

    Advertise with Us

    Salesforce® Articles

    Dreamforce 2023

    Top Salesforce® Bloggers 2023

    Top Salesforce Consultants

    Get Listed

    Company

    Contact Us

    About Us

    Privacy Policy

    Terms & Conditions

    InsightHub

    Salesforce Blogs

    Salesforce Videos

    Salesforce Groups

    Salesforce Jobs

    © 2025 - Forcetalks ● All Rights Reserved

    Salesforce® is a trademark of Salesforce® Inc. No claim is made to the exclusive right to use “Salesforce”. Any services offered within the Forcetalks website/app are not sponsored or endorsed by Salesforce®.

    Try AuditMyCRM - It is a Salesforce CRM Audit tool which comprehensively scans your Salesforce org and gives you the list of errors or warnings you need to take care of.
    We use cookies to enhance your browsing experience. Please see our privacy policy if you'd like more information on our use of cookies.