Skip to content

Instantly share code, notes, and snippets.

View douglascayers's full-sized avatar

Doug Ayers douglascayers

View GitHub Profile
@douglascayers
douglascayers / ChatterApiPostMultipleFiles.java
Last active March 17, 2017 04:11
Chatter REST API "/connect/batch" resource to post multiple files
String header = '--some_random_boundary_string' + '\r\n';
String separator = '\r\n' + '--some_random_boundary_string' + '\r\n';
String footer = '\r\n' + '\r\n' + '--some_random_boundary_string--';
HttpRequest req = new HttpRequest();
req.setEndpoint( URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v39.0/connect/batch');
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
req.setHeader('Content-Type', 'multipart/form-data; boundary=some_random_boundary_string');
req.setHeader('Accept', 'application/json');
req.setBody(header +
@douglascayers
douglascayers / ChatterApiPostSingleFile.java
Created March 17, 2017 04:11
Chatter REST API "/connect/files/users/me" resource to post single file.
String header = '--some_random_boundary_string' + '\r\n';
String separator = '\r\n' + '--some_random_boundary_string' + '\r\n';
String footer = '\r\n' + '\r\n' + '--some_random_boundary_string--';
HttpRequest req = new HttpRequest();
req.setEndpoint( URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v39.0/connect/files/users/me');
req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
req.setHeader('Content-Type', 'multipart/form-data; boundary=some_random_boundary_string');
req.setHeader('Accept', 'application/json');
req.setBody(header +
@douglascayers
douglascayers / AccountTrigger.trg
Created March 22, 2017 00:11
Example trigger that delegates to helper class.
trigger AccountTrigger on Account ( before insert, before update, before delete,
after insert, after update, after delete ) {
AccountTriggerHandler handler = new AccountTriggerHandler();
if ( Trigger.isBefore && Trigger.isInsert ) {
handler.handleBeforeInsert( Trigger.new );
}
else if ( Trigger.isBefore && Trigger.isUpdate ) {
handler.handleBeforeUpdate( Trigger.old, Trigger.oldMap, Trigger.new, Trigger.newMap );
@douglascayers
douglascayers / AccountPage.vfp
Created March 27, 2017 17:11
Example Visualforce page that displays the Classic Attachments and Salesforce Files related lists.
<apex:page standardController="Account">
<!-- classic attachments related list -->
<apex:relatedList subject="{!account.id}" list="CombinedAttachments" />
<!-- new salesforce files related list -->
<apex:relatedList subject="{!account.id}" list="AttachedContentDocuments" />
</apex:page>
@douglascayers
douglascayers / XMLParser.java
Created April 22, 2017 19:21
Apex code snippet demonstrating how to parse CDATA in XML using Dom.XMLNode
String xml =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<root>' +
' <SomeNode><![CDATA[<b>contains html tags</b>]]></SomeNode>' +
'</root>';
// replace CDATA sections with parseable tokens
xml = xml.replaceAll( '<!\\[CDATA\\[', 'XML_CDATA_START' ).replaceAll( ']]>', 'XML_CDATA_END' );
// we will build up a map of original text and replacement text
@douglascayers
douglascayers / ContentNotePDFController.java
Created May 2, 2017 05:46
Proof of concept inspired by Matthew Hauck's question on the Success Community how to export Enhanced Note to PDF, https://success.salesforce.com/0D53A000035M8cM
/**
* Developed by Doug Ayers (douglascayers.com)
*
* Proof of concept inspired by Matthew Hauck's question
* on the Success Community how to export Enhanced Note to PDF
* https://success.salesforce.com/0D53A000035M8cM
*/
public with sharing class ContentNotePDFController {
public transient String title { get; set; }
@douglascayers
douglascayers / FeedItemTrigger.java
Created May 9, 2017 03:14
Proof of concept automatically add "Recommendation" link to Chatter Posts. Inspired by https://success.salesforce.com/0D53A000036B7Ai
/**
* For any Chatter post that is not a 'LinkPost' then add a FeedAttachment
* that links to system to integrate with, such as a third-party system to capture recommendations.
*
* https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_feedattachment.htm
*
* Inspired by https://success.salesforce.com/0D53A000036B7Ai
*/
trigger FeedItemTrigger on FeedItem (after insert) {
@douglascayers
douglascayers / LineNumberCounter.java
Created May 17, 2017 15:12
Simple Java method to count all lines in a file. Requires Java 8+
private static int getLineCount( File file ) throws Exception {
try ( LineNumberReader reader = new LineNumberReader( new FileReader( file ) ) ) {
while ( reader.readLine() != null ) {}
return reader.getLineNumber();
}
@douglascayers
douglascayers / ContentHubTest1.cls
Last active July 14, 2023 10:04
ConnectApi.ContentHub examples of adding and updating files in external data source (e.g. Google Drive or SharePoint Online) before Summer '17
/**
* Developed by Doug Ayers (douglascayers.com)
* Org62 Case 16584902
* GUS W-006274
*
* This test creates a file "new document.txt" with contents "Hello World"
* in an external data source, such as Google Drive or SharePoint Online.
*
* Despite being a unit test it has real side effects in the external system.
*
@douglascayers
douglascayers / UploadFile.html
Last active April 18, 2021 14:48
JavaScript snippet for uploading fille to Salesforce as ContentVersion and sharing to record as ContentDocumentLink via jsforce and jquery.
<apex:page>
<head>
<script src="{!$Resource.jquery224}"></script> <!-- https://jquery.com/ -->
<script src="{!$Resource.jsforce170}"></script> <!-- https://jsforce.github.io/ -->
<script>$j = jQuery.noConflict();</script>
</head>
<body>
<form>