Skip to content

Instantly share code, notes, and snippets.

@olgaloza
olgaloza / CaseTriggerHandler.cls
Last active August 12, 2021 07:25
RAD 2 Homework 4 - Write a new trigger and handler class from scratch
public class CaseTriggerHandler {
//Handler Methods
public static void onBeforeInsert(List<Case> newCases) {
//Set default Reason if not provided at case creation
for ( Case c : newCases) {
if (c.Reason == NULL) {
System.debug('---->Case reason is '+c.Reason);
c.Reason = 'Other';
}
else {
@olgaloza
olgaloza / CaseTrigger.trigger
Last active November 7, 2018 06:47
RAD 2 Homework 4 - Write a new trigger and handler class from scratch
trigger CaseTrigger on Case (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
if(Trigger.isInsert && Trigger.isBefore){
CaseTriggerHandler.onBeforeInsert(Trigger.new);
}
if(Trigger.isInsert && Trigger.isAfter){
CaseTriggerHandler.onAfterInsert(Trigger.new);
}
@olgaloza
olgaloza / OpportunityUtility.cls
Created October 22, 2018 02:41
RAD 2 Homework 2 - Opportunity Class
//utility class for homework 2
public with sharing class OpportunityUtility {
//this is static a method that Takes an integer as an argument and returns a list of the top n Opportunities,
//ordered first by amount in descending order, then by Account Name.
//n is the integer that was passed in as an argument
//Include: Opportunity ID, Amount, Account Name, MainCompetitors__c, CloseDate, Stage
public static List<Opportunity> getTopOpportunities(Integer n) {
List<Opportunity> topOpportunities =
@olgaloza
olgaloza / OpportunityUtility.cls
Created October 22, 2018 02:41
RAD 2 Homework 2 - Opportunity Class
//utility class for homework 2
public with sharing class OpportunityUtility {
//this is static a method that Takes an integer as an argument and returns a list of the top n Opportunities,
//ordered first by amount in descending order, then by Account Name.
//n is the integer that was passed in as an argument
//Include: Opportunity ID, Amount, Account Name, MainCompetitors__c, CloseDate, Stage
public static List<Opportunity> getTopOpportunities(Integer n) {
List<Opportunity> topOpportunities =
@olgaloza
olgaloza / OpportunityUtility.cls
Created October 22, 2018 02:34
RAD 2 Homework 2 - OpportunityUtility class
//utility class for homework 2
public with sharing class OpportunityUtility {
//this is static a method that Takes an integer as an argument and returns a list of the top n Opportunities,
//ordered first by amount in descending order, then by Account Name.
//n is the integer that was passed in as an argument
//Include: Opportunity ID, Amount, Account Name, MainCompetitors__c, CloseDate, Stage
public static List<Opportunity> getTopOpportunities(Integer n) {
List<Opportunity> topOpportunities =
@olgaloza
olgaloza / createStockItems.apxc
Created April 16, 2018 23:18
Final Project - Create sample Stock Items class
public class createStockItems {
public static void createSampleStockItems(Integer numberOfItems) {
//Putting new stock items into a list to bulkify the code
List<Stock_Item__c> stockItemsList = new List<Stock_Item__c>();
//Loop through and add stock items:
for (Integer i = 0; i < numberOfItems; i++) {
System.debug('i = ' + i);
Stock_Item__c si = new Stock_Item__c();
si.Item_Name__c = 'Sample Stock Item ' + (i+1);
@olgaloza
olgaloza / StockItemTrigger.apxt
Created April 16, 2018 05:34
Final Project - Trigger
trigger StockItemTrigger on Stock_Item__c (before insert, before delete) {
//Instantiate StockItemHandler
//Before Insert Handling
if(Trigger.isInsert && Trigger.isBefore) {
//call the class in your handler for before insert
StockItemHandler.onBeforeInsert(Trigger.new);
}
@olgaloza
olgaloza / StockItemHandler_Test.apxc
Last active April 16, 2018 18:12
Final Project - Test Class
@isTest
private class StockItemHandler_Test {
@isTest static void testBeforeInsertFunctionality() {
try {
// Implement test code
// to test, try creating 2 items with same names.
Stock_Item__c item1 = new Stock_Item__c(Item_Name__c = 'Test Stock Item');
Stock_Item__c item2 = new Stock_Item__c(Item_Name__c = 'Test Stock Item');
/* FOR SOME REASON I COULDN'T GET THIS TEST VERSION TO WORK
@olgaloza
olgaloza / StockItemHandler.apxc
Created April 16, 2018 05:13
Final Project - Handler Class
//A Utility class to process Stock Item records from the Stock Item Handler
public with sharing class StockItemHandler {
//Default Constructor
public StockItemHandler() {
}
//Create methods here to handle the before insert, before delete and utility processes described in the requirements
//They should accept lists of Stock_Item__c records from the trigger
@olgaloza
olgaloza / RandomContactFactory.apxc
Created April 12, 2018 19:23
Create Test Data for Apex Tests Challenge
//@isTest
public class RandomContactFactory {
public static List<Contact> generateRandomContacts(Integer numContactsToGenerate, String FName) {
List<Contact> contactList = new List<Contact>();
for(Integer i=0;i<numContactsToGenerate;i++) {
Contact c = new Contact(FirstName=FName + ' ' + i, LastName = 'Contact '+i);
contactList.add(c);
System.debug(c);
}