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, 4 months 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

    Salesforce Lightning Scheduler Mastery In 2025

    Blog in Others

    Salesforce Lightning Scheduler is a powerful tool that can revolutionize how your team schedules appointments and manages resources. In 2025, mastering this tool is crucial…

    Benefits of Salesforce, Lightning Component, Lightning Web Components
    Cloudy Aug 1, 2025
    223  Views

    How to Optimize Your Salesforce Org's Performance and Productivity

    Blog in Salesforce

    Your company may have had Salesforce for some time already, having poured financial and human resources into its maintenance. There are several clouds, plenty of…

    AppExchange Apps, AppExchange Tools, audit, Chrome Extension, Configuration
    Twistellar Nov 24, 2022
    2,435  Views

    How to Integrate SMS with Salesforce CRM

    Blog in Salesforce

    Salesforce CRM is the main platform for dealing with customer data, sales processes, and service schedules. Nevertheless, numerous organizations continue to use outside tools in…

    360 SMS APP Jan 20, 2026
    818  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

    © 2026 - 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.