Skip to content

Instantly share code, notes, and snippets.

View douglascayers's full-sized avatar

Doug Ayers douglascayers

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / SampleOpportunityController.java
Created March 14, 2017 19:49
Example Visualforce page that shows Opportunities with or without Contact Roles.
public with sharing class SampleOpptyController {
public List<Opportunity> opportunities { get; set; }
public SampleOpptyController() {
this.opportunities = queryOpportunities();
}
private List<Opportunity> queryOpportunities() {
return [
@douglascayers
douglascayers / query-by-sobject
Last active March 24, 2018 17:11
Example SOQL to query for all fields using Dyanamic Query in Apex
// without an ID, simply specify the object to then derive the sobject type
DescribeSObjectResult describeResult = Account.getSObjectType().getDescribe();
List<String> fieldNames = new List<String>( describeResult.fields.getMap().keySet() );
String query =
' SELECT ' +
String.join( fieldNames, ',' ) +
' FROM ' +
describeResult.getName()