Toggle Side Panel

  • Blogs
  • 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
  • Contact Us
  • Discussions
More options
    Sign in Sign up
    • Blogs
    • 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
    • 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 10 years 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;

    • [adinserter block='9']
    • 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

    Popular Salesforce Blogs

    Integrating Salesforce with AI and Machine Learning Tools | The Ultimate Guide

    Integrating Salesforce with AI and Machine Learning Tools | The Ultimate Guide

    Blog in Others

    Salesforce is a cloud-based customer relationship management (CRM) platform that helps businesses manage their sales, marketing, and customer service operations. Integrating Salesforce with AI and…

    CRM Application, CRM Integration, CRM Platform, CRM solution, Cross-platform Services
    Afrah Mar 23, 2023
    2,287  Views
    Salesforce IOT + Raspberry Pi – Introduction and Setup

    Raspberry Pi – Introduction and Setup

    Blog in Salesforce integration

    Why you need this article? Because we will be doing Salesforce IOT examples in upcoming posts. In this post, we will be getting knowledge about…

    Raspberry Pi, Salesforce Integration, Salesforce IOT
    0to1Code.Com Nov 29, 2017
    8,894  Views

    JanBask's Salesforce Service Cloud Certification Program Acknowledged as a Top Choice

    Blog in Salesforce Training

    In today's rapidly evolving business landscape, providing exceptional customer experiences has become the cornerstone of success. As organizations strive to meet and exceed customer expectations,…

    Business Imperative, Case Management, Certification Preparation, Certification Program, Community of Learners
    Pawan Sep 19, 2023
    1,497  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®.

    We use cookies to enhance your browsing experience. Please see our privacy policy if you'd like more information on our use of cookies.