Skip to content

Instantly share code, notes, and snippets.

@aleksrad
aleksrad / ReciperHelper
Last active April 15, 2020 23:17
PART_2_Final_Project
public with sharing class RecipeHelper {
//define custom exception
public class RecipeException extends Exception {}
// takes ingredient details and creates a corresponding SOBject record with the provided recipe ID as the parent
public static void addIngredient (String ingredientName, Integer measurementAmount, string measurementType, Id recipeId) {
Ingredient__c newIngredient = new Ingredient__c();
newIngredient.Name = ingredientName;
newIngredient.Measurement__c = measurementAmount;
@aleksrad
aleksrad / RecipeHandler.cls
Created April 6, 2020 22:21
RADWomen Part 2 Final Project
public with sharing class RecipeHelper {
//takes ingredient details and creates a corresponding SOBject record with the provided recipe ID as the parent
public static void addIngredient (String ingredientName, Integer measurementAmount, string measurementType, Id recipeId) {
Ingredient__c newIngredient = new Ingredient__c();
newIngredient.Name = ingredientName;
newIngredient.Measurement__c = measurementAmount;
newIngredient.Measurement_Type__c = measurementType;
newIngredient.Recipe__c = recipeId;
@aleksrad
aleksrad / Calculator_Test
Last active March 25, 2020 23:03
PART_2_Week6_Homework
@isTest
public class Calculator_Tests {
@isTest
public static void additionPositiveTest() {
integer a = 2;
integer b = 3;
integer c = Calculator.addition(a, b);
System.assertEquals(5, c, 'expected result is 5');
}
@aleksrad
aleksrad / RecipeHandler_Test
Last active March 19, 2020 16:33
PART_2_Week5_Homework
@IsTest
public class RecipertriggerHandler_Test {
@isTest
static void testIsDraft() {
Recipe__c recipe = new Recipe__c();
recipe.Name = 'New Recipe';
insert recipe;
@aleksrad
aleksrad / RecipeTrigger
Created March 11, 2020 19:38
PART_2_Week4_Homework
trigger RecipeTrigger on Recipe__c (before insert, after insert, before update, after update) {
if (Trigger.isBefore) {
if (Trigger.isInsert) {
ReciperTriggerHandler.beforeInsert(Trigger.new);
}
if (Trigger.isUpdate) {
ReciperTriggerHandler.beforeUpdate(Trigger.newMap);
@aleksrad
aleksrad / Lead Trigger
Last active March 4, 2020 21:45
PART2_Week3_Homework
trigger LeadTrigger on Lead (before insert, before update, after insert, after update) {
if (Trigger.isAfter) {
if(Trigger.isInsert) {
LeadTriggerHandler.LeadAfterInsert(Trigger.new);
}
else if(Trigger.isUpdate) {
LeadTriggerHandler.LeadAfterUpdate(Trigger.newMap, Trigger.oldMap);
}
}
/Create a class that has a method accepting two strings. The method searches for contacts
that have a last name matching the first string and a mailing postal code matching the second.
It gets the ID and Name of those contacts and returns them.
public class ContactSearch {
public static List<Contact> searchForContacts(string lastName, string mailingCode) {
List<Contact> myContacts = [SELECT Name, Id FROM Contact WHERE LastName =: lastName AND MailingPostalCode =: mailingCode];
System.debug(myContacts);
/*Create an Apex class with a method that returns an array (or list) of formatted strings ('Test 0', 'Test 1', ...).
The length of the array is determined by an integer parameter.*/
public class StringArrayTest {
public static List<String> generateStringArray(Integer returnedStrings) {
List<String> names = new List<String>();
for (integer i = 0; i < returnedStrings; i++) {
string oneName = 'Test ' + i;
names.add(oneName);
//The first line of our trigger gives the Trigger's name (AccountTrigger), names the object (Account) and lists the trigger_events that will cause the code below to execute
//For example, "before insert" indicates that before an account record (or records) is inserted, the trigger will be called.
//For more information on trigger syntax: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_syntax.htm
trigger AccountTrigger on Account (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
//Each section of code below handles a different event & timing combination. For now, we are demonstrating a trigger that has all of the logic right here.
//Later on we'll be looking at other ways of handling Trigger events using handler classes, but for now, we want to keep all the logic in once place as
//we're learning.
trigger AccountAddressTrigger on Account (before insert, before update) {
for (Account a : Trigger.new) {
if (a.Match_Billing_Address__c = true && a.BillingPostalCode != null) {
a.ShippingPostalCode = a.BillingPostalCode;
}
}
}