Skip to content

Instantly share code, notes, and snippets.

View capeterson's full-sized avatar

Chris Peterson capeterson

View GitHub Profile
trigger CountIncidents on itil_b__Incident__c (after insert, after update){
List<Id> AccountIDs = itil_b.Util.getIDFields(Trigger.new,'itil_b__account__c');
List<AggregateResult> totalIncidents = [SELECT itil_b__Account__c, COUNT(Id)
FROM itil_b__Incident__c WHERE itil_b__Account__c IN :AccountIDs AND itil_b__Account__c != null
GROUP BY ROLLUP(itil_b__Account__c)];
Map<id,Account> accounts = new Map<id,Account>([SELECT total_incidents__c, id FROM Account WHERE Id IN :accountIDs]);
for(AggregateResult ar: totalIncidents){
@capeterson
capeterson / gist:1241262
Created September 25, 2011 22:34
Tag Upload Controller
public with sharing class TagDemoController {
Public ApexPages.StandardController sc {get; set;}
public DocumentTag documentSelector {get; set;}
public TagDemoController(ApexPages.StandardController sc){
this.sc = sc;
documentSelector = new DocumentTag(); //Initialize our tag to avoid a null pointer
}
public PageReference save(){
@capeterson
capeterson / gist:1241267
Created September 25, 2011 22:37
Tag Upload Page
<apex:page standardController="SomeObject__c" extensions="TagDemoController">
<apex:form>
<apex:pageBlock>
<apex:pageBlockButtons>
<apex:commandButton action="{!save}" value="Save" />
</apex:pageBlockButtons>
Select a document: <apex:inputField value="{!documentSelector.itemId}" />
</apex:pageBlock>
</apex:form>
</apex:page>
@capeterson
capeterson / Explicit.xml
Created October 17, 2011 21:17
Visualforce Inline Help
<apex:pageBlockSection columns="1">
<apex:repeat value="{!$ObjectType.Incident__c.FieldSets.PortalNewLower}" var="field">
<apex:pageBlockSectionItem helpText="{!$ObjectType.Incident__c.Fields[field].InlinehelpText}">
<apex:outputLabel value="{!field.label}" />
<apex:inputField value="{!newIncident[field]}" required="{!field.required || field.DBRequired}" style="width:65%" />
</apex:pageBlockSectionItem>
</apex:repeat>
</apex:pageBlockSection>
/**
* Simple wrapper class to represent a record type sObject and RecordTypeInfo object all in one.
* This object needs to be used in any ui-facing record type operations going forward
* in order to support record type label overrides. See: http://www.ca-peterson.com/2011/10/tales-of-isv-supporting-override-labels.html
* @author cpeterson
**/
global with sharing class RecordTypeDescribe {
private static Map<String,Map<id,RecordTypeDescribe>> cache {
get{
if(cache == null)
@capeterson
capeterson / gist:1630454
Created January 18, 2012 02:26
apex:repeat indexes in visualforce pages
<apex:actionFunction name="removeItem" action="{!removeItem}" rerender="theForm">
<apex:param name="rowIndex" value="" />
</apex:actionFunction>
<apex:pageBlockTable var="index" value="{!memberIndexes}">
<apex:column width="18">
<apex:image value="{!URLFOR($Resource.RemoveItem)}" title="Remove Family Member" width="16" onClick="removeItem({!index});" />
</apex:column>
<apex:repeat var="field" value="{!$ObjectType.Person__c.FieldSets.NewFamily}">
<apex:column >
<apex:facet name="header">{!field.label}</apex:facet>
@capeterson
capeterson / gist:1642180
Created January 19, 2012 19:54
getObjectType
public static Schema.SObjectType getObjectType(id subject){
if(subject == null)
return null;
Schema.SObjectType result;
string target = subject;
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
string keyPrefix;
for(Schema.SObjectType describe: gd.values() ){
keyPrefix = describe.getDescribe().getKeyPrefix();
@capeterson
capeterson / gist:1857114
Created February 18, 2012 03:02
PushTopic deactivate
String pushTopicId = '0IFD0000000008jOAA';
PushTopic pt = [SELECT Id FROM PushTopic WHERE Id = :pushTopicId];
pt.Id = pushTopicId;
pt.IsActive = false;
update(pt)
@capeterson
capeterson / Component_Query.cls
Created February 28, 2012 06:03
Visualforce Query Component. Particularly useful for visualforce email templates.
global with sharing class Component_Query {
global String queryString {get; set;}
global List<sObject> results {get{
List<sObject> result = Database.Query(queryString);
return result;
} set; }
private static testmethod void contactTest(){
Contact c = new Contact(lastName = 'apex test contact');
insert c;
@capeterson
capeterson / LolEnums.cls
Created June 7, 2012 00:00
Replication of an issue with enum quality after summer12 salesforce upgrade.
global class LolEnums implements Database.batchable<sObject>, Database.Stateful{
public enum Places {FIRST, SECOND, THIRD}
public Places val;
global Database.QueryLocator start(Database.BatchableContext bc) {
val = Places.FIRST;
return Database.getQueryLocator('SELECT id FROM User');
}
global void execute(Database.BatchableContext BC, List<sObject> work){
System.assertEquals(val,Places.FIRST);