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 9 years, 9 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;

    • [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

    Why Insurance Agency Should Use Salesforce Partner Portal

    Blog in Salesforce

    Today, customers seek convenience in accessing information and services online. Especially for an insurance broker, adaptation to digital transformation has become the most prominent thing.…

    Centralized Inquiries, Competitive Edge, CRMJetty, Digital Excellence, Digital Payments
    CRMJetty Apr 26, 2024
    976  Views
    Salesforce Configuration

    Salesforce Configuration Vs Salesforce Customization and Their Best Practices

    Blog in Others, Salesforce customization

    What Is Configuration In Salesforce? Salesforce design is everything you can do inside the CRM framework.  This implies you can add another field to your…

    APEX Test, Apex Triggers, Application Synchronizes, Best Practices, Building Visual Power Pages
    Pooja Dec 30, 2020
    9,065  Views

    Better Data Quality in Salesforce Helps Decision Making

    Blog in Data, Salesforce Products, Salesforce Tools

    Ensuring the data in your Salesforce environment is accurate and up to date is a challenge for every organisation. To be able to make important…

    AppExchange, Data Capture, Data Cleaning, Data Entry, Data Quality
    KeyNode Oct 16, 2018
    9,380  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.