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
-
Can I Insert a Static Resource using Apex Code in Salesforce?
Posted by Ajit on April 14, 2016 at 8:28 AMCan I Insert a static resource using apex code in Salesforce?
Sourabh replied 9 years, 6 months ago 5 Members · 6 Replies -
6 Replies
-
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']
-
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> -
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> -
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> -
Thanx Abhinav, but when i performed the insert operation in my code then it was throwing the error that DML not allowed.
-
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.