Skip to content

Instantly share code, notes, and snippets.

View charltonAthletic's full-sized avatar

Andy Hitchings charltonAthletic

View GitHub Profile
@isTest
private class TestDemoWebCallout {
static testMethod void testRestfulCallout() {
Lead l = new Lead();
l.Status = 'Open';
l.LastName = 'Owen';
l.FirstName = 'Bob';
l.Street = '1234 Sesame Street';
l.City = 'Houston';
/*
======================================================================
The following apex code demonstrates logging into another salesforce
org via the SOAP/XML web service api and then using session id to
query the standard REST API as well as the Chatter REST API.
To run this code, simply copy and paste into execute anonymous,
then replace the value of the first three variables accordingly.
NOTES:
(1) You'll need to create a remote site setting for both the login
@charltonAthletic
charltonAthletic / gist:bcddd1a5c4f1a720e773
Last active August 29, 2015 14:20
How many comments in Apex classes / triggers? (Execute anon script)
/* Taken from: http://www.johnwestenhaver.com/2015/03/how-much-of-your-apex-is-comments.html
I am unsure why string method .replaceAll is needed but I assume it is because the .trim method doesn't remove white
space at the start of a line.
http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_string.htm
This is an execute anon script */
Integer classLines = 0;
Integer classComments = 0;
Integer triggerLines = 0;
Integer triggerComments = 0;
// This Javascript snippet can be used on a custom button to pass a parameter to a report allowing dynamic reports.
// window.open("salesforce_instance.com/reportId?firstparamater)
// More info on using Javascript behind custom buttons to create dynamic reports can be found here: http://www.tgerm.com/2011/04/passing-params-to-reports-salesforce.html
window.open("https://eu0.salesforce.com/00O11000000P42z?pv0={!Account.Id}", "Key_Contacts", "toolbar=0, scrollbars=1");
// Typically you would use Batch Apex for this or if you wanted it via a trigger, it would utilize a helper class. Nonetheless, the code may be useful.
trigger NumberOfApprovedContracts on Contract (after delete, after insert, after update, after undelete) { // trigger context variables
Set<Id> toUpdateAccIds = new Set<Id>();
//Set<Id> zeroAccIds = new Set<Id>();
// IF CONTRACT CREATED OR UNDELETED, FOR ALL THESE CONTRACTS, IF APPROVED THEN ADD CONTRACT ACCOUNT ID TO THE SET
if(Trigger.isInsert || Trigger.isUndelete) {
//A common pattern for triggered operations in Apex
public static void updateContactPhone (List<Account> priorAccounts, List<Account> updatedAccounts) { // static method, takes 2 List params of Account sObject type
Set<Id> modifiedAccounts_Ids = new Set<Id>();
for (Integer i=0; i<updatedAccounts.size(); i++) { // Array notation - Alternative is... for (Account a : updatedAccounts) {
if (updatedAccounts[i].Phone != priorAccounts[i].Phone) { // if the phone number is different... (use dot notation to get to field value)
modifiedAccounts_Ids.add(updatedAccounts[i].Id); // add the Account Id to the List collection
}
}
// Custom controller for Visualforce Page - Example 1
// Probably the nicest part of this controller is: 'AND OwnerId = :UserInfo.getUserId'
// which utilizes the getUserId method from the UserInfo class. More info here: salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_userinfo.htm
public class OpportunityRevisitDatesController {
public ApexPages.StandardSetController ssc { // allows us to create list controllers
get {
if(ssc == null) {
<apex:page controller="OpportunityRevisitDatesController" sidebar="true" showHeader="true">
<apex:pageBlock >
<b>Hi {!$User.FirstName} - you have <u>{! opportunities.size}</u> New Business opportunities which have
a revisit date in the past. Please update these opportunities with a correct revisit date.</b>
<apex:pageBlockSection >
</apex:pageBlockSection>
<apex:pageBlockTable value="{! opportunities}" var="opps" rendered="{! opportunities.size>0}">
// This is a Salesforce formula which uses CASE, ISPICKVAL and boolean conditions.
CASE(Split_Region__c,
"Hong Kong", "Hong Kong",
"Singapore", "Singapore",
"Australia & NZ", "Australia & NZ",
"Japan", "Japan",
"Canada", "Canada",
"Caribbean", "LATAM",
"Latin America", "LATAM",
// This simple trigger changes the value of the standard Activity field 'Type' on task records to 'Email' if, before insert, the task is BLANK.
// This trigger could be improved upon by creating a method in a helper class to keep the trigger logicless.
trigger TaskWithBlankType of Task (before insert) { // 'before insert' is the trigger context variable
for (Task t : Trigger.new) { // for loop - for all tasks which match the trigger context variable...
if (t.Type == null) { // if branch nested in the for loop. If the type on the task is empty...
t.Type = 'Email'; // change the value of the Type to 'Email'. Remember: no DML operations required on 'before' trigger context variables
}
}
}